private int GetMaxMove(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.MinValue; 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; } } return bestMoveValue; }
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); }
public void MakeMove(int index) { if (m_GameOver) { throw new InvalidOperationException("Game is already over."); } m_Board[index] = (m_CurrentPlayer == Player.Player1 ? CellState.Player1 : CellState.Player2); OnBoardChanged(); GameState gameState = m_Board.GetGameState(); switch (gameState) { case GameState.InProgress: break; case GameState.Player1Won: case GameState.Player2Won: case GameState.Draw: OnGameOver(gameState); return; default: break; } // Now the other player becomes the next player m_CurrentPlayer = GetNextPlayer(m_CurrentPlayer); //if (m_CurrentPlayer == m_ComputerPlayer) //{ // // it is the computer's turn // DoComputerMove(); // return; //} }