static void Main(string[] args) { // Leave the test mode here in case it's useful later! if (args.Any(a => a.ToLower() == "/test")) { TestMode.Run(); return; } var game = new Game(); var cpuPlayerId = game.RegisterPlayer("Cpu player"); var humanPlayerId = game.RegisterPlayer("Human Player"); RandomlyPlaceShips(game, cpuPlayerId); game.StartGame(cpuPlayerId); RandomlyPlaceShips(game, humanPlayerId); game.StartGame(humanPlayerId); while (game.State == GameState.Playing) { if (game.IsAwaitingPlayer(cpuPlayerId)) { CpuGuess(game, cpuPlayerId); } else { HumanGuess(game, humanPlayerId); } } if (game.HasPlayerWon(humanPlayerId)) { Console.WriteLine("You have won!"); } else { Console.WriteLine("Sorry, you lost this time."); } }
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."); } }