// Calls minimax for each possible move and returns best move public static int FindBestMove(Connect4Game connect4, Connect4Board startBoard, int maxDepth) { MiniMaxVanilla.maxDepth = maxDepth; MiniMaxVanilla.connect4 = connect4; int player = MAXIMIZING; List <MoveScore> results = new List <MoveScore>(); Console.WriteLine("Move scores {move, score}: "); foreach (int move in connect4.GetPossibleMoves(startBoard)) { Connect4Board nextBoard = connect4.SimulateMove(startBoard, player, move); int searchScore = MiniMaxSearch(nextBoard, player * -1, 1); Console.Write("{" + move + ", " + searchScore + "} , "); results.Add(new MoveScore() { move = move, score = searchScore }); } int decidedMove = DecideMove(results); Console.WriteLine("Move chosen: " + decidedMove); return(decidedMove); }
// Calls minimax for each possible move and returns best move public static int FindBestMove(Connect4Game connect4, Connect4Board startBoard, int maxDepth) { MiniMaxAlphaBeta.maxDepth = maxDepth; MiniMaxAlphaBeta.connect4 = connect4; int player = MAXIMIZING; int a = int.MinValue; int b = int.MaxValue; List<MoveScore> results = new List<MoveScore>(); Console.WriteLine("\nMove scores {move, score, a}: "); foreach (int move in connect4.GetPossibleMoves(startBoard).OrderBy(x => rand.Next()).ToList()) { Connect4Board nextBoard = connect4.SimulateMove(startBoard, player, move); int searchScore = MiniMaxSearch(nextBoard, MINIMIZING, 1, a, b); // UPDATE ALPHA a = Math.Max(a, searchScore); Console.Write("{" + move + ", " + searchScore + ", " + a + "} , "); results.Add(new MoveScore() { move = move, score = searchScore }); } int decidedMove = DecideMove(results); Console.WriteLine("Move chosen: " + decidedMove); return decidedMove; }