The game of maze. (10 points) In this question, you need to implement a maze gam
ID: 3714875 • Letter: T
Question
The game of maze. (10 points) In this question, you need to implement a maze game. The layout of the maze is given in the file “maze.txt”. In the file, ‘+’ suggests a block or wall that cannot pass. ‘.’ is a passable tile. ‘*’ is your start/current location and ‘E’ is the exit/goal.
(3 points) Read the maze layout from the text file and put it into a 2D array.
(2 points) Write a function to print the maze.
(3 points) Write a function that accepts a direction {‘u’,’d’,’l’,’r’} and moves the asterisk 1 tile toward that direction. The function should alert the user with “invalid move” if it runs into a wall and the asterisk should stay still. The function should also tell the user when the exit is reached.
d. (2 points) Write a loop to prompt the user to input a direction at each step until the exit/goal is reached.
Explanation / Answer
Let me know if you have any doubt.
The program made for 5 X 5 Maze :
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
using namespace std;
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();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.