예제 #1
0
        //Pre: none
        //Post: none
        //Description: this method takes the player through a full game. At the end, it asks the player if they would like to play again or quit, and acts accordingly.
        public static void PlayGame()
        {
            //This string will store the user's initial input
            string userInput;

            //This queue will store the input as a queue
            SequenceQueue seqQueue = new SequenceQueue();

            //This bool will store whether the player has won or not
            bool playerWon = false;

            //Before each game, reset the positions of the player, goal, and obstacles
            gameGrid.SetGame();


            //This for loop ensures the game goes on for NUM_ROUNDS rounds
            for (int i = 0; i < NUM_ROUNDS; i++)
            {
                //Clearing the sequence after each game before each round.
                seqQueue.Clear();

                Console.Clear();
                Console.WriteLine("REACH THE GOAL \n-----------------");
                Console.WriteLine("Enter a sequence of moves (wasd) to try to reach the goal. \nPress enter when you've finished making your sequence.");
                //input to DisplayGrid is true because we want to display the original position of the player
                gameGrid.DisplayGrid(true);

                userInput = Console.ReadLine();


                //Try catch block to catch any exceptions thrown by the Enqueue function
                try
                {
                    //For each character in the user's input, enqueue it. The enqueue method throws an exception if the character is not a valid move.
                    for (int j = 0; j < userInput.Length; j++)
                    {
                        seqQueue.Enqueue(userInput[j]);
                    }
                }
                catch (ArgumentException)
                {
                    //decrementing i to ensure the player does not lose a round for entering faulty input
                    i--;
                    Console.WriteLine("One or more of the characters you entered is invalid. \nPress ENTER to try again.");
                    Console.ReadLine();
                    continue;
                }

                //Try catch block to catch any exceptions thrown by the CheckWin function
                try
                {
                    //CheckWin takes the player's Queue of moves
                    playerWon = gameGrid.CheckWin(seqQueue);

                    //If the CheckWin function returns true, break out of the loop.
                    if (playerWon)
                    {
                        Console.Clear();
                        Console.WriteLine("REACH THE GOAL \n-----------------");
                        Console.WriteLine("Enter a sequence of moves (wasd) to try to reach the goal. \nPress enter when you've finished making your sequence.");
                        //input to DisplayGrid is false because we want to display the player's  updated position rather than the original position
                        gameGrid.DisplayGrid(false);
                        break;
                    }
                    //Otherwise, notify the user that they haven't yet succeeded and display the number of tries they have left.
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("REACH THE GOAL \n-----------------");
                        Console.WriteLine("Enter a sequence of moves (wasd) to try to reach the goal. \nPress enter when you've finished making your sequence.");
                        //input to DisplayGrid is false because we want to display the player's  updated position rather than the original position
                        gameGrid.DisplayGrid(false);
                        Console.WriteLine($"That sequence didn't get you to the goal. ({NUM_ROUNDS - i - 1} tries left). \nPress ENTER to continue.");
                    }
                }
                catch (ArgumentException e)
                {
                    Console.Clear();
                    Console.WriteLine("REACH THE GOAL \n-----------------");
                    Console.WriteLine("Enter a sequence of moves (wasd) to try to reach the goal. \nPress enter when you've finished making your sequence.");
                    //input to DisplayGrid is false because we want to display the player's  updated position rather than the original position
                    gameGrid.DisplayGrid(false);
                    Console.WriteLine($"{e.Message} \nPress ENTER to continue.");
                }
                Console.ReadLine();
            }


            //Display to the user whether they have won or not, and ask them if they would like to play again.
            if (playerWon)
            {
                Console.WriteLine("You reached the goal! Good job! Would you like to play again? \nEnter 'y' if yes, and anything else if not.");
            }
            else
            {
                Console.WriteLine("You didn't win this time. Would you like to play again? \nEnter 'y' if yes, and anything else if not.");
            }
            //If the user would like to play again, call PlayGame() again. Otherwise, display a goodbye message.
            if (Console.ReadLine() == "y" ? true : false)
            {
                PlayGame();
            }
            else
            {
                Console.WriteLine("Thanks for playing. Bye!");
            }
        }
예제 #2
0
        //Pre: accepts a character sequence of the user's moves.
        //Post: none.
        //Description: this function carries out the user's sequences of moves, and
        public bool CheckWin(SequenceQueue moveSequence)
        {
            tempPlayerPos = playerPos;

            //Keeps track of the next character in the queue
            char nextMove;

            //Stores the queue's initial size
            int size = moveSequence.Size();

            //For each character in the queue, try to move the player according to that character, throw exceptions if the player went out of bounds or ran into an obstacle, and check for a win.
            for (int i = 0; i < size; i++)
            {
                //Casting to char since Dequeue returns a nullable char
                nextMove = (char)moveSequence.Dequeue();

                switch (nextMove)
                {
                case 'w':
                    tempPlayerPos -= COLS;
                    break;

                case 's':
                    tempPlayerPos += COLS;
                    break;

                case 'a':
                    tempPlayerPos -= 1;
                    break;

                case 'd':
                    tempPlayerPos += 1;
                    break;

                default:
                    throw new ArgumentException("This sequence is invalid.");
                }

                //If the potential position is less than zero and greater than the maximum position, it's out of bounds.
                //If the player was on the left edge before being moved by nextMove and the user tried moving it further left, it's out of bounds
                //If the player was on the right edge before being moved by nextMove and the user tried moving it further right, it's out of bounds.
                if ((tempPlayerPos < 0 || tempPlayerPos >= ROWS * COLS) || ((tempPlayerPos + 1) % COLS == 0 && nextMove == 'a') || (tempPlayerPos % 5 == 0 && nextMove == 'd'))
                {
                    //Moving the  player piece completely off the screen to indicate that they went out of bounds
                    tempPlayerPos = -1;

                    throw new ArgumentException("This sequence of moves would make the player go out of bounds!");
                }

                //If the player's newly incremented position is equal to the position of the goal, they have won, so return true.
                if (tempPlayerPos == goalPos)
                {
                    return(true);
                }

                //For each obstacle, check if the player has touched it.
                for (int j = 0; j < obstacles.Length; j++)
                {
                    //If the player has touched the obstacle, make it visible and throw an exception.
                    if (tempPlayerPos == obstacles[j].GetPos())
                    {
                        obstacles[j].makeVisible();

                        throw new ArgumentException("You ran into an obstacle!");
                    }
                }
            }

            //If no win was found, return false.
            return(false);
        }