Ejemplo n.º 1
0
        public static void PlayGame(Maze maze)
        {
            // Clears the console and displays a message for the player
            Console.Clear();
            Console.WriteLine("---- Game Started. You are in a random room inside the maze. ----");
            Console.WriteLine("");

            // Initialises variables allowing for random number generation
            int    numberOfRooms = maze.Rooms.Length + 1;
            Random r             = new Random();

            // Booleans deciding behaviour. gameOver is set to true when the exit has been found. canDropCoin is true when the users wealth is over 0, and the room has no treasure
            bool gameOver    = false;
            bool canDropCoin = false;

            // Creates an instance of player. Puts player in a random room. r.Next generates a random number between 1 and the number of rooms in the maze.
            Player player = new Player(r.Next(1, numberOfRooms));

            // Calls InsideRoom() with maze and player as parameters
            InsideRoom(maze, player);

            // This loop will run when gameOver is false
            while (!gameOver)
            {
                // Each iteration of the loop starts with canDropCoin being set to false, as it is checked further along whether it should be set to true
                canDropCoin = false;

                Console.WriteLine("You can either go North, East, South, or West (N/E/S/W)");

                // Checks if the current room has no treasure, and if the players wealth is above 0.
                if (maze.Rooms[player.CurrentRoomNo - 1].Treasure == 0 && player.Wealth > 0)
                {
                    // Set canDropCoin to true
                    canDropCoin = true;
                }

                // Displays message saying the user can pick up a coin, only if canDropCoin is true
                if (canDropCoin)
                {
                    Console.WriteLine("Or you can drop a coin (D)");
                }

                Console.Write("Selection: ");

                // Gets the user input and converts it to upper case
                string direction = Console.ReadLine().ToUpper();

                // Gets the index of the current room in the room array
                int roomIndex = player.CurrentRoomNo - 1;

                // Gets the room object of the current room from the room array
                Room thisRoom = maze.Rooms[roomIndex];

                // If the user input is one of the directional options
                if (direction == "N" || direction == "E" || direction == "S" || direction == "W")
                {
                    // Gets the passage number of the direction selected.
                    int passage = thisRoom.GetPassage(direction);

                    // If the passage number is not equal to 0
                    if (passage != 0)
                    {
                        // Checks if the passage is an exit
                        if (maze.Passages[passage - 1].IsExit)
                        {
                            // Sets gameOver to true, ending the game
                            gameOver = true;
                        }
                        else
                        {
                            // Calls the GoToRoom() method on player. This uses the GetNewRoom() method of the passage to get the room that the passage leads to
                            player.GoToRoom(maze.Passages[passage - 1].GetNewRoom(player.CurrentRoomNo));

                            // Calls InsideRoom
                            InsideRoom(maze, player);
                        }
                    }
                    else
                    {
                        // If the passage number is 0, this message will be displayed, and the loop will restart.
                        Console.WriteLine("-----------------------");
                        Console.WriteLine("You can't go this way. ");
                        Console.WriteLine("-----------------------");
                        continue;
                    }
                }
                // If the user inputs 'D' to drop a coin
                else if (direction == "D")
                {
                    // If canDropCoin is true
                    if (canDropCoin)
                    {
                        // Sets the treasure attribute of the current room to be a coin. The coin will always be the last treasure object in the array, so I use Treasure.Length to get the treasure id of the coin
                        maze.Rooms[player.CurrentRoomNo - 1].Treasure = maze.Treasures.Length;

                        // Removes 1 wealth from the player
                        player.Wealth -= 1;
                        Console.WriteLine("-----------------------");
                        Console.WriteLine("Coin dropped. Wealth -1.");
                        Console.WriteLine("-----------------------");
                    }
                    // If the player cannot drop a coin
                    else
                    {
                        // Tells the player they can't do that
                        Console.WriteLine("-----------------------");
                        Console.WriteLine("You can't do that. ");
                        Console.WriteLine("-----------------------");
                    }
                }
                // If anything other than NESWD is entered
                else
                {
                    // Tells player to enter a valid direction, restarts the loop
                    Console.WriteLine("-----------------------");
                    Console.WriteLine("Please enter a valid direction.");
                    Console.WriteLine("-----------------------");
                    continue;
                }
            }

            // Runs when gameOver is true and the loop is over.
            // Displays the wealth of the player, and prompts the user to press any key.
            Console.WriteLine("-----------------------");
            Console.WriteLine("You found the exit!");
            Console.WriteLine("Final Wealth: {0}", player.Wealth);
            Console.WriteLine("-----------------------");
            Console.WriteLine("Press any key to continue...");

            // Clears the console after the user presses a key. This leads to the menu being displayed.
            Console.ReadKey();
            Console.Clear();
        }