Sorry, it's not bug free. Here is a solution.
Sorry, Daniel
I have tried to read your idea.. I mean code .. may that one is also a right way.. but I felt better to try this way..
The code is not bug free just found the logic is working....
I'm sure that you have to fix errors...
here with comments ..
Code:
/*************************************/
/*mazebyz.h on 20th Jan 2002*/
/* Part of magebyz.c*/
/* I found that this is the core part should be clear*/
/*************************************/
int get_next_move(char maze[][12],int row, int col,
int direction, int *n_row, int *n_col, int *n_direction)
{
switch(direction) /* 4 possible directions*/
{
case 0: /*when your direction is West*/
if(is_a_valid_cell(maze, row+1, col)==YES) /*first possible move*/
{
*n_row=row+1;
*n_col=col;
*n_direction=(direction-1+NO_OF_D)%NO_OF_D; /*Your direction changed*/
return YES;
}
/* if left cell is not valid*/
else if(is_a_valid_cell(maze, row,col-1)==YES) /*second possible move*/
{
*n_row=row;
*n_col=col-1;
*n_direction=direction; /*same direction*/
return YES;
}
/* if left cell & front cell is not valid*/
else if(is_a_valid_cell(maze, row-1, col)==YES)/*third possible move*/
{
*n_row=row-1;
*n_col=col;
*n_direction=(direction+1)%NO_OF_D; /*Your direction changed*/
return YES;
}
/* if left cell & front & right cell is not valid*/
/* turn back*/
else if(is_a_valid_cell(maze, row,col+1)==YES)/*fourth and last possible move*/
{
*n_row=row;
*n_col=col+1;
*n_direction=(direction+2)%NO_OF_D; /*Your direction changed*/
return YES;
}
break;
case 1: /*when your direction is North*/
if(is_a_valid_cell(maze, row, col-1)==YES)
{
*n_row=row;
*n_col=col-1;
*n_direction=(direction-1+NO_OF_D)%NO_OF_D;
return YES;
}
else if(is_a_valid_cell(maze, row-1,col)==YES)
{
*n_row=row-1;
*n_col=col;
*n_direction=direction;
return YES;
}
else if(is_a_valid_cell(maze, row, col+1)==YES)
{
*n_row=row;
*n_col=col+1;
*n_direction=(direction+1)%NO_OF_D;
return YES;
}
else if(is_a_valid_cell(maze, row+1,col)==YES)
{
*n_row=row+1;
*n_col=col;
*n_direction=(direction+2)%NO_OF_D;
return YES;
}
break;
case 2: /*when your direction is East*/
if(is_a_valid_cell(maze, row-1,col)==YES)
{
*n_row=row-1;
*n_col=col;
*n_direction=(direction-1+NO_OF_D)%NO_OF_D;
return YES;
}
else if(is_a_valid_cell(maze, row, col+1)==YES)
{
*n_row=row;
*n_col=col+1;
*n_direction=direction;
return YES;
}
else if(is_a_valid_cell(maze, row+1,col)==YES)
{
*n_row=row+1;
*n_col=col;
*n_direction=(direction+1)%NO_OF_D;
return YES;
}
else if(is_a_valid_cell(maze, row, col-1)==YES)
{
*n_row=row;
*n_col=col-1;
*n_direction=(direction+2)%NO_OF_D;
return YES;
}
break;
case 3: /*when your direction is South*/
if(is_a_valid_cell(maze, row, col+1)==YES)
{
*n_row=row;
*n_col=col+1;
*n_direction=(direction-1+NO_OF_D)%NO_OF_D;
return YES;
}
else if(is_a_valid_cell(maze, row+1,col)==YES)
{
*n_row=row+1;
*n_col=col;
*n_direction=direction;
return YES;
}
else if(is_a_valid_cell(maze, row, col-1)==YES)
{
*n_row=row;
*n_col=col-1;
*n_direction=(direction+1)%NO_OF_D;
return YES;
}
else if(is_a_valid_cell(maze, row-1,col)==YES)
{
*n_row=row-1;
*n_col=col;
*n_direction=(direction+2)%NO_OF_D;
return YES;
}
break;
}
return NO;
}
/*************************************/
/*mazebyz.c on 20th Jan 2002*/
/*************************************/
#include <stdio.h>
#define YES 0
#define NO 1
#define SUCCESS 0
#define NO_OF_D 4
#define ST_POINT 2
/*
Cell address:
00------>11
|
|
|
|
v
11
Directions:
01 N
^
|
W 00<- x ->02 E
|
v
03 S
*/
int position[3]={2,0,2}; /*row 2, col 1, north 2*/
static int starting_flag= 0;
void printMaze( char maze[][ 12 ] );
int check_gameover(int, int , int);
#include "mazebyz.h"
int main()
{
int row, col, direction;
/* 0 1 2 3 4 5 6 7 8 9 10 11 */
char maze[ 12 ][ 12 ] =
{ '1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1', /* 0 */
'1', '0', '0', '0', '0', '0', '0', '0', '0','0', '0', '1', /* 1 */
'0', '0', '1', '0', '1', '0', '1', '1', '1','1', '0', '1', /* 2 */
'1', '1', '1', '0', '0', '0', '0', '0', '0','1', '0', '1', /* 3 */
'0', '0', '0', '0', '0', '1', '1', '1', '0','1', '0', '1', /* 4 */
'1', '1', '1', '1', '0', '1', '0', '0', '0','1', '0', '1', /* 5 */
'1', '0', '0', '1', '0', '1', '0', '1', '0','1', '0', '1', /* 6 */
'1', '1', '0', '1', '0', '1', '0', '1', '0','1', '0', '1', /* 7 */
'1', '0', '0', '0', '0', '0', '0', '0', '0','1', '0', '1', /* 8 */
'1', '0', '1', '1', '1', '1', '0', '1', '0','1', '0', '1', /* 9 */
'1', '0', '0', '0', '0', '0', '0', '1', '0','0', '0', '1', /* 10 */
'1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1' };/* 11 */
row=2;
col=0;
direction=2;
//printMaze(maze);
//printf("\n%d %d %d\n",row, col, direction);
printf("Cell address:\n\n");
printf(" 00------>11\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" v\n");
printf(" 11\n\n");
printf("\nDirections:\n");
printf(" 01 N\n");
printf(" ^\n");
printf(" |\n");
printf("W 00<- x ->02 E\n");
printf(" |\n");
printf(" v\n");
printf(" 03 S\n");
/*Recursive function call*/
mazeTraverse( maze, row, col, direction );
//printMaze(maze);
return 0;
}
/* here is the recursive function to walk through the maze*/
int mazeTraverse( char maze[12][ 12 ], int row, int col, int direction )
{
int stat;
int n_row, n_col, n_direction; /* Move: next position*/
stat=check_gameover(row, col, direction); /* checkes if game is over*/
if(stat==ST_POINT && starting_flag==1) /*1 is to avoid the first move*/
{
printf("\nReturned to the starting point.\n");
printf("No other exit through this entry.\n");
return ST_POINT;
}
if(starting_flag==0) starting_flag=1; /*To mark that first move done*/
if(stat==YES) /*When check_gameover(); returns yes.*/
{
printf("\nExit point found!!!\n");
printf("Now, you are free.\n");
return YES;
}
if(maze[row][col]=='0')
maze[row][col]='x'; /*mark first time foot step*/
else if(maze[row][col]=='x')
maze[row][col]='*'; /*mark second time foot step*/
printMaze(maze); /*display your position in the maze*/
/*find the qualified next pisition*/
get_next_move(maze, row, col, direction, &n_row, &n_col, &n_direction);
/*Testing: display next move:*/
printf("Move: r=%2.2d, c=%2.2d, d=%2.2d\n",n_row, n_col, n_direction);
/*Recursion*/
mazeTraverse( maze, n_row, n_col, n_direction );
return YES;
}
/*Compare the starting position with current position*/
int is_starting(row,col,direction)
{
if(row!=position[0])
return NO;
else if(col!=position[1])
return NO;
else return YES;
}
/* Checkes if a position is qualified*/
/*Not out of border & not a wall*/
int is_a_valid_cell(char maze[][12],int row, int col)
{
if( row < 0 || row > 11)
return NO;
else if( col < 0 || col > 11)
return NO;
else if(maze[row][col]!='1') return YES;
}
/*checks if you can see the outside world*/
/*check exit point*/
int is_at_border(int row, int col)
{
if(row==0||col==0)
return YES;
if(row==11||col==11)
return YES;
else return NO;
}
/*checks the position: exit point or entry position*/
int check_gameover(int row, int col, int direction)
{
if (is_starting(row,col,direction)==YES)
return ST_POINT;
if(is_at_border(row, col)==YES)
return YES;
else return NO;
}
/*displays the your position in the maze*/
void printMaze( char maze[][ 12 ] )
{
int i, j;
printf( "\n\n" );
for ( i = 0; i < 12; i++ )
for ( j = 0; j < 12; j++ ) {
printf( "%c", maze[ i ][ j ] );
if ( j == 11 )
printf( "\n" );
}
}
/*made little change: when no exit point found*/