Beispiel #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());
            }
        }
Beispiel #2
0
        private static int Minimax(GameBoard board, int depth, bool isMaximizing, int row, int col, int parentBestScore, TicTacToeGameRules myGameRules)
        {
            int maxScore = 1;
            int minScore = -1;
            int score;

            // See move
            //board.DrawGameBoard();
            //Console.ReadLine();
            if (TicTacToeGameRules.CheckForWin(board, row, col, myGameRules.currentPlayer.myPiece))
            {
                score = 1;
                return(score);
            }
            else if (TicTacToeGameRules.CheckForWin(board, row, col, myGameRules.opponent.myPiece))
            {
                score = -1;
                return(score);
            }
            else if (TicTacToeGameRules.CheckForDraw(board) || depth <= 0)
            {
                score = 0;
                return(score);
            }

            if (isMaximizing)
            {
                int bestScore = minScore;
                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, myGameRules.currentPlayer.myPiece);
                            score = Minimax(board, depth - 1, false, y, x, minScore, myGameRules);
                            board.SetGameBoardSpace(y, x, " ");
                            if (score > bestScore)
                            {
                                bestScore = score;
                            }
                            if (bestScore >= parentBestScore)
                            {
                                return(bestScore);
                            }
                        }
                    }
                }
                return(bestScore);
            }
            else
            {
                int bestScore = maxScore;
                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, myGameRules.opponent.myPiece);
                            score = Minimax(board, depth - 1, true, y, x, maxScore, myGameRules);
                            board.SetGameBoardSpace(y, x, " ");
                            if (score < bestScore)
                            {
                                bestScore = score;
                            }
                            if (bestScore <= parentBestScore)
                            {
                                return(bestScore);
                            }
                        }
                    }
                }
                return(bestScore);
            }
        }
        public void PlayGame()
        {
            bool changePlayerLoop;

            do
            {
                Console.Clear();
                Console.WriteLine(" Tic-Tac-Toe\n");
                changePlayerLoop = ChangePlayer();
            } while (!changePlayerLoop);

            GameBoard myGameBoard = new GameBoard(3, 3, " ", true, "left", "number", "top", "letter");
            GameBoard aiGameBoard = new GameBoard(3, 3, " ", true, "left", "number", "top", "letter");

            //GameBoard myGameBoard = new GameBoard(4, 4, " ", true, "left", "number", "top", "letter");
            //GameBoard aiGameBoard = new GameBoard(4, 4, " ", true, "left", "number", "top", "letter");

            //GameBoard myGameBoard = new GameBoard(5, 5, " ", true, "left", "number", "top", "letter");
            //GameBoard aiGameBoard = new GameBoard(5, 5, " ", true, "left", "number", "top", "letter");

            #region

            /*
             * myGameBoard.SetGameBoardSpace(0, 0, "O");
             * myGameBoard.SetGameBoardSpace(0, 1, " ");
             * myGameBoard.SetGameBoardSpace(0, 2, "X");
             *
             * myGameBoard.SetGameBoardSpace(1, 0, "X");
             * myGameBoard.SetGameBoardSpace(1, 1, "X");
             * myGameBoard.SetGameBoardSpace(1, 2, "O");
             *
             * myGameBoard.SetGameBoardSpace(2, 0, "O");
             * myGameBoard.SetGameBoardSpace(2, 1, " ");
             * myGameBoard.SetGameBoardSpace(2, 2, " ");
             */
            #endregion

            #region

            /*
             * myGameBoard.SetGameBoardSpace(0, 0, "#");
             * myGameBoard.SetGameBoardSpace(0, 1, "#");
             * myGameBoard.SetGameBoardSpace(0, 2, " ");
             * myGameBoard.SetGameBoardSpace(0, 3, "#");
             * myGameBoard.SetGameBoardSpace(0, 4, "#");
             *
             * myGameBoard.SetGameBoardSpace(1, 0, "#");
             * myGameBoard.SetGameBoardSpace(1, 1, " ");
             * myGameBoard.SetGameBoardSpace(1, 2, " ");
             * myGameBoard.SetGameBoardSpace(1, 3, " ");
             * myGameBoard.SetGameBoardSpace(1, 4, "#");
             *
             * myGameBoard.SetGameBoardSpace(2, 0, "#");
             * myGameBoard.SetGameBoardSpace(2, 1, " ");
             * myGameBoard.SetGameBoardSpace(2, 2, " ");
             * myGameBoard.SetGameBoardSpace(2, 3, " ");
             * myGameBoard.SetGameBoardSpace(2, 4, "#");
             *
             * myGameBoard.SetGameBoardSpace(3, 0, "#");
             * myGameBoard.SetGameBoardSpace(3, 1, " ");
             * myGameBoard.SetGameBoardSpace(3, 2, " ");
             * myGameBoard.SetGameBoardSpace(3, 3, " ");
             * myGameBoard.SetGameBoardSpace(3, 4, "#");
             *
             * myGameBoard.SetGameBoardSpace(4, 0, "#");
             * myGameBoard.SetGameBoardSpace(4, 1, "#");
             * myGameBoard.SetGameBoardSpace(4, 2, "#");
             * myGameBoard.SetGameBoardSpace(4, 3, "#");
             * myGameBoard.SetGameBoardSpace(4, 4, "#");
             */
            #endregion

            //TicTacToeGameRules myGameRules = new TicTacToeGameRules(myGameBoard, aiGameBoard, 5, 3, myPlayers);
            TicTacToeGameRules myGameRules = new TicTacToeGameRules(myGameBoard, aiGameBoard, 3, int.MaxValue, myPlayers);
            //TicTacToeGameRules myGameRules = new TicTacToeGameRules(myGameBoard, aiGameBoard, 5, 9, myPlayers);


            PlayAgain();
        }