Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Let the user try to move through a maze. The user will be presented with

ID: 3715298 • Letter: I

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. 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:

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 . . .

Explanation / Answer

The program made for 5 X 5 Maze :

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

*************************

#include<conio.h>
#include<fstream.h>
#include<iostream.h>

void print(char ch[5][5])
{
int i=0,j=0;
cout<<" ";
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cout<<ch[i][j];
}
cout<<" ";
}
}
void main() {
ifstream is("D:\Maze.txt");
char c;
char choice;
char ch[5][5];
int i=0,j=0;
clrscr();
if(!is)
{
cout<<"Can not open file";
}
while (is.get(c)) // loop while extraction from file is possible
{
if(c==' ')
{
//when new line change the row and make column to zero
i=i+1;
j=0;
}
else
{
ch[i][j] = c;
j=j+1;
}
}
is.close();
ch[1][0] = '*'; //starting position
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cout<<ch[i][j];
}
cout<<" ";
}
int curr_row = 1;
int curr_col = 0;
again:
int temp_cur_row = curr_row;
int temp_cur_col = curr_col;
if(ch[3][4] == '*')
{
goto win;
}
cout<<" Next Move (u= Up, d= Down, r= Right, l= Left, q=Quit) ?";
cin>>choice;
switch(choice)
{
case 'q':
case 'Q':
goto exit;

case 'u':
case 'U':
//row minus
curr_row = curr_row - 1;
if(curr_row<0 || curr_row > 4)
{
cout<<"Invalid move";
}
else if(ch[curr_row][curr_col]=='|')
{
cout<<"Invalid move";
curr_row = curr_row + 1;
}
else
{
ch[curr_row][curr_col] = '*';
ch[temp_cur_row][temp_cur_col] = ' ';
}
break;
case 'd':
case 'D':
//row plus
curr_row = curr_row + 1;
if(curr_row<0 || curr_row > 4)
{
cout<<"Invalid move";
}
else if(ch[curr_row][curr_col]=='|')
{
cout<<"Invalid move";
curr_row = curr_row - 1;
}
else
{
ch[curr_row][curr_col] = '*';
ch[temp_cur_row][temp_cur_col] = ' ';
}

break;
case 'r':
case 'R':
//col plus
curr_col = curr_col + 1;
if(curr_col<0 || curr_col > 4)
{
cout<<"Invalid move";
}
else if(ch[curr_row][curr_col]=='|')
{
cout<<"Invalid move";
curr_col = curr_col - 1;
}
else
{
ch[curr_row][curr_col] = '*';
ch[temp_cur_row][temp_cur_col] = ' ';
}
break;
case 'l':
case 'L':
//col minus
curr_col = curr_col - 1;
if(curr_col<0 || curr_col > 4)
{
cout<<"Invalid move";
}
else if(ch[curr_row][curr_col]=='|')
{
cout<<"Invalid move";
curr_col = curr_col + 1;
}
else
{
ch[curr_row][curr_col] = '*';
ch[temp_cur_row][temp_cur_col] = ' ';
}
break;
default:
cout<<"Invalid choice:";
}
print(ch);
goto again;
win:
cout<<"You win it";
exit:
getch();
}