Question: In C++ Let the user try to move through a maze. The user will be presented with a maze, which is read from a file.

In C++

Let the user try to move through a maze.

The user will be presented with a maze, which is read from a file. The maze has twice as many columns as rows. You should use constants. There are two test files provided, small_maze.txt and maze.txt. small_maze.txt has only 4 rows and 8 columns. maze.txt has 10 rows and 20 columns. While you are writing your maze program and testing, I recommend using small_maze.txt. When you feel like your program is working, update your constants, rebuild your program, and you should be able to use the larger maze.txt file. That is the file I will be testing with, but in the beginning it can be tedious to have to traverse that large of a maze when you are still debugging the program.

The user always starts at the second row, first column. The user can move up, down, left, or right. If the user tries to move in a direction where there is a wall, tell the user that the move is illegal. The user wins the game when they make it to the exit of the maze which is always the next to last row and last column. Basically, the user has to move from the upper left to the lower right.

At any time the user can choose to quit the game.

MazePlay.cpp will contain main()

Maze.cpp and Maze.h will contain the Maze class. Recommendations for the class:

Constants for the size of the two-dimensional array dimensions
Member variables for the maze (two-dimensional array), current x and y position
Constructor to initialize member variables
read method that accepts a filename. It should read the file into your two-dimensional array
print method that prints the current picture of the maze
get and set methods for x and y
legalMove method that accepts an x and y. It returns true or false if the user can make that move
won method returns true or false if the user has won the game

Sample Run #1: (the highlighted text is what the user types) user wins the game

Maze Filename? maze.txt

||||||||||||||||||||

* ||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? r

||||||||||||||||||||

* ||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? r

||||||||||||||||||||

* ||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? r

||||||||||||||||||||

* ||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? r

||||||||||||||||||||

*||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? d

****** SEVERAL MORE MOVES ******

||||||||||||||||||||

||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | ||| *

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? r

||||||||||||||||||||

||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | ||| *

||||||||||||||||||||

Congratulations! You won!

Press any key to continue . . .

Sample Run #2: (the highlighted text is what the user types) user loses the game and does illegal move

Maze Filename? maze.txt

||||||||||||||||||||

* ||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? d

That move is illegal -- try again

||||||||||||||||||||

* ||||||| ||

|||| ||| ||| ||||

||||| || ||||

| |||||| ||

| ||||||||| | ||

| ||| ||||| |

|||| || || |||

|||||| | |||

||||||||||||||||||||

Next move (u=Up, d=Down, r=Right, l=Left, q=Quit)? q

Sorry, you lost

Press any key to continue . . .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!