Ejemplo n.º 1
0
        public void Init(GameBoard board)
        {
            combinations = new int[2][];
              for (int i = 0; i < 2; i++)
              {
            combinations[i] = new int[70];
              }

              for (int r = 0; r < Constants.Rows; r++)
              {
            for (int c = 0; c < Constants.Columns; c++)
            {
              var result = TerminalPositionsTable.Get(c, r);

              foreach (var item in result)
              {
            var pos = board[c, r];
            if (pos != Player.None)
            {
              this[pos][item]++;
            }
              }

            }
              }
        }
Ejemplo n.º 2
0
        public void BlockTestHorizontal()
        {
            var state = new Player[7, 6];

              for (int i = 0; i < 7; i++)
              {
            for (int j = 0; j < 6; j++)
            {
              state[i, j] = Player.None;
            }
              }

              for (int i = 0; i < 3; i++)
              {
            state[i, Constants.Rows - 1] = Player.Human;
              }

              var board = new GameBoard(state);

              var game = new Game(board, Player.Human)
              {
            MoveGenerator = new LeftToRightMoveOrdering(),
            SearchStrategy = new AlphaBeta(),
            Input = new MockGameInput()
              };

              game.Start();

              Assert.IsTrue
              (
            game.Board[3, Constants.Rows - 1] == Player.AI,
            "AI had er voor moeten kiezen om de speler te blokkeren."
              );
        }
Ejemplo n.º 3
0
 public Game(GameBoard board, Player currentPlayer)
 {
     this.RecursionDepth = 7;
       this.Board = board;
       board.Game = this;
       TerminalPositionsTable.Init();
       this.PlayerCombinations = new PlayerCombinations(board);
 }
Ejemplo n.º 4
0
        public int FindBestColumnIndex(Game game, Player maxPlayer)
        {
            this.game = game;
              this.board = game.Board;
              this.maxPlayer = maxPlayer;

              NodesEvaluated = 0;

              moves = this.game.MoveGenerator.GetMovesForTurn(this.game);

              this.score = Negamax(0, -100000, 100000, maxPlayer);

              return this.bestColumn;
        }
Ejemplo n.º 5
0
        public static bool IsLeafNode(GameBoard board, out Player winner)
        {
            if (board.InTerminalState(Player.AI))
              {
            winner = Player.AI;
            return true;
              }
              else if (board.InTerminalState(Player.Human))
              {
            winner = Player.Human;
            return true;
              }

              winner = Player.None;
              return false;
        }
Ejemplo n.º 6
0
        private void BlockTestVertical(int column)
        {
            var state = new InitialBoardState();

              for (int i = 3; i < 6; i++)
              {
            state[column, i] = Player.Human;
              }

              var board = new GameBoard(state);

              var game = new Game(board, Player.Human)
              {
            MoveGenerator = new LeftToRightMoveOrdering(),
            SearchStrategy = new AlphaBeta(),
            Input = new MockGameInput()
              };

              game.Start();

              Assert.IsTrue(game.Board[column, 2] == Player.AI, "AI had er voor moeten kiezen om de speler te blokkeren.");
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Process.GetCurrentProcess().PriorityBoostEnabled = true;
              Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;

              const int iterations = 10;

              var searchDepths = new[] { 6, 9 };
              var searchStrategies = new[]
              {
            new
            {
              Name = "Alpha-beta", Type = typeof(AlphaBeta)
            },
            new
            {
              Name = "Minimax",Type = typeof(Minimax)
            }
              };

              var moveOrderStrategies = new[]
              {
            new
            {
              Name = "Links naar rechts", Type = typeof(LeftToRightMoveOrdering)
            },
            new
            {
              Name = "Statisch",Type =typeof(StaticMoveOrdering)
            },
            new
            {
              Name = "Dynamisch",Type =typeof(DynamicMoveOrdering)
            }
              };

              var initialStates = new[]
              {
            new
            {
              Name = "Situatie 1",
              State = InitialBoardState.GetTestPositionOne()
            },
            new
            {
              Name = "Situatie 2",
              State = InitialBoardState.GetTestPositionTwo()
            }
              };

              var sb = new StringBuilder();
              foreach (var searchDepth in searchDepths)
              {
            sb.AppendLine("Diepte " + searchDepth);
            sb.AppendLine();

            foreach (var state in initialStates)
            {
              sb.AppendLine(state.Name);
              sb.AppendLine();
              foreach (var moveOrdering in moveOrderStrategies)
              {
            sb.AppendLine(moveOrdering.Name);
            sb.AppendLine();

            foreach (var search in searchStrategies)
            {
              Console.WriteLine("Processing " + search.Name);
              TimeSpan time = TimeSpan.Zero;
              int bestColumn = 0;
              int nodesEvaluated = 0;
              for (int i = 0; i < iterations; i++)
              {
                var board = new GameBoard(state.State);
                var game = new Game(board, Player.Human)
                {
                  RecursionDepth = searchDepth,
                  Input = new InvalidGameInput(),
                  SearchStrategy = Activator.CreateInstance(search.Type) as ISearchStrategy,
                  MoveGenerator = Activator.CreateInstance(moveOrdering.Type) as IMoveOrderStrategy,
                };

                game.Start();
                time += game.TimeTaken;
                nodesEvaluated = game.NodesEvaluated;
                bestColumn = game.LastBestColumn;
                GC.Collect();
              }

              sb.AppendLine
              (
                search.Name + ": " + nodesEvaluated
                + " nodes doorzocht, duurde: "
                + new TimeSpan(time.Ticks / iterations).TotalMilliseconds + " ms, "
                + "beste kolom: " + bestColumn
              );
            }
            sb.AppendLine();
              }
            }
              }

              string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "benchmark");

              if (!Directory.Exists(path))
              {
            Directory.CreateDirectory(path);
              }

              using (StreamWriter outfile =
            new StreamWriter(Path.Combine(path, Guid.NewGuid().ToString("N") + ".txt")))
              {
            outfile.Write(sb.ToString());
              }
        }
Ejemplo n.º 8
0
        public static void LogBoard(GameBoard board)
        {
            builder.Append(board.ToString());

              builder.AppendLine();
        }
Ejemplo n.º 9
0
 public PlayerCombinations(GameBoard board)
 {
     Init(board);
 }