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."); } }
private static void RandomlyPlaceShip(Game game, Guid playerId, int length) { var random = new Random(); while (true) { try { ShipOrientation orientation; if (random.Next(0, 1) == 0) { orientation = ShipOrientation.Horizontal; } else { orientation = ShipOrientation.Vertical; } Coordinate location; var longSide = random.Next(0, 9); var shortSide = random.Next(0, 9 - length); switch (orientation) { case ShipOrientation.Horizontal: location = new Coordinate(shortSide, longSide); break; default: location = new Coordinate(longSide, shortSide); break; } game.PutShip(playerId, new Ship() { Length = length, Orientation = orientation, Location = location, }); // Break out of the while loop. return; } catch (ShipCollisionException) { // Just try again! } } }