#include <stdio.h>
#define NORTH 1
#define EAST 2
#define SOUTH 3
#define WEST 4
#define MAZESIZE 41
char maze[MAZESIZE][MAZESIZE] ={
{"_________________________________________"},
{"S>| | | | | |"},
{"| | +---+ +---- | +-- | +-+ | ------+ | |"},
{"| | | | | | | | | | | |"},
{"| |-- | | | +---+-+ +-----+ |-----+ +---|"},
{"| | | | | | | | | |"},
{"| | +-----+ | | | +---+ | +-----+ | +-+ |"},
{"| | | | | | | | | |"},
{"|---+ +---+-+ +---+ | --+ +---+ +-----+ |"},
{"| | | | | | | | | |"},
{"|---- | | | --+ | | +-----+ --+-- | ----|"},
{"| | | | | | | |"},
{"| +---+ |-- +---+ | | +---- | +---+-- | |"},
{"| | | | | | | | | | |"},
{"| +-+ | | --+ | +-+-+-+-----+---+ +---+ |"},
{"| | | | | | | | |"},
{"|-----+-+-+ +---+ | | --+---+-+ | | +-+ |"},
{"| | | | | | | | | | |"},
{"|---- | +---+-----+-- | | | | +---| | | |"},
{"| | | | | | | | | |"},
{"| --+-+ +-+-+ --+-+ +-+---+-+ +-+ +-+-+ |"},
{"| | | | | | | | | |"},
{"|-- | +-- | +-+ + +-+ +-+ | | | +---+---|"},
{"| | | | | | | | | | | | |"},
{"| +---+ +-+ | | | | +-+ +---+ | +-+ | | |"},
{"| | | | | | | | | | | | |"},
{"| | ----+---+ | +-+ | | +-- | +-----+-+ |"},
{"| | | | | | | | |"},
{"| +-+-- | | +-+-+ +-+-+-- +-+ | | ------|"},
{"| | | | | | | | |"},
{"|-+ | | +-+-- +---+ | +-+-+ | | | +---+ |"},
{"| | | | | | | | | | | | |"},
{"| +---+ | +---+ --+ --+ | +-+ | | | | | |"},
{"| | | | | | | | | | | | |"},
{"| | +---+ +-- +-+ +-+ +---+-----+ | | | |"},
{"| | | | | | | | | | | |"},
{"| +-+ | | | +-+ +-- | | +-----+ | | +---|"},
{"| | | | | | | | | | | | | |"},
{"| | --+---+ | +-----+ | | +-- +-+ | | | |"},
{"| | | | | | | F"},
{"+---------------------------------------+"}
};
int currentdirection=2; /* N=1, E=2, S=3, W=4 */
int currentrow=1;
int currentcolumn=1;
int WIN=0;
char P=' ';
int iterations;
void displayWorld()
{
for( int i=0;i<MAZESIZE; i++ )
{
for( int j=0;j<MAZESIZE; j++ )
{
printf( "%c", maze[i][j] );
}
printf( "\n" );
}
printf( "\n\n\n");
}
int moveFwd()
{
if( checkWin()==100)
{
printf( "\n\n\n*** YOU WIN!!! ***\n");
printf( "in iterations: %d\n\n\n", iterations );
WIN=1;
exit(0);
}
else if( wallInFrontOfMe()==1)
{
printf( "[ERROR]\n" );
printf( "[cd: %d\n]", currentdirection );
return(-1);
}
else
{
maze[currentrow][currentcolumn]=' ';
switch( currentdirection )
{
case NORTH:
currentrow--;
P='^';
break;
case EAST:
currentcolumn++;
P='>';
break;
case SOUTH:
currentrow++;
P='v';
break;
case WEST:
currentcolumn--;
P='<';
break;
}
maze[currentrow][currentcolumn]=P;
return(1);
}
return(1);
}
void turnRight()
{
if( ++currentdirection == 5 ) currentdirection=NORTH;
}
void turnLeft()
{
if( --currentdirection == 0 ) currentdirection=WEST;
}
int checkWin()
{
if( maze[currentrow-1][currentcolumn] == 'F' ) return 100;
if( maze[currentrow][currentcolumn+1] == 'F' ) return 100;
if( maze[currentrow+1][currentcolumn] == 'F' ) return 100;
if( maze[currentrow][currentcolumn-1] == 'F' ) return 100;
return(0);
}
int wallInFrontOfMe()
{
int row=currentrow;
int col=currentcolumn;
int direction=currentdirection;
switch( direction )
{
case NORTH:
if(( maze[row-1][col] == ' ' ) || (maze[row-1][col] == '.' ))return(0);
break;
case EAST:
if(( maze[row][col+1] == ' ' ) || (maze[row][col+1] == '.' ))return(0);
break;
case SOUTH:
if(( maze[row+1][col] == ' ' ) || (maze[row+1][col] == '.' ))return(0);
break;
case WEST:
if(( maze[row][col-1] == ' ' ) || (maze[row][col-1] == '.' ))return(0);
break;
default:
return(1);
}
return(1);
}
int main()
{
int startingrow=1;
int startingcolumn=1;
int startingdirection=EAST;
displayWorld();
iterations=0;
char input[1];
while( WIN==0 && iterations<=10000)
{
/* --------------------------------------------------------------------- */
/* Commands: */
/* if( wallInFrontOfMe() == 0 ) means "If there's no wall in front of me */
/* if( wallInFrontOfMe() == 1 ) means "If there is a wall in front of me */
/* moveFwd(); moves the * forwards one space */
/* turnRight(); turns the * to the right */
/* turnLeft(); turns the * to the left */
/* --------------------------------------------------------------------- */
/* PUT YOUR MAZE SOLVING CODE BELOW THIS LINE AND.... */
if( wallInFrontOfMe() == 1 )
{
turnRight();
if( wallInFrontOfMe()==1 )
{
turnRight();
turnRight();
if( wallInFrontOfMe()==1 )
{
turnLeft();
moveFwd();
}
else
{
moveFwd();
}
}
else
{
moveFwd();
}
}
else
{
turnRight();
if( wallInFrontOfMe()==1 )
{
turnLeft();
moveFwd();
}
else
{
moveFwd();
}
}
/* .... ABOVE THIS LINE */
/* -------------------------------------------------------------------- */
displayWorld();
printf( "press enter or CTRL-C to end\n");
scanf( "%c",&input);
iterations++;
}
displayWorld();
return(0);
}