Question: Write a C++ program will let the user try to move through a maze. The user will be presented with a maze, which is read

Write a C++ program will let the user try to move through a maze. The user will be presented with a maze, which is read from a file maze.txt has 10 rows and 20 columns.

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.

One way of reading the file is to read it one character at a time. Some methods of reading skip over spaces and newline characters. If you have an input stream called, inFile, and you want to read the next character (space or not), you can use the lines:

char c;

inFile.get(c);

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!