Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random Maze Program

Started by BugTilaheH Nov 24, 2001 at 10:06 PM 32 replies 4.3k views
Original Post
BugTilaheH
BugTilaheH
Argh, for a lab assignment, I have to write a program that randomly generates a maze, and a random starting position. The program then needs to find a path out of the maze, if it''s possible, to exit the maze. The functions and their implementations given are Rnd(...), CreateMaze(...) and DisplayMaze(...). Then the rest of the functions SolveMaze(...), MazeSolution(...), and GetMove(...) have to be implemented by me. So far, this is what the source looks like (below). I''m not asking anyone to write it for me ''cos this is for a grade and all. I just need help. I need help particularly in the function GetMove(). I don''t know why, but whenever we have an assignment that asks to get a move, I can''t do it! It''s so frustrating. Anyway, here''s the source. If any one could give me any pointers or hints on what I could do (such as a helpful algorithm), it would help out tremendously. If you need the source for the include files, just ask, if they''re needed for you to understand what I''m doing here. If you were to compile this and run it, you''d get an illegal vector index 12, max index = 11 (basically, I went out of range, which is baaaad), but only if you used APMATRIX.H (it provides out-of-range protection by giving you an error, instead of crashing your system). Also, I''ve looked everywhere on the internet for help, and I can''t find any sites that are helpful at all. Hopefully you guys could help, a little at least? Oh, btw, in the maze, you can go any direction including diagonally, so that makes 8.
  
#include "UTILITY2.H"
#include "APMATRIX.H"
#include "APSTACK.H"

struct Coordinate
{
	int Row, Col;
};

double Rnd(double &Seed)
{
	const double PI = 3.14159;
	Seed = (Seed + PI) * (Seed + PI);
	Seed = Seed - floor(Seed);
	return Seed;
}

typedef apmatrix <char> Matrix;

void CreateMaze(Matrix &Maze, Coordinate &Start);
void DisplayMaze(const Matrix &Maze);
void SolveMaze(Matrix &Maze, Coordinate Start, bool &Free);
void MazeSolution(bool Free);
void GetMove(const Matrix &Maze, Coordinate Current, Coordinate &Move, bool &Free);

void main()
{
	OutputSelection();
	Heading H("35A","11-12-01","100","RANDOM MAZE");
	Execution();
	Matrix Maze(12,12);
	Coordinate Start;
	bool Free;
	do
	{
		clrscr();
		CreateMaze(Maze, Start);
		DisplayMaze(Maze);
		SolveMaze(Maze,Start,Free);
		DisplayMaze(Maze);
		MazeSolution(Free);
	}
	while (NotFinished());
	Terminate();
}

void CreateMaze(Matrix &Maze, Coordinate &Start)
{
	double Seed;
	cout << "ENTER A RANDOM SEED ===>>  ";
	cin >> Seed;
	int R,C,L;
	for (R = 0; R < 12; R++)
		for (C = 0; C < 12; C++)
		{
		 if (R == 0 || C == 0 || R == 11 || C == 11)
			 Maze[R][C] = ''X'';
		 else
		 {
			 L = floor(2 * Rnd(Seed));
			 if (L == 0)
				 Maze[R][C] = ''X'';
			 else
				 Maze[R][C] = ''O'';
		 }
		}

	Maze[0][0] = ''E'';
	Start.Row = floor(11 * Rnd(Seed)) + 1;
	Start.Col = 11;
	Maze[Start.Row][Start.Col] = ''S'';
}

void DisplayMaze(const Matrix &Maze)
{
	Title("DISPLAY MAZE");
	int R, C;
	for (R = 0; R < 12; R++)
	{
		for (C = 0; C < 12; C++)
		 cout << Maze[R][C] << '' '';
		cout << endl;
	}
	getch();
}

void SolveMaze(Matrix &Maze, Coordinate Start, bool &Free)
{
	Title("SOLVE MAZE");
	int R, C;
	Free=false;
	apstack <Coordinate> S;
	S.makeEmpty();
	Coordinate Current,Move;
	Current = Start;
	S.push(Start);
	while (!(S.isEmpty() && Maze[Start.Row][Start.Col]==''S''))
	{
	GetMove(Maze,Current,Move,Free);
	if (Free==true)
			{
			Maze[Current.Row][Current.Col]= ''.'';
			S.push(Current);
			}
	else
	S.pop(Current);
	}
}

void MazeSolution(bool Free)
{
	Title("MAKE SOLUTION");
	if (Free==true)
	cout << "There is a solution to the maze" << endl;
	else
	cout << "There is no solution to the maze" << endl;
}

void GetMove(const Matrix &Maze, Coordinate Current, Coordinate &Move, bool &Free)
{
	apstack <Coordinate> S;
	/////////

	if (Maze[Current.Row][Current.Col]==''S'' && Maze[10][10]==''O'')  //lower left

	{
		S.push(Current);
		Free=true;
	}
	else if (Maze[Current.Row][Current.Col]==''S'' && Maze[9][10]==''O'') //upper left

	{
		S.push(Current);
		Free=true;
	}
	/*else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row][Current.Col-1]==''O'') // up
	{
		Maze[Move.Row][Move.Col-1];
		S.push(Current);
		//Current.Col--;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col-1]==''O'') // upper right
	{
		Maze[Move.Row+1][Move.Col-1];
		S.push(Current);
		//Current.Row++;
		//Current.Col--;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col]==''O'') // right
	{
		Maze[Move.Row+1][Move.Col];
		S.push(Current);
		//Current.Row++;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col+1]==''O'') // lower right
	{
		Maze[Move.Row+1][Move.Col+1];
		S.push(Current);
		//Current.Row++;
		//Current.Col++;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row][Current.Col+1]==''O'') // down
	{
		Maze[Move.Row][Move.Col+1];
		S.push(Current);
		//Current.Col++;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col+1]==''O'') // lower left
	{
		Maze[Move.Row-1][Move.Col+1];
		S.push(Current);
		//Current.Row--;
		//Current.Col++;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col]==''O'') // left
	{
		Maze[Move.Row-1][Move.Col];
		S.push(Current);
		//Current.Row--;
		Free=true;
	}
	else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col-1]==''O'') // upper left
	{
		Maze[Move.Row-1][Move.Col-1];
		S.push(Current);
		//Current.Row--;
		//Current.Col--;
		Free=true;
	}*/
	else
	{
		Free=false;
		//S.pop(Current);

	}
	//if (Maze[Current.Row][Current.Col]==''S'')

	//{Free = true;

	//S.push(Current);

	//Move = Current;

}
  
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
BugTilaheH
BugTilaheH
Oh, I see.. you people help everyone but me ''cos I need help on an assignment? Thanks. I feel so loved. {END SARCASM}

I''ve looked everywhere.. no one will help. Fine fine..

-Bug
Website: http://www.thepageofstuff.com
Message board: http://pub51.ezboard.com/bpageofstuff
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
AdmiralBinary
AdmiralBinary
My reply was a mistake. It was not intended to be offensive, and I apologize for my thoughtless words.

P.S. I'll make a note never again to be on crack while posting on gamedev.net

---------------

I finally got it all together...
...and then forgot where I put it.

Edited by - AdmiralBinary on November 26, 2001 1:26:07 AM

Edited by - AdmiralBinary on November 26, 2001 1:28:16 AM
BugTilaheH
BugTilaheH
... nobody''s laughing. Could you help instead of make fun of me? What? You hate me ''cos I''m a "n00b" and my code "sux0rs"?

If the above didn''t help, then what I''m trying to do is go through a 12x12 matrix "maze" that randomly displays X''s for the "walls" and O''s for the "open path". You start at S and continue by pushing coordinates on a stack and moving until you reach the E. If you get stuck, you pop from the stack and try a different route.

-Bug
Website: The Page of Stuff.com
Message board: POS.COM Forum
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
AdmiralBinary
AdmiralBinary
Dood, I''m sorry. I didn''t mean to make fun of u. Umm...I assumed it wasn''t u that wrote it. I''m sorta newbie too actually. Please accept my most heartfelt apologies.

P.S. I can''t see anything actually wrong with your solution. What part are you having trouble with? And what''s GetMove s''posed to do anyway???

---------------

I finally got it all together...
...and then forgot where I put it.
AdmiralBinary
AdmiralBinary
A peace offering. This may not help you, but it''s some code I used to make squares visible within the range of a unit on my strategy game. Basically, it radiates outwards from the start position until it reaches the range of the unit''s sight. I think the same principle could be applied to your maze.

P.S. No comments, very messy...nothing like as neat as your code, Bug.

GetAdj() - Get''s adjacent square in a certain direction.
CTerrin - A tile.

  
viewrange = chassis.viewrange;
loc->visible[side] = true;
list<CTerrain*> l_check;
list<CTerrain*> l_next;
list<CTerrain*>::iterator index;
l_next.push_back(loc);
for (int i = 0; i < viewrange; i++)
{
l_check.clear();
l_check = l_next;
l_next.clear();
for (index = l_check.begin(); index != l_check.end(); ++index)
{
for (int i = 0; i < 8; i++)
{
if (g_map.GetAdj((*index)->x,(*index)->y,(FACING)i))
{
g_map.GetAdj((*index)->x,(*index)->y,(FACING)i)->visible[side] = true;
l_next.push_back(g_map.GetAdj((*index)->x,(*index)->y,(FACING)i));
}
}
}
}


SORRYYYYYYY!!!!

---------------

I finally got it all together...
...and then forgot where I put it.
Oluseyi
Oluseyi
Bug, search the forums. There was a thread on mazes recently and you should find all the answers to your forum.

quote:
Original post by Anonymous Poster
(3) Always taking a right (or always a left, your choice) turn will get you out of a maze if there is an exit.

Provided there is no loop in the maze. A better solution to mazes is a modified Depth/breadth first search that keeps track of the path (I forget exactly how we implemented it in Advanced Data Structures...)
BugTilaheH
BugTilaheH
It''s ok. I was just wondering why someone would just attack me out of the blue ''cos my code was such a mess, and I know it is.. (and yes, I wrote the implementations for SolveMaze(), MazeSolution(), and GetMove()). I''ll try again by putting the code here again and commenting where I think it''s necessary.

OK, to answer some questions I remember:
GetMove does the following, well.. I''ll just write what the sheet says:
"Function GetMove is called by function SolveMaze and determines the next move of the maze solution.

The secret of this program is the manner in which you use the stack. You solve the maze by pushing current coordinate positions on the stack, provided moves are possible. If you cannot move then you must pop your stack and try previous positions.

Your program needs to keep track of positions that are not possibilities. The letter O indicates a path. The period also indicates a path, but a position that has been visited. From the program''s point of view, a period has the same meaning as a wall. The program solution must keep going until a path has been found, meaning that the exit E is now converted to a period. It is entirely possible that the exit is blocked and in such a case the program terminates as well. When that happens every possible path position will be converted to periods.

Function CreateMaze creates a maze with a solid wall on four sides. The [0,0] is an opening in the left top corner, which is the maze''s exit. The wall on the right side has one opening, which is randomly selected to be the start (S) of the maze.

  
#include "UTILITY2.H" //fluff code that has nothing to do with the program but makes it "prettier"
#include "APMATRIX.H" //header file for matrix implementations/math/routines, etc. Uses a templated class
#include "APSTACK.H" //same as above but for stacks


struct Coordinate //keeps track of the coordinates I guess, I didn''t write this

{
int Row, Col;
};

double Rnd(double &Seed) // gets a number from user and uses it to get random seed

{
const double PI = 3.14159;
Seed = (Seed + PI) * (Seed + PI);
Seed = Seed - floor(Seed);
return Seed;
}

typedef apmatrix <char> Matrix; //defines Matrix array as type char


void CreateMaze(Matrix &Maze, Coordinate &Start);
void DisplayMaze(const Matrix &Maze);
void SolveMaze(Matrix &Maze, Coordinate Start, bool &Free);
void MazeSolution(bool Free);
void GetMove(const Matrix &Maze, Coordinate Current, Coordinate &Move, bool &Free);

void main()
{
OutputSelection(); //ignore this, fluff code

Heading H("35A","11-12-01","100","RANDOM MAZE"); //ditto

Execution(); //ditto

Matrix Maze(12,12); //initializes Maze as a 12x12 matrix

Coordinate Start;
bool Free;
do
{
clrscr(); //clear screen

CreateMaze(Maze, Start);
DisplayMaze(Maze);
SolveMaze(Maze,Start,Free);
DisplayMaze(Maze);
MazeSolution(Free);
}
while (NotFinished()); //kinda obvious

Terminate(); //more fluff

}

void CreateMaze(Matrix &Maze, Coordinate &Start) //creates maze, I didn''t write this

{
double Seed;
cout << "ENTER A RANDOM SEED ===>> ";
cin >> Seed;
int R,C,L;
for (R = 0; R < 12; R++)
for (C = 0; C < 12; C++)
{
if (R == 0 || C == 0 || R == 11 || C == 11)
Maze[R][C] = ''X'';
else
{
L = floor(2 * Rnd(Seed));
if (L == 0)
Maze[R][C] = ''X'';
else
Maze[R][C] = ''O'';
}
}

Maze[0][0] = ''E'';
Start.Row = floor(11 * Rnd(Seed)) + 1;
Start.Col = 11;
Maze[Start.Row][Start.Col] = ''S'';
}

void DisplayMaze(const Matrix &Maze) //displays the maze, didn''t write this either

{
Title("DISPLAY MAZE");
int R, C;
for (R = 0; R < 12; R++)
{
for (C = 0; C < 12; C++)
cout << Maze[R][C] << '' '';
cout << endl;
}
getch();
}

void SolveMaze(Matrix &Maze, Coordinate Start, bool &Free)
{
Title("SOLVE MAZE"); //fluff

int R, C;
Free=false; //not sure if it should be set true or false?

apstack <Coordinate> S; //defines S as a stack, not sure what the type should be but Coordinate seems to work fine

S.makeEmpty(); //makes the stack empty

Coordinate Current,Move; //not sure if Move should be of type Coordinate

Current = Start;
S.push(Start); //pushes Start on the stack

while (!(S.isEmpty() && Maze[Start.Row][Start.Col]==''S'')) //while the stack is not empty and the maze is not at start

{
GetMove(Maze,Current,Move,Free); //getmove call

if (Free==true) //if true, put a dot at current location and push current coordinates on the stack

{
Maze[Current.Row][Current.Col]= ''.'';
S.push(Current);
}
else // else pop the current coordinats off the stack

S.pop(Current);
}
}

void MazeSolution(bool Free)
{
Title("MAKE SOLUTION");
if (Free==true) //self-explantory

cout << "There is a solution to the maze" << endl;
else
cout << "There is no solution to the maze" << endl;
}

void GetMove(const Matrix &Maze, Coordinate Current, Coordinate &Move, bool &Free)
{
apstack <Coordinate> S; //?

if (Maze[Current.Row][Current.Col]==''S'' && Maze[10][10]==''O'') //lower left but isn''t doing anything

{
S.push(Current); //?

Free=true; //?

}
else if (Maze[Current.Row][Current.Col]==''S'' && Maze[9][10]==''O'') //upper left

{
S.push(Current);
Free=true;
}
/*else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row][Current.Col-1]==''O'') // up
{
Maze[Move.Row][Move.Col-1]; //i''m so lost..
S.push(Current);
//Current.Col--;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col-1]==''O'') // upper right
{
Maze[Move.Row+1][Move.Col-1];
S.push(Current);
//Current.Row++;
//Current.Col--;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col]==''O'') // right
{
Maze[Move.Row+1][Move.Col];
S.push(Current);
//Current.Row++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col+1]==''O'') // lower right
{
Maze[Move.Row+1][Move.Col+1];
S.push(Current);
//Current.Row++;
//Current.Col++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row][Current.Col+1]==''O'') // down
{
Maze[Move.Row][Move.Col+1];
S.push(Current);
//Current.Col++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col+1]==''O'') // lower left
{
Maze[Move.Row-1][Move.Col+1];
S.push(Current);
//Current.Row--;
//Current.Col++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col]==''O'') // left
{
Maze[Move.Row-1][Move.Col];
S.push(Current);
//Current.Row--;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col-1]==''O'') // upper left
{
Maze[Move.Row-1][Move.Col-1];
S.push(Current);
//Current.Row--;
//Current.Col--;
Free=true;
}*/

else
{
Free=false;
//S.pop(Current);

}
//if (Maze[Current.Row][Current.Col]==''S'')

//{Free = true;

//S.push(Current);

//Move = Current; //what do I do with Move??

}


I think at this point, the only error I''m getting is "can''t pop an empty stack", and if you were to uncomment all the if else''s, you''d get illegal vector index 12, max index=11 (went out of range).

Hope this helps ..


-Bug
Website: The Page of Stuff.com
Message board: POS.COM Forum
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
BugTilaheH
BugTilaheH
I''m already ahead of both of you. I checked out gametutorials.com and I asked on their forums, I''ve also seen everything you''ve mentioned, and none of it has helped ''cos they aren''t doing the same thing I am. I might use the first-breadth algorithm or whatever it''s called, but then again I dunno. I''m not going to use recursion, btw.
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
BugTilaheH
BugTilaheH
Holy cow! I got it to work! Well.. sorta..

I had the loop like this:
while (!(S.isEmpty() && Maze[Start.Row][Start.Col]==''S''))
All I had to do was take off ==''S'' and it stopped giving me the error "Can''t pop an empty stack", so now I think it''s working.. I wonder if I uncomment the rest of the code it''ll work then? *switches back to compiler to try* I''ll edit this post if it works. Sorry for "spamming".. too many replies from me. :3


-Bug
Website: The Page of Stuff.com
Message board: POS.COM Forum
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
Seriema
Seriema
dude!!.. damnit!... I spent 1 hour writing this tutorial for ya! So you BETTER READ IT!! When I tried to post it, it messed up! So... READ IT!! *GRRRRR*


What fun!!

I've done almost the exact thing as an assignment!! But we didn't have to make a random-map-generator

OK, here's two ways to solve it. The first one is the "dumb" one, called "backtracking". The second one finds the shortest path, called the Greedy-algorithm. I'll try to explain them as good as i can!!


¤ ¤ GENERAL TIP TO APPLY ON BACKTRACK AND OTHER IDEAS ¤ ¤
Input a new variable in your Coordinate struct (or what you use for your map). Preferably a bool, named was or been_here or whatever! Set it to false as default (when you create the map). Then when you Push something to the map, don't forget to pull the flag on the map with that coordinate to "true".

When you "walk around". ALWAYS check for:
* Is it a wall?
* Have I been here before?

The reason for this is that you don't end up walking back to a place you visited before and might end up in an endless loop!


¤ ¤ BACKTRACKING v. 1 ¤ ¤
You could do as the person before me said, always take a right. If it's a wal, try straight ahead. If that's a wall to, take left. If all failed, POP When you find "finish". Then exit the damn loop and your stack is the path!!


¤ ¤ BACKTRACKING v. 2 ¤ ¤
I made a little smarter than that *hehe* It assumes you have the coordinates for the "finish". Ask your teacher if you can store that coordinate in a variable (the program should have a function or just copy the coordinates when you create the finish )

Let me see if I can explain it without paper and pen =) You make a small array of your Coordinate struct, of 4 elements (one for each direction). Call it test_path (or something). You store the x,y position for North in element 0, East in 1, etc ONE step away from your current position. It would help if your struct had a float variable called vector or v. Then a small function to calculate the vector.

Vector = sqrt(x*x + y*y);

BUT!! Wait! Before you calc the vector you need the right x and y! Meaning the vector from your position to the finish. And not to the 0,0 coordinate. If you don't get it. Draw it on a paper What you need to do first is take the x from the test_path position and - it with the finish x. And the same with the y, of course =)

x = x_test - x_finish;

Now, done! Don't worry if the x is negative, if you check the vector formula it raises it by x^2 (x*x) so the negative becomes positive. Kinda nifty huh??

You will need to calc the vector for each test_path elements. Then when that's done you need to sort the array. So that the shortest path from the testing position to the finish is element 0, etc.

Do you know how to sort? If you don't... There are alot of ways!! Just loop through it twice, checking the elements next to each other (for example elements 0 and 1, or 1 and 2, etc). The biggest should be on the "right", if it isn't switch those two! (can be done with a simple swap function). Maybe not the most efficient way, but it's simple

NOW what? Well... You have to go trough that array of course! Checking for wall and "been here". If not wall or been here, then Push this coord into that stack and don't forget to turn the flag on the map to "been here" = true.

Easy huh? *ironic* This will sometimes be the shortest path! But not always! But it is alot smarter than version 1, if you want to impress on your teacher If you didn't get it, ask me and I'll explain further


¤ ¤ GREEDY-ALGORITHM ¤ ¤
This one is a tricky one! But it is kinda simple when you understand it The main idea is to count every step/tile/element (or what you want to call one move in your map) from the starting position to the finish position. You'll understand it if you bring out a piece of cross-ruled paper (you know, not lines but tiled?) and start drawing!! Draw a start position, just write an S in one square. And an F (finish) in a square about 5 squares down and 10 right. You could draw a wall somewhere here to further understand the algorithm. Now, around the S write the number 2 (start should be 1) in the four squares you can move on (North, East, South, West). Around each 2 write a 3 (but don't overwrite a wall or previosly "counted" square) in the same way, North, East, etc. Guess what's next? Yup, you guessed it! Write a 4 around those 3's. And continue untill you found the goal (or filled up the paper ). What you should have is a map full of numbers.

What you could do now is either use the backtrackin method to walk around on those numbers (always walking to a higher number, unless stuck then pop). But let's do it the cool way! If you take a red penn (or whatever) and start drawing FROM the goal TO the start (on the paper that is, not on the screen ) ALWAYS choosing a square with ONE lower number than the one you'r standing on. Example, finish is on square 23. Look for 22 (Norht, East, etc.) and go there, then 21, and so on. Now how does it look? "Isn't that the shortest path?!", you scream in exhaltation. Well yes, it is!!

So guess? Well your program/function should do the exact same thing! *marvelous* Your Coordinate struct should have ONE more member *wee* Called a "count" (or whatever) of an int type. Set these as 0 as default.

Start at "start", put the number 1 there. Then raise that number by 1, meaning... 2 and writing it on the four places around that 2. Don't forget to check for walls or previously counted squares (count != 0)!! I tried to do this with a binary tree but... The _easiest_ (trust me, not smartest but easiest) way to find that "1" is simply to run through your maze from 0,0 to x,y looking for that nr 1 and then writing 2 around it. After that, continue searching for nr 1 (yes, nr 1) until you reach x,y. THEN raise that search nr to 2 and write 3's around them. Get it? *I hope so* Because as you see on your paper, there will be alot of squares with the same number. And maybe not even in a simple way, if you did one with a wall (the nr 10 might apear 10 steps to the right of start AND 2 steps right above, because of a wall).

Pseudo-code (to help you out):
    
loop(number=0 -> infinity, finish_found != true)
{
loop(square.x=0 -> x)
loop(square.y=0 -> y) // yupp, TWO loops!! nestled...

{
if(square == finish) //hurray!!

finish_found = true;

if(square.count == number)
{
if(square_north != wall && square_north.count != 0)
square_north.count = number+1; //counts

// and the same for east, south and west (copy-paste the code ;))

else
if(square_east != wall && square_east.count != 0)
square_east.count = number+1;
else
if(square_south != wall && square_south.count != 0)
square_south.count = number+1;
else
if(square_west != wall && square_west.count != 0)
square_west.count = number+1;
}
}
}


Hope you understood that?? Now all you have to do is pick up that red pen again! Just loop from the finish position, looking for a square with count-1 from your position and Push that into the stack (don't forget to Push the finish position first!). Until you find the start position, Push that one too and stop!

THERE!! Now all you have to do is Pop your stack to watch that little guy running to the goal like if he had a map and a compass!

This should impress the pants of your proffesor!!

GOOD LUCK!!!! And tell me how it went!

[Edit] Found an error in my pseudo code *not good* And oh yeah! I'm thinking of starting to write some tutorials for Gamedev. Probably C++ newbie and algorithm, maybe even design patterns. What do you guys think of my writing style? Of course my tutorials would have images

}+TITANIUM+{

Edited by - Seriema on November 25, 2001 8:39:54 PM
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
BugTilaheH
BugTilaheH
Hmm.. that might help. I forgot to show some of my notes.. in GetMove, it''s supposed to do the following:
-Free=true; (?)
-a big struct (Huh? I guess I''m supposed to create a new struct of my own? I''ll use Seriema''s ideas, then)
-find available move
-copy & paste (how do you do that???)

I think the backtracking algorithm is what I''m supposed to do. The whole point of this is to find an O, push its coordinates on the stack, and keep going until you reach the end, and then pop to show the path.

More pseudo-code (I think for SolveMaze(...)) that I was given, which I''ve already tried to code (for the most part):
  
Current=Start; //Got that..

push(Start); (or current) // got that..

while !empty and !at start // got that

{
Call GetMove(...); //ok

if (Free)
{
current move //don''t have that..

Maze[..][..]=''.''; //got that I guess

push(current); // got that..

}
}
pop(current);


Random notes:
-test for finish (E) at front of struct (which struct???)
-moved assigned a value (??)
-check, then push, if no go, pop (duh)

Anyway, I''ll try out your code, Seriema. [] Thanks for the help everyone!!!

-Bug
Website: The Page of Stuff.com
Message board: POS.COM Forum
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
Seriema
Seriema
I''m gonna try to add you on ICQ (mine is 8778467) Since I didn''t get your problem really Instead of clogging up this forutm, I thought we''d talk in real-time

About what you said in the beginning of your last post:
"-Free=true; (?)"
ehm... yes?.. If I underand you correctly... what I''m meant was that your coordinate struct should have a bool member to remember if you''ve been on that square before or not. Without having to change the map info (0 or 1) itself, thus keeping that info so you can redraw the screen or whatever. And you should always check to see if you''ve been there before. Just think of a guy running in a circle, he''s not touching any walls is he? But is he getting somewhere?.. that''s why

"-a big struct (Huh? I guess I''m supposed to create a new struct of my own? I''ll use Seriema''s ideas, then)"
Yep! Let me see if I remember all the details.
  
Coordinate struct:
¤ int x
¤ int y
¤ bool been_here
¤ char info (or int if you wish, for the wall/nonwall)
¤ int count (if you''d do that Greed-algorithm version)
¤ float v (the length of the vector from here to finish, if you use my altered version of Backtracking)


"-find available move"
Yes? Check North, East, South, West to see if you can walk there (nonwall and not been there before).


"-copy & paste (how do you do that???)"
Hehe... I meant the code There are 4 almost identical if-statements. The only difference between them is North->East->South->West. So that was just a stupid tip to copy the first if-statement and paste it 3 more times and altering it. Just forget it..

Good luck!

}+TITANIUM+{
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
BugTilaheH
BugTilaheH
No, I meant that''s my notes on paper, for what GetMove is supposed to do, it was not a response to your post.

Anyway, I have ICQ but I don''t use it anymore (it''s really old)! Do you have AIM? If so, my SN is CMikeska. I''ll try to get on ICQ anyway.. oh, number is 10036539 if I remember correctly.

-Bug
Website: The Page of Stuff.com
Message board: POS.COM Forum
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/
Seriema
Seriema
Oh, OK! *haha* Thought it was strange questions

Sorry, don''t have AIM. But I actually have MSN and use it more often than I use ICQ. If you''re getting one of them, get MSN Mine is: kabooomm@hotmail.com


Good luck!

}+TITANIUM+{
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
Seriema
Seriema
Oh, OK! *haha* Thought it was strange questions

Sorry, don''t have AIM. But I actually have MSN and use it more often than I use ICQ. If you''re getting one of them, get MSN Mine is: kabooomm@hotmail.com


Good luck!

}+TITANIUM+{
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
BugTilaheH
BugTilaheH
Ugh.. I'm so giving up on this. Maybe my teacher will extend the time 'cause I don't think anyone figured it out yet, heh.. the same happened for the Knight's Tour program, god that was hard.. I never did figure that thing out either.

Oh well, some last things that might help you understand what I'm trying to do.

Here's a sample execution of the random maze (unsolved, random seed=0):

EXXXXXXXXXXX
XOXXOOXOOXXX
XOXOXOXOXOXX
XOOOXXOOOXXX
XXXXOOOXXOXX
XXOOXXOOXOOX
XOOOXOOXOXXX
XOOOXXXXOXXX
XXOXXXOXXOOX
XOXXOOXXOXOX
XXOXOOOOOOXS
XXXXXXXXXXXX

E=End, S=Start, X=Wall, O=Open path. You can go in any direction, including diagonally.
The above maze solved (I'm assuming this would be the quickest path):

.XXXXXXXXXXX
X.XXOOXOOXXX
X.XOXOXOXOXX
X...XXOOOXXX
XXXX...XXOXX
XXOOXXO.XOOX
XOOOXOOX.XXX
XOOOXXXX.XXX
XXOXXXOXX..X
XOXXOOXXOX.X
XXOXOOOOOOXS
XXXXXXXXXXXX

Now, if only I could figure out how to do that while using the functions given (look at my first post for the implementations of the given functions). The following were given, with full implementations:
double Rnd(...);
void CreateMaze(...);
void DisplayMaze(...);

My job is to implement the following functions:
void SolveMaze(...);
void MazeSolution(...);
void GetMove(...);
*Note: All code where ... would be was also given.
So, basically, the whole program was given to me, all I have to do is implement those 3 functions.

Does that clear up any confusion for anyone trying to help?
Also, GetMove(...) was explained somewhere above in my other posts (including notes on what I'm supposed to code in it), so if you don't get what it's supposed to do, read up.

Thank y'all for helping! I wish I could figure this thing out.. *scratches head*

[EDIT]- Trying to line up the output.. evil font..

-Bug
Website: The Page of Stuff.com
Message board: POS.COM Forum

Edited by - BugTilaheH on November 25, 2001 12:35:39 AM
-Bug/********************************** Website: The Page of Stuff.COM ** Message board: POS.COM Forum   **********************************/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.