Beispiel #1
0
        public void TwoPlayer(bool load)
        {
            Console.Clear();
            playing = true;

            //sets the game state to that loaded from the file chosen
            if (load)
            {
                string name = "";

                while (name == "")
                {
                    Console.WriteLine("\nPlease enter the name of the game to load.\n");
                    name = Console.ReadLine();
                }
                while (!rw.LoadTest(name))
                {
                    Console.WriteLine("\nSave file not found or corrupt. Please try again.\n");
                    name = Console.ReadLine();
                }

                rw.LoadGame(name);

                player1 = new Player(rw.Player1Name);
                player2 = new Player(rw.Player2Name);
                players.Add(player1);
                players.Add(player2);
                bships             = new Battleship(player1, player2);
                player1.HitCount   = rw.Player1Hit;
                player2.HitCount   = rw.Player2Hit;
                player1.MovesTaken = rw.Player1Moves;
                player2.MovesTaken = rw.Player2Moves;
                player1.LoadGameBoard(rw.Player2Track);
                player2.LoadGameBoard(rw.Player1Track);

                Console.WriteLine("\nLoaded!");
                Console.ReadKey();
            }
            //allow the players to set up the gameboards if a game is not loaded
            else
            {
                do
                {
                    do
                    {
                        Console.Write("\nPlease Enter the name of player 1: ");
                        name1 = Console.ReadLine();
                    }while (name1 == "");

                    Console.Clear();

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

                    Console.Clear();

                    if (name1 == name2)
                    {
                        Console.WriteLine("\nTwo players cannot use the same name!\n");
                    }
                }while (name1 == name2);

                Console.Clear();

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


                foreach (Player player in players)
                {
                    bships.PlaceShips(player);

                    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();
                    }
                }
            }
        }