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." ); }
public static int Evaluate(Game game, Player player) { var combinations = game.PlayerCombinations[player]; var opponentCombinations = game.PlayerCombinations[(Player)1 - (int)player]; int score = 0; for (int i = 0; i < combinations.Length; i++) { // This win-combination is only valuable if the opponent has no pieces in it if (opponentCombinations[i] == 0) { switch (combinations[i]) { case 1: score += 1; break; case 2: score += 10; break; case 3: score += 25; break; } } } return score; }
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; }
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."); }
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()); } }
public int[] GetMovesForTurn(Game game) { return moves; }