Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("The Game of Battleships! (one-sided version)");

            var board    = new Board();
            var computer = new Computer(board);

            computer.PlaceShips();

            var finished = false;

            do
            {
                Console.Write("\nEnter the coordinates to shoot: ");
                var coordinates = Console.ReadLine();

                if (coordinates.Equals("show the board", StringComparison.InvariantCultureIgnoreCase))
                {
                    PrintTheBoard(board);
                    continue;
                }
                var shotResult = computer.MarkAShot(new Square(coordinates));

                var message = GetMessageBasedOnResult(shotResult);

                Console.WriteLine(message);

                if (computer.AllShipsSunk())
                {
                    Console.WriteLine("\nAll the computer's ships have sunk!");
                    finished = true;
                }
            } while (!finished);
        }
Пример #2
0
        /// <summary>
        /// This method handles the main game loop
        /// </summary>
        private void GameLoop()
        {
            Stream      endMusic;
            SoundPlayer soundPlayer;
            bool        exitGame = false;

            rendering.DrawGameWindow();
            rendering.DrawGameScreens(player);
            player.PlaceShips(rendering);
            computer.PlaceShips();
            turn = 0;
            while (!exitGame) //main game loop
            {
                rendering.DrawGameScreens(player);
                rendering.DrawInfoBox(player, computer, turn);
                player.TakeShot(computer, rendering);
                System.Threading.Thread.Sleep(1000);
                rendering.DrawGameScreens(player);
                computer.TakeShot(player, rendering);
                System.Threading.Thread.Sleep(1000);
                if (player.AllShipsDestroyed())
                {
                    endMusic    = Battleships.Properties.Resources.BattleshipsLoss;
                    soundPlayer = new SoundPlayer(endMusic);
                    soundPlayer.Play();
                    rendering.DrawVictoryScreen(1);
                    Console.ReadLine();
                    soundPlayer.Stop();
                    endMusic.Dispose();
                    soundPlayer.Dispose();
                    exitGame = true;
                }
                if (computer.AllShipsDestroyed())
                {
                    endMusic    = Battleships.Properties.Resources.BattleshipsVictory;
                    soundPlayer = new SoundPlayer(endMusic);
                    soundPlayer.Play();
                    rendering.DrawVictoryScreen(0);
                    Console.ReadLine();
                    soundPlayer.Stop();
                    endMusic.Dispose();
                    soundPlayer.Dispose();
                    exitGame = true;
                }
                turn++;
            }
        }