10 Replies - 8022 Views - Last Post: 31 May 2010 - 12:14 AM Rate Topic: ***** 1 Votes

#1 Louis+Clovis=Lou_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-April 10

Maze Traversal Program

Posted 30 May 2010 - 07:40 AM

Hi, I'm trying to do my Maze Traversal Program.
Nonetheless, i think there is some error inside my codes although I can run it and then the program crash.

The first error i get is the name part and i think there is some more error in my codes since my program crash when key in the name for the player.

The program is written in C, not C++ or C#.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <windows.h>

#define NCOL 15
#define NROW 15

struct maze
{
	int num;
	char wall, path, start, exit, player;
};

void welcome() /*Welcome message*/
{
	char name[25];
	printf("***********Welcome to Louis's Maze Traversal***********\n\n");
	printf("Please enter your name: ");
	fflush(stdin);
	scanf("%c", &name[25]); /*something wrong with this part*/
	printf("\nGood days %c, let the game begin...good luck!!!\n\n", name);
}

void gen_maze(struct maze maz[NCOL][NROW]) /*Function that generating the random maze*/
{
	int a, b, c, d, e, f, g; /*Declaring of variables*/
	
	for(a = 0; a < NCOL; a++) /*This looping generates the random maze*/
	{
		for(b = 0; b < NROW; b++)
		{
			c=rand()%101;
			if(c<35)
			{
				maz[a][b].wall = '#';
				if(maz[a-1][b].wall == '#' && maz[a][b-1].wall == '#' && maz[a-1][b-1].wall == '#')
				{
					maz[a][b].path = '.';
				}
			}
			else
			{
				maz[a][b].path = '.';
				
				if (maz[a-1][b].path == '.' && maz[a][b-1].path == '.' && maz[a-1][b-1].path == '.')
				{
					maz[a][b].wall = '#';
				}
			}
		}
	}
	
	for(d = 0; d < NCOL; d++) /*This looping assign the boarder as a wall*/
	{
		for(e = 0; e < NROW; e++)
		{	
			if(d == 0 || d == 14 || e == 0 || e == 14)
			{
				maz[d][e].wall = '#';
			}
		}
	}
	
	f = rand()%10;
	g = rand()%10;
	
	maz[0][f+1].start = '.'; /*shows the starting point of the maze*/
	maz[11][g+1].exit = '.'; /*shows the exit of the maze*/
}

void print_maze(struct maze maz[NCOL][NROW]) /*Function that give the output of the randomly generated maze*/
{
	int x, y, a, b;
	
	for(x = 0; x < NCOL; x++)
	{
		for(y = 0; y < NROW; y++)
		{
			if(maz[b][a].wall == '#')
			{
				printf("# ");
			}
			else
			{
				if(maz[b][a].path == '.')
				{
					printf(". ");
				}
				else
				{
					if(maz[b][a].start == 'X')
					{
						printf(" ");
					}
					else
					{
						if(maz[b][a].exit == 'E')
						{
							printf("E ");
						}
						else
						{
							printf("@ ");
						}
					}
				}
			}
		}
	}
	printf("\n\n");
}	 	 	 

void move(struct maze maz[NCOL][NROW]) /*Movement of the player inside the maze*/
{
	char option;
	int a, b, c, d, e, f, g, h;
	
	for(a = 0; a < NCOL; a++) /*The detection of starting point*/
	{
		if(maz[0][a].wall == '.')
		{
			b = a;
		}
	}
	
	for(c = 0; c < NROW; c++) /*The detection of the exit*/
	{
		if(maz[14][c].wall == '.')
		{
			d = c;
		}
	}
	
	for(e = 0; e < NCOL; e++) /*The detection of the player*/
	{
		for(f = 0; f < NROW; f++)
		{
			if(maz[e][f].player == 'X')
			{
				g = e; h = f;
			}
		}
	}
	
	maz[0][b].player = 'X'; /*player as X*/
	
	do /*selection of your move*/
	{
		print_maze(maz);
		
		printf("\nOptions:\nW togo up\nS to go down\nA to go left\nD to go right\nQ to quit\nPlease select: \n\a");
		fflush(stdin);
		scanf("%i", &option);
		
		switch(option)
		{
			case 'W': case 'w': /*go up*/
			{
				if(maz[g][h - 1].path == '.')
				{
					maz[g][h - 1].player = 'X';
					maz[g][h].path = '.';
				}
				else
				{
					maz[g][h].player = 'X';
				}
				Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100);
				break;
			}
			
			case 'S': case 's': /*go down*/
			{
				if(maz[g][h + 1].path == '.')
				{
					maz[g][h + 1].player = 'X';
					maz[g][h].path = '.';
				}
				else
				{
					maz[g][h].player = 'X';
				}
				Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100);
				break;
			}
			
			case 'A': case 'a': /*go left*/
			{
				if(maz[g - 1][h].path == '.')
				{
					maz[g - 1][h].player = 'X';
					maz[g][h].path = '.';
				}
				else
				{
					maz[g][h].player = 'X';
				}
				Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100);
				break;
			}
			
			case 'D': case 'd': /*go right*/
			{
				if(maz[g + 1][h].path == '.')
				{
					maz[g + 1 ][h].player = 'X';
					maz[g][h].path = '.';
				}
				else
				{
					maz[g][h].player = 'X';
				}
				Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100);
				break;
			}
			
			case 'Q': case 'q': /*to quit*/
			{
				printf("\nGameover\a\a\a\a\a\a\a\a\a\nDo you want to try it again?");;
				Continue();
				Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100);
				break;
			}
			
			default:
			{
				maz[g][h].player = 'X';
				printf("\nThere is no such option\n");
				Continue();
				Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100);
			}
		}
	}while(option != 'Q' || option != 'q');
}


int Continue(void) /*decide want to continue of quit the game*/
{
	char choice;
	
	printf("Continue? (Y/N)");
	choice = getch();
	
	return choice == 'Y' || choice == 'y';
}

int main() /*The main function of the Maze Traversal Programme*/
{
	struct maze m[NCOL][NROW];
	char name;
	int a, b;
	srand((unsigned)time(NULL));
	
	Beep(450, 100); Beep(900, 100); Beep (250,100); Beep(300, 100); Beep(550,100); /*Beep sound produced*/
	
	welcome();
	gen_maze(m);
	print_maze(m);
	move(m);
	Continue();
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Maze Traversal Program

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Maze Traversal Program

Posted 30 May 2010 - 08:08 AM

In print_maze function, you have this:
int x, y, a, b;
        
for(x = 0; x < NCOL; x++)
{
    for(y = 0; y < NROW; y++)
    {
         if(maz[b][a].wall == '#')
         {
              printf("# ");
         }

// etc ... more


As you can see, a and b are two uninitialized variables, yet you try to use them here:
if(maz[b][a].wall == '#')


Would maz[b][a] be a valid element, since b and a contain nothing?
Was This Post Helpful? 0
  • +
  • -

#3 Louis+Clovis=Lou_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-April 10

Re: Maze Traversal Program

Posted 30 May 2010 - 08:21 AM

View Postsarmanu, on 30 May 2010 - 07:08 AM, said:

In print_maze function, you have this:
int x, y, a, b;
        
for(x = 0; x < NCOL; x++)
{
    for(y = 0; y < NROW; y++)
    {
         if(maz[b][a].wall == '#')
         {
              printf("# ");
         }

// etc ... more


As you can see, a and b are two uninitialized variables, yet you try to use them here:
if(maz[b][a].wall == '#')


Would maz[b][a] be a valid element, since b and a contain nothing?


sorry about that, i forgot to check, x suppose to be a and y suppose to be b.

but yet the same problem is at
void welcome() /*Welcome message*/
{
        char name[25];
        printf("***********Welcome to Louis's Maze Traversal***********\n\n");
        printf("Please enter your name: ");
        fflush(stdin);
        scanf("%c", &name[25]); /*something wrong with this part*/
        printf("\nGood days %c, let the game begin...good luck!!!\n\n", name);
}


i want to key in my name, Louis, but the output is L only, how to make the output to be Louis?
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Maze Traversal Program

Posted 30 May 2010 - 08:27 AM

Ok, first of all, DO NOT use fflush() to flush the input buffer. Read here for more details. A good alternative would be this:
char c;
// Simply eats all remaining characters.
while (( c = getchar()) != EOF && c != '\n');



Now, the second problem is here:
scanf("%c", &name[25]); /*something wrong with this part*/


should be:
scanf("%s", name);


More details here.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10366
  • View blog
  • Posts: 38,403
  • Joined: 27-December 08

Re: Maze Traversal Program

Posted 30 May 2010 - 08:30 AM

Also, just a thought, but if you organize your maze as a Graph structure instead of a 2D array, you will have an easier time Traversing it, as you can access the next Vertices from the exisitng Vertex's Edges. For this task, you might find recursion to be helpful.

I've written a Graphs Tutorial in Java which you may find helpful. Different language, but the same concept.
Was This Post Helpful? 0
  • +
  • -

#6 Louis+Clovis=Lou_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-April 10

Re: Maze Traversal Program

Posted 30 May 2010 - 05:37 PM

View PostLouis+Clovis=Lou_is, on 30 May 2010 - 07:21 AM, said:

View Postsarmanu, on 30 May 2010 - 07:08 AM, said:

In print_maze function, you have this:
int x, y, a, b;
        
for(x = 0; x < NCOL; x++)
{
    for(y = 0; y < NROW; y++)
    {
         if(maz[b][a].wall == '#')
         {
              printf("# ");
         }

// etc ... more


As you can see, a and b are two uninitialized variables, yet you try to use them here:
if(maz[b][a].wall == '#')


Would maz[b][a] be a valid element, since b and a contain nothing?


sorry about that, i forgot to check, x suppose to be a and y suppose to be b.

but yet the same problem is at
void welcome() /*Welcome message*/
{
        char name[25];
        printf("***********Welcome to Louis's Maze Traversal***********\n\n");
        printf("Please enter your name: ");
        fflush(stdin);
        scanf("%c", &name[25]); /*something wrong with this part*/
        printf("\nGood days %c, let the game begin...good luck!!!\n\n", name);
}


i want to key in my name, Louis, but the output is L only, how to make the output to be Louis?


@sarmanu like this??
void welcome() /*Welcome message*/
{
	char name[25]; /*Declaring of variables*/
	
	printf("***********Welcome to Louis's Maze Traversal***********\n\n");
	printf("Please enter your name: ");
	scanf("%s", name);
	printf("\nGood days %s, let the game begin...good luck!!!\n\n", name);
}


@macosxnerd101 i only learn array 2d -basic thing i learn, didn't learn graph structure

This post has been edited by Louis+Clovis=Lou_is: 30 May 2010 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Maze Traversal Program

Posted 30 May 2010 - 10:35 PM

Yes, that is correct. Do you still have problems?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10366
  • View blog
  • Posts: 38,403
  • Joined: 27-December 08

Re: Maze Traversal Program

Posted 30 May 2010 - 10:44 PM

View PostLouis+Clovis=Lou_is, on 30 May 2010 - 08:37 PM, said:

@macosxnerd101 i only learn array 2d -basic thing i learn, didn't learn graph structure

Probably better to stick to 2D array then. Graphs are definitely more complex to design than creating a 2D array. However, once you have a working Graph structure, designing the maze and solving the problem will be a lot easier. Once you get to Data Structures and Discrete Math, you will get more Graph theory, which has lots of applications in these types of problems.
Was This Post Helpful? 0
  • +
  • -

#9 Louis+Clovis=Lou_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-April 10

Re: Maze Traversal Program

Posted 30 May 2010 - 11:37 PM

View Postsarmanu, on 30 May 2010 - 09:35 PM, said:

Yes, that is correct. Do you still have problems?

void gen_maze(struct maze maz[NCOL][NROW]) /*Function that generating the random maze*/
{
	int a, b, c, d, e, f, g; /*Declaring of variables*/
	srand((unsigned)time(NULL));
	
	for(a = 0; a < NCOL; a++) /*This looping generates the random maze*/
	{
		for(b = 0; b < NROW; b++)
		{
			c=rand()%101;
			if(c<30)
			{
				maz[a][b].wall = '#';
				if(maz[a-1][b].wall == '#' && maz[a][b-1].wall == '#' && maz[a-1][b-1].wall == '#')
				{
					maz[a][b].path = '.';
				}
			}
			else
			{
				maz[a][b].path = '.';
				
				if (maz[a-1][b].path == '.' && maz[a][b-1].path == '.' && maz[a-1][b-1].path == '.')
				{
					maz[a][b].wall = '#';
				}
			}
		}
	}
	
	for(d = 0; d < NCOL; d++) /*This looping assign the boarder as a wall*/
	{
		for(e = 0; e < NROW; e++)
		{	
			if(d == 0 || d == 14 || e == 0 || e == 14)
			{
				maz[d][e].wall = '#';
			}
		}
	}
	f = rand()%13;
	maz[0][f+1].start = '.'; /*shows the starting point of the maze*/
	g = rand()%13;
	maz[14][g+1].exit = '.'; /*shows the exit of the maze*/
}


can help to check whether this part is correct?
it is because when i wanted to generate my maze, it give my program a crash.

View Postmacosxnerd101, on 30 May 2010 - 09:44 PM, said:

View PostLouis+Clovis=Lou_is, on 30 May 2010 - 08:37 PM, said:

@macosxnerd101 i only learn array 2d -basic thing i learn, didn't learn graph structure

Probably better to stick to 2D array then. Graphs are definitely more complex to design than creating a 2D array. However, once you have a working Graph structure, designing the maze and solving the problem will be a lot easier. Once you get to Data Structures and Discrete Math, you will get more Graph theory, which has lots of applications in these types of problems.


Yeah, but for now i only learn 2d array, struct, loop, random number, etc, all very basic command for c language.
I think next time i will learn that.
Was This Post Helpful? 0
  • +
  • -

#10 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Maze Traversal Program

Posted 30 May 2010 - 11:43 PM

if(maz[a-1][b].wall == '#' && maz[a][b-1].wall == '#' && maz[a-1][b-1].wall == '#')


What happens if a or b (or both) are 0? maz[a-1] would be [b]maz[-1][b]. Is -1 a valid subscript in your 2D array?
Was This Post Helpful? 0
  • +
  • -

#11 Louis+Clovis=Lou_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-April 10

Re: Maze Traversal Program

Posted 31 May 2010 - 12:14 AM

void gen_maze(struct maze maz[NCOL][NROW]) /*Function that generating the random maze*/
{
	int a, b, c, d, e, f, g; /*Declaring of variables*/
	srand((unsigned)time(NULL));
	c=rand()%101;
	
	for(a = 0; a < NCOL; a++) /*This looping generates the random maze*/
	{
		for(b = 0; b < NROW; b++)
		{
			
			if(c<30)
			{
				maz[a][b].wall = '#';
			}
			else
			{
				maz[a][b].path = '.';
			}
		}
	}
	
	for(d = 0; d < NCOL; d++) /*This looping assign the boarder as a wall*/
	{
		for(e = 0; e < NROW; e++)
		{	
			if(d == 0 || d == 14 || e == 0 || e == 14)
			{
				maz[d][e].wall = '#';
			}
		}
	}
	f = rand()%13;
	maz[0][f+1].start = '.'; /*shows the starting point of the maze*/
	g = rand()%13;
	maz[14][g+1].exit = '.'; /*shows the exit of the maze*/
}


now i'm correct?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1