Ejemplo n.º 1
0
        public void Play()
        {
            char playAgainResponse;

            do
            {
                // Setup grid
                grid.SetUp();

                // Place ships
                grid.AddShip(new Battleship());
                grid.AddShip(new Destroyer());
                grid.AddShip(new Destroyer());

                while (grid.ShipsRemaining > 0)
                {
                    grid.PrintGridForPlayer();
                    Console.WriteLine("Enter grid reference to fire at: ");

                    string input = Console.ReadLine();

                    if (input == CheatCodes.REVEAL_GRID)
                    {
                        grid.PrintActualGrid();
                        continue;
                    }

                    if (input == CheatCodes.GIVE_UP)
                    {
                        break;
                    }

                    Point?chosenPoint = GridRef.GridRefToPoint(input);
                    if (chosenPoint == null)
                    {
                        Console.WriteLine("Please enter a valid point. It should be a letter directly followed by a number i.e. A1.");
                        continue;
                    }

                    Point point = (Point)chosenPoint;

                    if (!grid.IsPointValid(point))
                    {
                        Console.WriteLine("That point is outside the bounds of the grid");
                        continue;
                    }

                    string fireString = grid.Fire(point);
                    Console.WriteLine(fireString);
                }

                grid.PrintActualGrid();

                Console.WriteLine("Game Over");

                do
                {
                    Console.WriteLine();
                    Console.WriteLine("Play again? [y/n]: ");
                    playAgainResponse = char.ToUpper(Console.ReadKey().KeyChar);
                }while (playAgainResponse != 'Y' && playAgainResponse != 'N');
            } while (playAgainResponse == 'Y');
        }