private static void CpuGuess(Game game, Guid playerId) { var random = new Random(); try { var guess = new Coordinate(random.Next(0, 10), random.Next(0, 10)); var result = game.Guess(playerId, guess); Console.WriteLine("Computer guessed {0}, it was a {1}!", guess.ToString(true), result); } catch (AlreadyGuessedException) { // Just continue and guess again. } }
private static void HumanGuess(Game game, Guid humanPlayerId) { try { Console.WriteLine("Please enter a cooridinate to guess."); var input = Console.ReadLine().ToUpper(); var guess = new Coordinate(input); var result = game.Guess(humanPlayerId, guess); Console.WriteLine("It was a {0}!", result); } catch (FormatException) { Console.WriteLine("User input must be in the form \"A5\"."); } catch (AlreadyGuessedException) { Console.WriteLine("You've already guessed that! Have another go."); } }
public static void Run() { var game = new Game(); game.StateChanged += new EventHandler<StateChangedArgs>(StateChangeHandler); game.CurrentPlayerChanged += new EventHandler(PlayerChangeHandler); var player1Id = game.RegisterPlayer("Player 1"); var player2Id = game.RegisterPlayer("Player 2"); // Get player 1 ready. game.PutShip(player1Id, new Ship() { Location = new Coordinate(3, 0), Class = ShipClass.Battleship, Orientation = ShipOrientation.Horizontal }); game.PutShip(player1Id, new Ship() { Location = new Coordinate(2, 1), Class = ShipClass.Destroyer, Orientation = ShipOrientation.Horizontal }); game.PutShip(player1Id, new Ship() { Location = new Coordinate(5, 3), Class = ShipClass.Destroyer, Orientation = ShipOrientation.Vertical }); game.StartGame(player1Id); // Get player 2 ready. game.PutShip(player2Id, new Ship() { Location = new Coordinate(3, 3), Class = ShipClass.Battleship, Orientation = ShipOrientation.Horizontal }); game.PutShip(player2Id, new Ship() { Location = new Coordinate(2, 4), Class = ShipClass.Destroyer, Orientation = ShipOrientation.Horizontal }); game.PutShip(player2Id, new Ship() { Location = new Coordinate(5, 5), Class = ShipClass.Destroyer, Orientation = ShipOrientation.Vertical }); game.StartGame(player2Id); var random = new Random(); while (game.State == GameState.Playing) { Guid currentPlayer; int playerNum; if (game.IsAwaitingPlayer(player1Id)) { playerNum = 1; currentPlayer = player1Id; } else { playerNum = 2; currentPlayer = player2Id; } try { var guess = new Coordinate(random.Next(0, 10), random.Next(0, 10)); var result = game.Guess(currentPlayer, guess); Console.WriteLine("Player {0} guessed {1}, it was a {2}!", playerNum, guess, result); } catch (AlreadyGuessedException) { // Just continue and guess again. } } if (game.HasPlayerWon(player1Id)) { Console.WriteLine("Player 1 has won."); } else { Console.WriteLine("Player 2 has won."); } }