i've working with these program, it's a multidimensional array program of a maze, it's called Herman, as a game..
i have initialized the array, and start the position, but it is not working properly, it stops when it hits the position before, when there are other positions available..it is suppose to stop when there are no more exits.
here's the code, hope somebody can help me with this.
Code:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define BLANK ' '
#define MAX 12
#define NORTH 1
#define EAST 2
#define SOUTH 3
#define WEST 4
int main(void)
{
char maze[MAX][MAX];
bool north,south,east,west;
int direction;
int row;
int col;
char ch = 48;
srand(time(NULL));
// 1. intialize array
for(row = 0;row < MAX; row++)
{
for(col = 0;col < MAX; col++)
{
if(row == 0 || row == (MAX - 1))
{
maze[row][col] = (ch + col);
}
else if(col == 0 || col == (MAX - 1))
{
maze[row][col] = (ch + row);
}
else
{
maze[row][col] = BLANK;
}
}//end col loop
}//end row loop
// 2. Start Position
row = rand()%10 + 1;
col = rand()%10 + 1;
printf("STARTING POSITION\n");
printf("ROW %d \n",row);
printf("COLUMN %d \n",col);
// 3.setting Loop
while(north,east,south,west)
{
direction = rand()%4+1;
if(direction == 1)
{//1
if(maze[row][col] == BLANK)
{
maze[row][col] = 'N';
row = row - 1;
north = true; east = true; south = true; west = true;
}
else
north = false;
}//1
else if(direction == 2)
{//2
if(maze[row][col] == BLANK)
{
maze[row][col] = 'E';
col = col + 1;
north = true; east = true; south = true; west = true;
}
else
east = false;
}//2
else if(direction == 3)
{//3
if(maze[row][col] == BLANK)
{
maze[row][col] = 'S';
row = row + 1;
north = true; east = true; south = true; west = true;
}
else
south = false;
}//3
else
{//4
if(maze[row][col] == BLANK)
{
maze[row][col] = 'W';
col = col - 1;
north = true; east = true; south = true; west = true;
}
else
west = false;
}//4
}//while
for(row = 0;row < MAX; row++)
{
for(col = 0;col < MAX; col++)
printf(" %c",maze[row][col]);
printf("\n\n");
}
printf("\n");
printf("\n");
return 0;
}
thanks for your help
Juan