示例#1
0
        public static void ExpertMove(GameBoard board, GameBoard aiboard, Player player, TicTacToeGameRules myGameRules, out int row, out int col)
        {
            row = 0;
            col = 0;
            int bestScore = -1;
            int score;

            aiboard.InitializeGameBoard();

            if (board.rows == 3 && board.cols == 3 && board.GetGameBoardSpace(1, 1) == " ")
            {
                row = 1;
                col = 1;
            }
            else
            {
                for (int y = 0; y < board.rows; y++)
                {
                    for (int x = 0; x < board.cols; x++)
                    {
                        if (board.GetGameBoardSpace(y, x) == " ")
                        {
                            board.SetGameBoardSpace(y, x, player.myPiece);
                            score = Minimax(board, myGameRules.minimaxDepth, false, y, x, -1, myGameRules);
                            board.SetGameBoardSpace(y, x, " ");
                            if (score > bestScore)
                            {
                                bestScore = score;
                            }
                            aiboard.SetGameBoardSpace(y, x, score.ToString());
                            //aiGameBoard.DrawGameBoard();
                        }
                    }
                }
                // See scores
                //aiboard.DrawGameBoard();
                //Console.WriteLine($"bestScore = {bestScore}");
                //Console.ReadLine();

                // Pick Random From Best
                Random rand = new Random();
                do
                {
                    row = rand.Next(board.rows);
                    col = rand.Next(board.cols);
                } while (aiboard.GetGameBoardSpace(row, col) != bestScore.ToString());
            }
        }