Your assignment is to write a maze traverser. The input will be the parameters o
ID: 3606534 • Letter: Y
Question
Your assignment is to write a maze traverser. The input will be the parameters of a rectangular maze followed by a character representation of the maze itself. The output of your program will be the maze with a path from entry point to exit point You can design your own class for the maze and any other classes you may need 2 Program Specification Your maze traverser will accept parameters and a rectangular maze through standard input. The first two parameters will be the number of rows and columns in the maze. The next two parameters will be the row and column of the entry point. The last two parameters wil be the row and column of the exit point. The parameters will be followed by the character representation of the maze Your program will draw a path from entry point to exit point The maze will be a rectangular grid of blocks and floor panels. A block l be a (hashtag), a floor panel will be a blank. The walls of the maze will be constructed from blocks and the floor from floor panels. Your traverser can move freely from floor panel to adjacent floor panel, but it will not be possible to walk through the walls. You can drop a marker on any floor panel to show a step in you path. A marker can be any character except a block or a floor panel. You can also pick up a marker and replace it with a floor pane You can touch the walls, but you cannot alter the walls, so don't even think about. The walls are too high to jump over, so that option is out as wl. No digging under the walls, as there are giant carnivorous moles down there. 3 Maze Traverser In a real-life maze, you could enter the maze, put one hand on the w left or right) and start walking. As long as you never stop touching the wall with that hand, you will make your way to the exit. Your algorithmic maze traverser can be right- or left-handed. The right-handed traverser always wants to turn to the right, the left-handed traverser always wants to turn to the left It would be good to decide on a convention for directions in the maze. I went with North, South, East and West for a rectangular maze. You traverser will have to know the row and colum of its current location as well as the direction the traverser is facing/goingExplanation / Answer
Hi,
Cpp code for above required maze program will be:
#include<stdio.h>
#include<stdlib.h>
//main-function
int mazeTraverse( char mazePtr[ 12 ][ 12 ], int startRow, int startColumn, int endRow, int endColumn );
void printMaze( char maze[ 12 ][ 12 ], const int startRow, const int startColumn, int endRow, int endColumn );
int main( void ){
int startRow =2, startColumn = 0, state = 0, endrow= 4, endColumn = 11;
// 0 1 2 3 4 5 6 7 8 9 10 11
char maze[ 12 ][ 12 ] = {{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, // 0
{'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'}, // 1
{'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'}, // 2
{'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'}, // 3
{'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'}, // 4
{'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 5
{'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 6
{'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 7
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'}, // 8
{'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'}, // 9
{'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'}, // 10
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };// 11
printf("The maze ******** ");
printMaze( maze, 12, 12 );
system("pause");
system("cls");
state = mazeTraverse( maze, startRow, startColumn, endRow, endColumn );
if(state == 0){
printf(" Player found the exit! ");
}
system("pause");
return 0;
}
//global
int startingFlag = 1, direction = 0;
//enumeration
enum Stat { OVER, NOT_OVER };
/*core function*/
int mazeTraverse( char maze[ 12 ][ 12 ], int startRow, int startColumn, int endRow, int endColumn ){
//sub-function
int gameOver( const int startRow, const int startColumn );
int move( char maze[ 12 ][ 12 ], int *startRow, int *startColumn, int *currentDirection );
enum Stat State;
//check if game is over
State = gameOver( startRow, startColumn );
if(State == OVER && startingFlag == 0){
printMaze( maze, currentRow, currentColumn );
return OVER; //return OVER indicating succesfully find the exit
}
//indicate player is ready to move
if(startingFlag == 1){
//print initial maze
printf("Player's ready ************** ");
printMaze( maze, startRow, startColumn );
system("pause");
system("cls");
//set first direction based on starting position
if(currentRow == 0){
direction = 1;
}else if(currentRow == 11){
direction = 0;
}else if(currentColumn == 0){
direction = 2;
}else if(currentColumn == 11){
direction == 3;
}
startingFlag = 0;
}
//seek for next move
move( maze, ¤tRow, ¤tColumn, &direction );
//system("pause");//activate this to see each move
system("cls");
mazeTraverse( maze, startRow, startColumn );
return OVER;
}
/*seek for next move*/
enum Direction { NORTH, SOUTH, EAST, WEST };
int move( char maze[ 12 ][ 12 ], int *startRow, int *startColumn, int *currentDirection ){
int posibble[ 4 ] = { 0 };// 1 -> North; 2 -> South; 3 -> East; 4 -> West;
int counter = 0;
enum Direction Seek;
Seek = *currentDirection;
/*move the player respect to current direction*/
//cover the current position
maze[ *currentRow ][ *currentColumn ] = '.';
//move the player respect to current direction
if(Seek == NORTH){
//print direction
printf("direction = NORTH ");
//move north
*currentRow -= 1;
}else if(Seek == SOUTH){
//print direction
printf("direction = SOUTH ");
//move south
*currentRow +=1;
}else if(Seek == EAST){
//print direction
printf("direction = EAST ");
//move east
*currentColumn += 1;
}else if(Seek == WEST){
//print direction
printf("direction = WEST ");
//move west
*currentColumn -= 1;
}
//print each move
printMaze( maze, *startRow, *startColumn );// print maze with player current position
/*analyse for next direction*/
//seek posibble direction
printf("Seek next direction... ");
if(maze[ *startRow - 1 ][ *startColumn ] == '.' && Seek != SOUTH){
printf("NORTH is possible ");
posibble[ 0 ] = 1;
counter++;
}
if(maze[ *startRow + 1 ][ *startColumn ] == '.' && Seek != NORTH){
printf("SOUTH is possible ");
posibble[ 1 ] = 1;
counter++;
}
if(maze[ *startRow ][ *startColumn + 1 ] == '.' && Seek != WEST){
printf("EAST is possible ");
posibble[ 2 ] = 1;
counter++;
}
if(maze[ *startRow ][ *startColumn - 1 ] == '.' && Seek != EAST){
printf("WEST is possible ");
posibble[ 3 ] = 1;
counter++;
}
printf(" ");
//follow right wall
//Direction { NORTH, SOUTH, EAST, WEST };
if(counter == 1){
if(posibble[ 1 ] == 1){//south
*currentDirection = 1;
}else if(posibble[ 2 ] == 1){//east
*currentDirection = 2;
}else if(posibble[ 0 ] == 1){//north
*currentDirection = 0;
}else if(posibble[ 3 ] == 1){//west
*currentDirection = 3;
}
}else if(counter == 2){
if(posibble[ 2 ] == 1 && posibble[ 3 ] == 1){// posibble: EAST, WEST
if(Seek == SOUTH){
*currentDirection = 3;
}else if(Seek == NORTH){
*currentDirection = 2;
}
}else if(posibble[ 0 ] == 1 && posibble[ 1 ] == 1){// posibble: NORTH,SOUTH
if(Seek == EAST){
*currentDirection = 1;
}else if(Seek == WEST){
*currentDirection = 0;
}
}else if(posibble[ 0 ] == 1 && posibble[ 3 ] == 1){// NORTHWEST
*currentDirection = 0;
}else if(posibble[ 0 ] == 1 && posibble[ 2 ] == 1){// NORTHEAST
*currentDirection = 2;
}else if(posibble[ 1 ] == 1 && posibble[ 2 ] == 1){// SOUTHEAST
*currentDirection = 1;
}else if(posibble[ 1 ] == 1 && posibble[ 3 ] == 1){// SOUTHWEST
*currentDirection = 3;
}
}else if(counter == 3){
if(Seek == NORTH){
*currentDirection = 2;
}else if(Seek == SOUTH){
*currentDirection = 3;
}else if(Seek == EAST){
*currentDirection = 1;
}else if(Seek == WEST){
*currentDirection = 0;
}
}else if(counter == 0){
//dead end
if(Seek == NORTH){
*currentDirection = 1;
}else if(Seek == SOUTH){
*currentDirection = 0;
}else if(Seek == EAST){
*currentDirection = 3;
}else if(Seek == WEST){
*currentDirection = 2;
}
}
}
/*check if game is over*/
int gameOver( const int startRow, const int startColumn, int endRow, int endColumn ){
if(startRow == endRow && startColumn == endColumn ){
return OVER;
}else{
return NOT_OVER;
}
}
/*print current maze*/
void printMaze( char maze[ 12 ][ 12 ], const int startRow, const int startColumn, int endRow, int endColumn){
int mazeRow, mazeColumn;
printf(" ");
for(mazeRow = 0; mazeRow < 12; mazeRow++){
for(mazeColumn = 0; mazeColumn < 12; mazeColumn++){
if(mazeRow == startRow && mazeColumn == startColumn){
maze[ mazeRow ][ mazeColumn ] = 'X';
}
printf("%2c", maze[ mazeRow ][ mazeColumn ] );
}
printf(" ");
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.