private static void GameMaster() { while (!_gameEnd) { int userMove; //_gameBoard.PrintBoard(); do { userMove = int.Parse(Console.ReadLine()); } while (!_gameBoard.MoveLegal(userMove)); _gameBoard.Move(userMove, Board.PLAYER); if (_gameBoard.GameEnd(userMove, out char winner) && winner == Board.PLAYER) { //_gameBoard.PrintBoard(); //Console.WriteLine("Game over! PLAYER WON!"); _gameEnd = true; SendGameEnding(); } //if user didn't win, it's our turn int cpuMove = CPUTurn(); _gameBoard.Move(cpuMove, Board.CPU); _gameBoard.PrintBoard(); //did we win if (_gameBoard.GameEnd(cpuMove, out char winner1) && winner1 == Board.CPU) { _gameEnd = true; //Console.WriteLine("Game over! CPU WON!"); SendGameEnding(); } } }
private static double Evaluate(Board currentBoard, char lastPlayer, int lastInsertedColumn, int currentDepth) { //3. rule helpers bool allWins = true; bool allLoses = true; //check if the game is over if (currentBoard.GameEnd(lastInsertedColumn, out char winner)) { if (winner == Board.CPU) { return(1); } if (winner == Board.PLAYER) { return(-1); } } //if there is no winner, and we are at the max depth, return 0 if (currentDepth == 0) { return(0); } //if there are more depths to search, move to the next depth currentDepth--; char newPlayer = lastPlayer == Board.PLAYER ? Board.CPU : Board.PLAYER; double subNodesSum = 0; int possibleMoves = 0; for (int i = 0; i < currentBoard.Width; i++) { if (currentBoard.MoveLegal(i)) { possibleMoves++; currentBoard.Move(i, newPlayer); double subNodeResult = Evaluate(currentBoard, newPlayer, i, currentDepth); currentBoard.UndoMove(i); if (subNodeResult > -1) { allLoses = false; } if (subNodeResult < 1) { allWins = false; } if (subNodeResult >= 1 && newPlayer == Board.CPU) { return(1); //1. rule } if (subNodeResult <= -1 && newPlayer == Board.PLAYER) { return(-1); //2. rule } subNodesSum += subNodeResult; } } //3. rule if (allWins) { return(1); } if (allLoses) { return(-1); } subNodesSum /= possibleMoves; return(subNodesSum); }