private int aiMinMaxAlgorithm(int i_Depth, out int o_RowOfBestMove, out int o_ColumnOfBestMove, OthelloGameLogic i_TheGameForNextMove) { o_RowOfBestMove = -1; o_ColumnOfBestMove = -1; int rowOfBestMove = -1; int columnOfBestMove = -1; int returnMaxScore = int.MinValue; int blackScore; int whiteScore; int currentScoreFromRecursiveMove; OthelloGameLogic theNextLevelBoard; if (!i_TheGameForNextMove.GameOver) { for (int row = 1; row <= BoardLength; row++) { for (int column = 1; column <= BoardLength; column++) { bool isLegalPosition = i_TheGameForNextMove.isLegalPositionForNewDisk(row, column, i_TheGameForNextMove.IsBlackTurn ? eCurrentDiskMode.Black : eCurrentDiskMode.White); if (isLegalPosition) { theNextLevelBoard = new OthelloGameLogic(i_TheGameForNextMove); theNextLevelBoard.MinMaxSimulation = true; if (theNextLevelBoard.IsBlackTurn) { theNextLevelBoard.BlackMakeMove(row, column); } else { theNextLevelBoard.WhiteMakeMove(row, column); } theNextLevelBoard.CalculateScore(out blackScore, out whiteScore); currentScoreFromRecursiveMove = blackScore - whiteScore; if (i_Depth == 0 || theNextLevelBoard.GameOver) { o_RowOfBestMove = row; o_ColumnOfBestMove = column; } else { currentScoreFromRecursiveMove = aiMinMaxAlgorithm(i_Depth - 1, out rowOfBestMove, out columnOfBestMove, theNextLevelBoard); } if (currentScoreFromRecursiveMove > returnMaxScore) { returnMaxScore = currentScoreFromRecursiveMove; o_RowOfBestMove = row; o_ColumnOfBestMove = column; } } } } } return(returnMaxScore); }
private bool startGameLogicEngine(int i_BoardSize, string i_PlayerOneName, bool i_IsPlayerOneComputer, string i_PlayerTwoName, bool i_IsPlayerTwoComputer) { int rowForNewDisk = 0; int columnForNewDisk = 0; int blackPlayerScore = 0; int whitePlayerScore = 0; bool isUserAskToExit = false; bool isGoodLogicMove = true; string messageToUser = string.Empty; messageToUser = string.Format("Input is not correct logically"); BoardGame = new OthelloGameLogic(i_BoardSize, i_PlayerOneName, i_IsPlayerOneComputer, i_PlayerTwoName, i_IsPlayerTwoComputer); while (!BoardGame.GameOver && !isUserAskToExit) { if (!isGoodLogicMove) { Console.WriteLine(messageToUser); } else { printBoard(); } isUserAskToExit = getMoveFromPlayer(out rowForNewDisk, out columnForNewDisk); if (BoardGame.IsBlackTurn) { isGoodLogicMove = BoardGame.BlackMakeMove(rowForNewDisk, columnForNewDisk); } else { OthelloGameLogic.s_IsItHumanPlaying = true; isGoodLogicMove = BoardGame.WhiteMakeMove(rowForNewDisk, columnForNewDisk); } } printBoard(); if (!isUserAskToExit) { BoardGame.CalculateScore(out blackPlayerScore, out whitePlayerScore); messageToUser = string.Format( "Black score is {0}, White score {1}. And the winner is {2}", blackPlayerScore, whitePlayerScore, blackPlayerScore > whitePlayerScore ? "Black" : blackPlayerScore == whitePlayerScore ? "Both" : "White"); Console.WriteLine(messageToUser); } return(isUserAskToExit); }