예제 #1
0
        public void OnePlayer()
        {
            Console.Clear();
            playing = true;

            do
            {
                Console.Write("\nPlease Enter the name of player 1: ");
                name1 = Console.ReadLine();
            }while (name1 == "");

            Console.Clear();

            player1 = new Player(name1);
            player2 = new Player("Robot");
            players.Add(player1);
            players.Add(player2);
            bships = new Battleship(player1, player2);

            bships.PlaceShips(player1);
            player2.AIBoard();

            Console.WriteLine("\nPress any button to continue...");
            Console.ReadKey();
            Console.Clear();

            player1.TrackBoard = player2.PlayerBoard;
            player2.TrackBoard = player1.PlayerBoard;

            //loop for the actual game
            while (playing == true)
            {
                foreach (Player player in players)
                {
                    if (GameOver())
                    {
                        Console.Clear();
                        rw.SaveLog(player1.PlayerName, player2.PlayerName);

                        playing = false;
                        break;
                    }
                    else
                    {
                        Console.Clear();
                        PlayerMove(player);

                        //if player2 has just taken his turn, allow the game to be saved by the players then exit
                        if (player == player2)
                        {
                            Console.WriteLine("\nPress the \"s\" key if you would like to save and quit, else press any key to continue. . . ");
                            ConsoleKeyInfo key = Console.ReadKey(false);
                            if (key.Key == ConsoleKey.S)
                            {
                                rw.SaveGame(player1, player2);
                                Console.WriteLine("\nSaved.\n");
                                Environment.Exit(0);
                            }
                        }

                        Console.Clear();
                        Console.WriteLine("Please pass to the next player.\n\nPress any button to start your turn.");
                        Console.ReadKey();
                    }
                }
            }
        }