static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.UTF8; Chessboard testBoard = new Chessboard(); while (true) { testBoard.displayBoard(1); Regex validMove = new Regex("^[a-hA-H][1-8]-[a-hA-H][1-8]$"); String lastMove = ""; while (testBoard.stringMakeMove(lastMove, 1) == 0) { Console.Write("Please enter a valid move: "); lastMove = Console.ReadLine(); } Console.WriteLine("Valid move! Score: " + testBoard.evaluateBoard()); testBoard.displayBoard(1); int[][] minMaxResult = minimax(3, testBoard, 1); Console.WriteLine("\r\n\r\nComputer made move " + moveToText(minMaxResult[1], minMaxResult[2]) + "\r\n"); testBoard.stringMakeMove(moveToText(minMaxResult[1], minMaxResult[2]), -1); } /* * Console.WriteLine(); * Dictionary<int[], List<int[]>> currentLegalMoves = testBoard.getLegalMoves(-1, true); * foreach (KeyValuePair<int[], List<int[]>> moveSet in currentLegalMoves) { * foreach (int[] move in moveSet.Value) { * Console.WriteLine("Legal move: " + (char)(moveSet.Key[1] + 65) + (moveSet.Key[0] + 1) + "-" + (char)(move[1] + 65) + (move[0] + 1)); * Chessboard nextMove = Chessboard.DeepCopy(testBoard); * nextMove.stringMakeMove(Chessboard.moveToText(moveSet.Key, move), -1); * // nextMove.displayBoard(1); * // Console.WriteLine("Score for this board: " + nextMove.evaluateBoard()); * // Console.WriteLine(); * } * } * Console.WriteLine(); * * Console.WriteLine(Convert.ToInt32(Console.ReadLine())); * * int[] moveToCheck = new int[] { Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()) }; * Console.WriteLine(currentLegalMoves[moveToCheck]); * * */ Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
static public int[][] minimax(int layers, Chessboard board, int player) { if (layers == 0) { int[][] highScoreArr = new int[2][]; highScoreArr[0] = new int[2] { board.evaluateBoard(), 0 }; highScoreArr[1] = new int[2] { 0, 0 }; // Console.WriteLine("Final board score: " + highScoreArr[0][0]); return(highScoreArr); } else { Dictionary <int[], List <int[]> > moves = board.getLegalMoves(player, true); int score = 0; bool firstRun = true; int highScore = 0; int[] bestMoveTo = { 0, 0 }; int[] bestMoveFrom = { 0, 0 }; foreach (KeyValuePair <int[], List <int[]> > moveSet in moves) { foreach (int[] move in moveSet.Value) { Chessboard newBoard = Chessboard.DeepCopy(board); newBoard.stringMakeMove(moveToText(moveSet.Key, move), player); if (firstRun) { score = minimax(layers - 1, newBoard, -player)[0][0]; highScore = score; bestMoveFrom = moveSet.Key; bestMoveTo = move; firstRun = false; } else { score = minimax(layers - 1, newBoard, -player)[0][0]; } if ((score > highScore && player == 1) || (score < highScore && player == -1)) { // Console.WriteLine("Sick move found"); highScore = score; bestMoveFrom = moveSet.Key; bestMoveTo = move; } } } int[][] highScoreArr = new int[3][]; highScoreArr[0] = new int[2] { highScore, 0 }; highScoreArr[1] = bestMoveFrom; highScoreArr[2] = bestMoveTo; // if (layers == 2) { Console.WriteLine("Best move is " + bestMoveFrom[0] + ":" + bestMoveFrom[1] + " -> " + bestMoveTo[0] + ":" + bestMoveTo[1]); } if (layers == 2) { Console.Write("."); } return(highScoreArr); } }