예제 #1
0
 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.
     }
 }
예제 #2
0
 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.");
     }
 }
예제 #3
0
        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.");
            }
        }
예제 #4
0
        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.");
            }
        }
예제 #5
0
        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!
                }
            }
        }
예제 #6
0
 private static void RandomlyPlaceShips(Game game, Guid playerId)
 {
     RandomlyPlaceShip(game, playerId, 4);
     RandomlyPlaceShip(game, playerId, 4);
     RandomlyPlaceShip(game, playerId, 5);
 }