Exemplo n.º 1
0
        public static void PlayGame(this char[,] board)
        {
            board.FillWithEmptyCells();
            board.PrintBoard();
            bool isGameOver = false;
            int  xForO = 0, yForO = 0;

            while (!isGameOver)
            {
                board.PlayerMoves();
                int minResult = int.MaxValue;
                int result;
                for (int x = 0; x < 3; x++)
                {
                    for (int y = 0; y < 3; y++)
                    {
                        if (board[x, y] == EMPTY_CELL)
                        {
                            board[x, y] = COMPUTER_PLAYER;
                            result      = board.MinimaxWithAlphaBetaPruning(YOU_PLAYER, int.MinValue, int.MaxValue);
                            if (result < minResult)
                            {
                                minResult = result;
                                xForO     = x;
                                yForO     = y;
                            }
                            board[x, y] = EMPTY_CELL;
                        }
                    }
                }
                board[xForO, yForO] = COMPUTER_PLAYER;

                Console.Clear();
                board.PrintBoard();

                char terminalState = board.CheckForGameOver();
                if (terminalState != EMPTY_CELL)
                {
                    if (terminalState == YOU_PLAYER)
                    {
                        Console.WriteLine("You win!");
                    }
                    else if (terminalState == COMPUTER_PLAYER)
                    {
                        Console.WriteLine("Computer wins!");
                    }
                    else
                    {
                        Console.WriteLine("It's a draw!");
                    }
                    isGameOver = true;
                }
            }
        }
Exemplo n.º 2
0
        // Minimax with alpha-beta pruning algorithm used by computer to predict its moves
        public static int MinimaxWithAlphaBetaPruning(this char[,] board, char player, int alpha, int beta)
        {
            char terminalState = board.CheckForGameOver();

            if (terminalState != EMPTY_CELL) // board[,] is a leaf, we are in a terminal state
            {
                if (terminalState == EQUAL)
                {
                    return(DRAW);
                }
                else
                if (terminalState == YOU_PLAYER)     // if you win
                {
                    return(VICTORY);
                }
                else // if computer wins
                {
                    return(LOSS);
                }
            }
            else // board[,] is NOT a leaf, the state is not terminal
            {
                char nextPlayer;
                if (player == YOU_PLAYER)
                {
                    nextPlayer = COMPUTER_PLAYER;
                }
                else
                {
                    nextPlayer = YOU_PLAYER;
                }

                int newAlpha = alpha, newBeta = beta;
                int value;

                if (player == COMPUTER_PLAYER) // board[,] is a node for minimizing your score
                {
                    for (int x = 0; x < 3; x++)
                    {
                        for (int y = 0; y < 3; y++)
                        {
                            if (board[x, y] == EMPTY_CELL)
                            {
                                board[x, y] = COMPUTER_PLAYER;
                                value       = board.MinimaxWithAlphaBetaPruning(nextPlayer, newAlpha, newBeta);
                                board[x, y] = EMPTY_CELL;
                                newBeta     = Math.Min(newBeta, value);
                                // beta pruning
                                if (newBeta <= newAlpha)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    return(newBeta);
                }
                else // You are the player and board[,] is a node for maximizing
                {
                    for (int x = 0; x < 3; x++)
                    {
                        for (int y = 0; y < 3; y++)
                        {
                            if (board[x, y] == EMPTY_CELL)
                            {
                                board[x, y] = YOU_PLAYER;
                                value       = board.MinimaxWithAlphaBetaPruning(nextPlayer, newAlpha, newBeta);
                                board[x, y] = EMPTY_CELL;
                                newAlpha    = Math.Max(newAlpha, value);
                                // alpha pruning
                                if (newAlpha >= newBeta)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    return(newAlpha);
                }
            }
        }