/// <summary> /// A helper method for competing two players against each other. /// </summary> /// <param name="xPlayer">The player to act first.</param> /// <param name="oPlayer">The player to act second.</param> /// <returns> /// The square type of the winner, or SquareTypes.N if the game /// was a draw. /// </returns> public static SquareTypes PlayGameToEnd(IPlayer xPlayer, IPlayer oPlayer) { TicTacToeGame game = new TicTacToeGame(); SquareTypes winner = SquareTypes.N; // Play until we have a winner or the game board is full. for (int moveNum = 0; moveNum < 9 && winner == SquareTypes.N; moveNum++) { IPlayer curPlayer; SquareTypes curSquareType; // Determine if it's X's or O's turn to act. if (moveNum % 2 == 0) { curPlayer = xPlayer; curSquareType = SquareTypes.X; } else { curPlayer = oPlayer; curSquareType = SquareTypes.O; } // Get the next move from the current player. var move = curPlayer.GetMove(game.Board); // Check to make sure the player's move is legal. Debug.Assert(game.IsEmpty(move.X, move.Y), "Player tried to make an illegal move!"); // Set the board to the player's move. game.Board[move.X, move.Y] = curSquareType; // Start checking if we have a winner once xPlayer has made // at least 3 moves. if (moveNum > 3) { winner = game.GetWinner(); } } // Return the square type of the winner, or SquareTypes.N if it was a draw. return(winner); }
/// <summary> /// A helper method for competing two players against each other. /// </summary> /// <param name="xPlayer">The player to act first.</param> /// <param name="oPlayer">The player to act second.</param> /// <returns> /// The square type of the winner, or SquareTypes.N if the game /// was a draw. /// </returns> public static SquareTypes PlayGameToEnd(IPlayer xPlayer, IPlayer oPlayer) { TicTacToeGame game = new TicTacToeGame(); SquareTypes winner = SquareTypes.N; // Play until we have a winner or the game board is full. for (int moveNum = 0; moveNum < 9 && winner == SquareTypes.N; moveNum++) { IPlayer curPlayer; SquareTypes curSquareType; // Determine if it's X's or O's turn to act. if (moveNum % 2 == 0) { curPlayer = xPlayer; curSquareType = SquareTypes.X; } else { curPlayer = oPlayer; curSquareType = SquareTypes.O; } // Get the next move from the current player. var move = curPlayer.GetMove(game.Board); // Check to make sure the player's move is legal. Debug.Assert(game.IsEmpty(move.X, move.Y), "Player tried to make an illegal move!"); // Set the board to the player's move. game.Board[move.X, move.Y] = curSquareType; // Start checking if we have a winner once xPlayer has made // at least 3 moves. if(moveNum > 3) winner = game.GetWinner(); } // Return the square type of the winner, or SquareTypes.N if it was a draw. return winner; }