help with MAZE

This is a discussion on help with MAZE within the C Programming forums, part of the General Programming Boards category; i've working with these program, it's a multidimensional array program of a maze, it's called Herman, as a game.. i ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    help with MAZE

    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

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    First off, you should be compiling this as a .c source file not a .cpp source file. If you did, you'd know that bool isn't a valid datatype in C.

    Second, you should ask yourself what this really means:

    Code:
    while(north,east,south,west)
    ...and why it's hindering your map building.
    Sent from my iPadŽ

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    First off, you should be compiling this as a .c source file not a .cpp source file. If you did, you'd know that bool isn't a valid datatype in C.
    It is in C99. (Actually, it's a macro, but that's beside the point. ) Just be sure to include <stdbool.h>


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Instead of a large main(), create 3 functions
    initialiseMaze(char maze[MAX][MAX]);
    fillMaze(char maze[MAX][MAX]);
    printMaze(char maze[MAX][MAX]);

    Then tell us which one isn't working properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 12:52 AM
  2. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM
  3. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM
  4. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM
  5. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 02:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21