public void updatePlayersOptions() { // this method is updating the Lists of the players options List <Cell> cellList = new List <Cell>(); GameUtilities.ePlayerColor lastPlayerTurn; bool isCellAnOption, shouldMethodAddCellsToUpdateList; m_WhitePlayerOptions.Clear(); m_BlackPlayerOptions.Clear(); lastPlayerTurn = m_PlayerTurn; shouldMethodAddCellsToUpdateList = false; foreach (Cell cellIteator in m_GameBoard.Matrix) { if (cellIteator.Sign == Cell.k_Empty) { m_PlayerTurn = GameUtilities.ePlayerColor.WhitePlayer; isCellAnOption = isPlayerMoveBlockingEnemy(cellIteator.Row, cellIteator.Column, ref cellList, shouldMethodAddCellsToUpdateList); if (isCellAnOption) { m_WhitePlayerOptions.Add(cellIteator); } m_PlayerTurn = GameUtilities.ePlayerColor.BlackPlayer; isCellAnOption = isPlayerMoveBlockingEnemy(cellIteator.Row, cellIteator.Column, ref cellList, shouldMethodAddCellsToUpdateList); if (isCellAnOption) { m_BlackPlayerOptions.Add(cellIteator); } } } // restore the turn of the last player m_PlayerTurn = lastPlayerTurn; }
public static string RequestPlayerToPlay(string i_PlayerName, GameUtilities.ePlayerColor i_PlayerTurn, Board.eBoardSize i_CurrentBoardSize) { // this method is recieving the player that should play now and asking the player to play bool isMoveValidate; string playerMoveInput, currentPlayerName, currentPlayerColor; char currentPlayerSign; currentPlayerName = i_PlayerName; if (i_PlayerTurn == GameUtilities.ePlayerColor.BlackPlayer) { currentPlayerColor = "Black"; currentPlayerSign = (char)GameUtilities.ePlayerColor.BlackPlayer; } else { currentPlayerColor = "White"; currentPlayerSign = (char)GameUtilities.ePlayerColor.WhitePlayer; } Console.WriteLine(string.Format("{0} player {1}, please play your turn => {2}.", currentPlayerColor, currentPlayerName, currentPlayerSign)); playerMoveInput = Console.ReadLine(); playerMoveInput = playerMoveInput.ToUpper(); isMoveValidate = isPlayerStringValid(playerMoveInput, i_CurrentBoardSize); while (!isMoveValidate) { ClearLines(2); SyntaxIsntValid(); playerMoveInput = Console.ReadLine(); playerMoveInput = playerMoveInput.ToUpper(); isMoveValidate = isPlayerStringValid(playerMoveInput, i_CurrentBoardSize); } return(playerMoveInput); }
public GameManager(Board i_GameBoard, GameUtilities.ePlayerColor i_PlayerTurn) { Board copiedBoard = i_GameBoard; m_GameBoard = copiedBoard; m_PlayerTurn = i_PlayerTurn; }
private void initialize(HumanPlayer i_WhiteHumanPlayer, HumanPlayer i_BlackHumanPlayer, PcPlayer i_BlackPCPlayer) { // this method is initializing the player options, scores and board. m_GameBoard.Initialize(); initializePlayersOptions(); initializePlayersScores(i_WhiteHumanPlayer, i_BlackHumanPlayer, i_BlackPCPlayer); m_PlayerTurn = GameUtilities.ePlayerColor.WhitePlayer; }
private bool isCellAnEnemy(Cell i_CellIterator, GameUtilities.ePlayerColor i_CurrentPlayerTurn) { // this method return true if the sign of the input cell is different from i_CurrentPlayerTurn bool isCellEnemy; isCellEnemy = i_CellIterator.Sign != (char)i_CurrentPlayerTurn && i_CellIterator.Sign != Cell.k_Empty; return(isCellEnemy); }
public static void InformPlayerItMoveIsntAnOption(GameUtilities.ePlayerColor i_PlayerTurn) { // this method infrom the player that is move is not in the player option and ask him to pick other move. string playerColor = GameUtilities.ePlayerColor.BlackPlayer == i_PlayerTurn ? "Black" : "White"; ClearLines(2); Console.WriteLine("{0} player, your choice is not an option, please pick other move.", playerColor); System.Threading.Thread.Sleep(3000); ClearLines(1); }
public void UpdateBoard(List <Cell> i_CellsToUpdate, GameUtilities.ePlayerColor i_PlayingPlayer) { // this method recieves a list of cells and a player color and put the correct sign in those cells. foreach (Cell currentCell in i_CellsToUpdate) { m_Board[currentCell.Row, currentCell.Column].Sign = (char)i_PlayingPlayer; } i_CellsToUpdate.Clear(); }
private static int heuristic(Board i_Board, GameUtilities.ePlayerColor i_playerTurn) { // heuristic method for Minimax algorithm int heuristicResult; int differencePCHuman; char playerTurnSign; heuristicResult = 0; differencePCHuman = differencePCScoreHumanScore(i_Board); playerTurnSign = i_playerTurn == GameUtilities.ePlayerColor.BlackPlayer ? (char)GameUtilities.ePlayerColor.BlackPlayer : (char)GameUtilities.ePlayerColor.WhitePlayer; heuristicResult += getCornersHeuristic(i_Board, playerTurnSign); return(heuristicResult + differencePCHuman); }
private bool isPlayerOptionEmpty(GameUtilities.ePlayerColor i_PlayerColor) { // this method recieve a PlayerColor and check if his options list is empty. bool isOptionListEmpty; if (i_PlayerColor == GameUtilities.ePlayerColor.BlackPlayer) { isOptionListEmpty = m_BlackPlayerOptions.Count == 0; } else { // if not black player - than its a white player. isOptionListEmpty = m_WhitePlayerOptions.Count == 0; } return(isOptionListEmpty); }
private void turnChangingManager() { // this method is managing the players turn changing if (m_PlayerTurn == GameUtilities.ePlayerColor.BlackPlayer && m_WhitePlayerOptions.Count > 0) { m_PlayerTurn = GameUtilities.ePlayerColor.WhitePlayer; } else if (m_PlayerTurn == GameUtilities.ePlayerColor.WhitePlayer && m_BlackPlayerOptions.Count > 0) { m_PlayerTurn = GameUtilities.ePlayerColor.BlackPlayer; } else if ((m_PlayerTurn == GameUtilities.ePlayerColor.BlackPlayer && m_WhitePlayerOptions.Count == 0) || (m_PlayerTurn == GameUtilities.ePlayerColor.WhitePlayer && m_BlackPlayerOptions.Count == 0)) { UI.InformTurnHasBeenChanged(m_PlayerTurn); } }
private static bool isGameOver(Board i_GameBoardState, GameUtilities.ePlayerColor i_MaximizingPlayer) { // this method passing all cell in the list and check if their is an option for maximizingPlayer GameManager tempGameManager = new GameManager(i_GameBoardState, i_MaximizingPlayer); List <Cell> cellLists = new List <Cell>(); bool addToCellsList, isCellAnOption, currentGameNotOver; addToCellsList = false; currentGameNotOver = false; foreach (Cell cellIteator in i_GameBoardState.Matrix) { isCellAnOption = tempGameManager.isPlayerMoveBlockingEnemy(cellIteator.Row, cellIteator.Column, ref cellLists, addToCellsList); if (isCellAnOption) { return(currentGameNotOver); } } currentGameNotOver = true; return(currentGameNotOver); }
public static int Minimax(Board i_GameBoardState, int i_Depth, GameUtilities.ePlayerColor i_MaximizingPlayer, ref Cell io_Cell, ref List <KeyValuePair <int, List <Cell> > > i_ListOfKeyValue) { // this method return a List of pairs < heuristic score , list of cells that lead to this score > // using Minimax algorithm, it return that list of pair I described by ref GameManager gameMangaerAI = new GameManager(i_GameBoardState, i_MaximizingPlayer); List <Cell> playerOptionList = new List <Cell>(); List <Cell> playerMovesList = new List <Cell>(); KeyValuePair <int, List <Cell> > scoreAndCellsListPair; Board copiedBoard; int eval, minEval, maxEval; if (i_Depth == 0 || isGameOver(i_GameBoardState, i_MaximizingPlayer)) { return(heuristic(i_GameBoardState, i_MaximizingPlayer)); } else { gameMangaerAI.updatePlayersOptions(); if (i_MaximizingPlayer == GameUtilities.ePlayerColor.BlackPlayer) { // this is PC's turn - Choose max value maxEval = int.MinValue; foreach (Cell cellIteator in gameMangaerAI.BlackPlayerOptions) { copiedBoard = gameMangaerAI.GameBoard.Clone(); gameMangaerAI.isPlayerMoveBlockingEnemy(cellIteator.Row, cellIteator.Column, ref playerOptionList); copiedBoard.UpdateBoard(playerOptionList, i_MaximizingPlayer); eval = Minimax(copiedBoard, i_Depth - 1, GameUtilities.ePlayerColor.WhitePlayer, ref io_Cell, ref i_ListOfKeyValue); if (eval > maxEval) { playerMovesList.Add(io_Cell); scoreAndCellsListPair = new KeyValuePair <int, List <Cell> >(eval, playerMovesList); i_ListOfKeyValue.Add(scoreAndCellsListPair); io_Cell.Row = cellIteator.Row; io_Cell.Column = cellIteator.Column; maxEval = eval; } } return(maxEval); } else { // this is Human player - Choose min value minEval = int.MaxValue; foreach (Cell cellIteator in gameMangaerAI.WhitePlayerOptions) { copiedBoard = gameMangaerAI.GameBoard.Clone(); gameMangaerAI.isPlayerMoveBlockingEnemy(cellIteator.Row, cellIteator.Column, ref playerOptionList); copiedBoard.UpdateBoard(playerOptionList, i_MaximizingPlayer); eval = Minimax(copiedBoard, i_Depth - 1, GameUtilities.ePlayerColor.BlackPlayer, ref io_Cell, ref i_ListOfKeyValue); if (eval < minEval) { playerMovesList.Add(io_Cell); scoreAndCellsListPair = new KeyValuePair <int, List <Cell> >(eval, playerMovesList); i_ListOfKeyValue.Add(scoreAndCellsListPair); minEval = eval; } } return(minEval); } } }
public static void DeclareWinner(int i_WhitePlayerScore, int i_BlackPlayerScore, GameUtilities.ePlayerColor i_WinnerColor) { // this method is printing a game over message, which contains the scores of both of the players and the winner name and color. StringBuilder winnerDeclerationMessage = new StringBuilder(string.Empty, 60); string winnerColor; if (i_WinnerColor == GameUtilities.ePlayerColor.BlackPlayer) { winnerColor = "Black player X"; } else { winnerColor = "White player O"; } winnerDeclerationMessage.AppendFormat("White player score: {1}{0}Black player score: {2}{0}The winner is: {3}!", Environment.NewLine, i_WhitePlayerScore, i_BlackPlayerScore, winnerColor); Console.WriteLine(winnerDeclerationMessage); }
public static void InformTurnHasBeenChanged(GameUtilities.ePlayerColor i_PlayerTurn) { // this method is informing the players that the turn has been changed. Console.WriteLine("{0} player, the enemy has no options. Its now your turn again", i_PlayerTurn); }
public HumanPlayer(GameUtilities.ePlayerColor i_PlayerColor) { // Human player c'tor m_PlayerColor = i_PlayerColor; }