Esempio n. 1
0
        private int GetMinMove(Board board, Player currentPlayer)
        {
            GameState gameState = board.GetGameState();

            if (gameState != GameState.InProgress)
            {
                return(-GetValueForCompletedGame(gameState, currentPlayer));
            }

            // the game has not yet ended
            int bestMoveValue = Int32.MaxValue;

            int[] possibleMoves = board.GetEmptyCells();
            for (int i = 0; i < possibleMoves.Length; i++)
            {
                Board successorBoard = board.Clone();
                successorBoard[possibleMoves[i]] = (currentPlayer == Player.Player1 ? CellState.Player1 : CellState.Player2);

                // We need to find the minimum of the max values
                Player nextPlayer = BoardManager.GetNextPlayer(currentPlayer);
                int    maxValue   = GetMaxMove(successorBoard, nextPlayer);

                if (maxValue < bestMoveValue)
                {
                    bestMoveValue = maxValue;
                }
            }

            return(bestMoveValue);
        }
Esempio n. 2
0
        internal int GetBestMove(Board board, Player currentPlayer)
        {
            int bestMoveValue = Int32.MinValue;
            int bestMove      = -1;

            int[] possibleMoves = board.GetEmptyCells();
            for (int i = 0; i < possibleMoves.Length; i++)
            {
                Board successorBoard = board.Clone();
                successorBoard[possibleMoves[i]] = (currentPlayer == Player.Player1 ? CellState.Player1 : CellState.Player2);

                // We need to find the maximum of the min values
                Player nextPlayer = BoardManager.GetNextPlayer(currentPlayer);
                int    minValue   = GetMinMove(successorBoard, nextPlayer);

                if (minValue > bestMoveValue)
                {
                    bestMoveValue = minValue;
                    bestMove      = possibleMoves[i];
                }
            }

            return(bestMove);
        }