Thread: Maze Project

  1. #16
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    you're going to need something...

    well here's your problem:

    Your mouse has to be able to look in four directions doesn't it? So you have to look and see where it can fo. Then you have to see:
    Code:
    nextstep[x][y]=maze[w][l]
    
    //then in  a loop:
    
    if (nextstep[x][y+1]==open) 
      nextstep[x][y]=nexstep[x][y+]
    I'm not sure that's the best way to do it. But that's one way...it need additions and more work but...a start.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  2. #17
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Code:
    char nextstep;
    			char xpos = 30;
    
    			do
    			{
    				for (m = xpos; m >= 1; m--)
    				{
    					if (maze[w][l] == 'x')
    						nextstep = false;
    					else
    					if (maze[w][l] == 'o')
    						nextstep == m--;
    
    					cout << m << endl;
    				}
    			}while ( m != 0);
    Ok what im thinking is that this code is going to calculate the nextmove of m. Tell me if you think this works. Now all im trying to do at this point is get it to move if the nextsquare is an 'o' and not move if the next square is 'x'. The loop will tell it to keep going until it reaches 0. What i need to do now to make it work with the maze is to tell it where 'M' is. How can i do that? I know I can't use 30. Any suggestions?

  3. #18
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Ok kermi. Now you got me thinking. I didnt know i could use nextstep like nextstep[x][y]. Now i know what your saying. But one question still haunts me. How do i set up M? I mean i have to give a position and my teacher is going to use differant mazes for this program. any suggestions?

  4. #19
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Ok i understand you dont want to spend your sunday night helping me, but Iv really got to get this done. So here is a really stupid question. exactly what does the x and y mean in the array? What are they standing for? I think the x and y is the differant sides of the mouse...or its so the size of the maze can be stored in it. Is that any where close? And can you answer my other question above?

  5. #20
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Code:
    void demo()
    	{
    
    		int l, w;
    		char border = 0;
    		char open = 1;
    
    		for (w = 0; w < 20; ++w)
    		{
    			for (l = 0; l < 20; ++l)
    			{
    				InMaze >> maze[w][l];
    				if (maze[w][l] == 'x')
    					maze[w][l] = border;
    				else
    				if (maze[w][l] == 'o')
    					maze[w][l] = open;
    				else
    				if (maze[w][l] == 'm')
    					maze[w][l] = m;
    				else
    				if (maze[w][l] == 'c')
    					maze[w][l] = c;
    			}
    		}
    
    			char nextstep=m;
    
    			do
    			{
    				
    			 if (nextstep+1==open)
    			 {
    				 nextstep=nextstep++;
    				 cout << nextstep << endl;
    			 }
    
    			}while ( m != 0);
    Can anyone tell me why this code produces a single odd symbol? I think I set it up right. Basically what im trying to do is tell the program to search for the letter 'm' and then start trying to find the next square. But all I get out is a really weird symbol. Any comments?

  6. #21
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    you need to give m a posistion. and have it move around. take a piece of paper. draw a maze on it:


    0 1 2
    +------
    0| X 0 X
    1| X 0 X
    2| 0 0 X

    Say your mouse starts at mx=0 my=2

    then you have to say, is there an open move for me to go to? Is there one to the North? mx,my-1 no. Is there one to the East? mx+1,my
    yes. mx = mx+1

    Now repeat. You'll run into problems with this is when your mouse tries to check to the south for exmple here. You go out of the array. You have to have a variable of max rows and max cols, so that you never check if your mx or my will go past that value.

    That's one method. Not sure if it's the best but...

    My bed time now...good luck
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #22
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    thank you. I truly appreaciate the help man.

  8. #23
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Code:
    	void demo()
    	{
    		
    		int x=8;
    		int y=8;
    		int l, w;
    		char border = 0;
    		char open = 1;
    
    		for (w = 0; w < 20; ++w)
    		{
    			for (l = 0; l < 20; ++l)
    			{
    				InMaze >> maze[w][l];
    				if (maze[w][l] == 'x')
    					maze[w][l] = border;
    				else
    				if (maze[w][l] == 'o')
    					maze[w][l] = open;
    				else
    				if (maze[w][l] == 'm')
    					maze[w][l] = mouse[x][y];
    				else
    				if (maze[w][l] == 'c')
    					maze[w][l] = c;
    			}
    		}
    
    			do
    			{
    				
    			 if (mouse[x][y-1]==open)
    			 {
    				 mouse[x][y]=mouse[x][y--];
    				 cout << y << endl;
    			 }
    			 else
    			 if (mouse[x][y+1]==open)
    			 {
    				 mouse[x][y]=mouse[x][y++];
    				 cout << y << endl;
    			 }
    			 else
    			 if (mouse[x-1][y]==open)
    			 {
    				 mouse[x][y]=mouse[x--][y];
    				 cout << x << endl;
    			 }
    			 else
    			 if (mouse[x+1][y]==open)
    			 {
    				 mouse[x][y]=mouse[x++][y];
    				 cout << x << endl;
    			 }
    				 
    
    			}while ( mouse[x][y] != 0);
    
    			if (m == 0)
    				cout << "maze finished" << endl;
    	}
    God I hope someone is online. Ok this code is correct i think. But for some reason I dont get any output from where it says stuff like "cout << x << endl;"

    Any one know why nothing shows up?

  9. #24
    I'll help but I'm a little confused. It looks like you want this mouse to move to any space it finds empty until it gets through the maze?

    First off, I'm seeing a lot of variables that I'm not sure about...

    if (m == 0)

    Whats m?

    'maze' I'm assuming would be the array that contains the maze... 'mouse' is what exactly? Why is it a 2D array? Shouldnt your mouse be represented by x and y variables inside the maze?

    Oh, and earlier in your code I noticed this:

    if (maze[w][l] == 'o')
    nextstep == m--;

    I notice it disapears later, so I'm not sure if this was just a mistake but the '==' operator is for comparison and the '=' operator is for assigning. That should have been this:

    if (maze[w][l] == 'o')
    nextstep = m--;

    Just for future reference.

    If you can clear up my questions I'll be hanging around to help (for a while, and at times).
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  10. #25
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Ok the m stands for mouse. I think im going to get rid of it. Ill post my entire source so you can see everything. I have the mouse being a 2d array because the dud kermi suggested it. Here is my source.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    
    ifstream InMaze;
    ofstream Maze;
    
    void reading();
    void demo();
    void demo2();
    
    char m;
    char c = 0;
    
    char maze[20][20];
    char mouse[20][20];
    
    	int main ()
    	{
    
    		cout << "       Welcome to THE MAZE     " << endl;
    		cout << "This program doenst work!" << endl;
    		cout << endl;
    		
    		//reading();
    		//demo();
    		return 0;
    	}
    
    	// This is where the data is read into the program
    	void reading()
    	{
    
    		InMaze.open("maze.txt");
    		Maze.open("Result.txt");
    	}
    
    
    	//This is a demo of how the output will be setup
    	void demo()
    	{
    		
    		int x=8;
    		int y=8;
    		int l, w;
    		char border = 0;
    		char open = 1;
    
    		for (w = 0; w < 20; ++w)
    		{
    			for (l = 0; l < 20; ++l)
    			{
    				InMaze >> maze[w][l];
    				if (maze[w][l] == 'x')
    					maze[w][l] = border;
    				else
    				if (maze[w][l] == 'o')
    					maze[w][l] = open;
    				else
    				if (maze[w][l] == 'm')
    					maze[w][l] = mouse[x][y];
    				else
    				if (maze[w][l] == 'c')
    					maze[w][l] = c;
    			}
    		}
    
    			do
    			{
    				
    			 if (mouse[x][y-1]==open)
    			 {
    				 mouse[x][y]=mouse[x][y--];
    				 Maze << y;
    			 }
    			 else
    			 if (mouse[x][y+1]==open)
    			 {
    				 mouse[x][y]=mouse[x][y++];
    				 Maze << y;
    			 }
    			 else
    			 if (mouse[x-1][y]==open)
    			 {
    				 mouse[x][y]=mouse[x--][y];
    				 Maze << x;
    			 }
    			 else
    			 if (mouse[x+1][y]==open)
    			 {
    				 mouse[x][y]=mouse[x++][y];
    				 Maze << x;
    			 }
    				 
    
    			}while ( mouse[x][y] != 0);
    
    			if (mouse[x][y] == 0)
    				cout << "maze finished" << endl;
    
    		for (int i = 0; i < 20; i++)
    		{
    			for (int a = 0; a < 20; a++)
    			{
    				maze[i][a].Write();
    				cout << endl;
    			}
    		}
    	}
    Now the maze array is 2d so it can hold the x and y axis of it. Im really not sure if this is right. Im juz kinda going blindly into the night. Any suggestions?

    O and the mouse needs to find the shorest path. I plan on adding a recurring algorithm later on. But right now im stuck. Please help. THanx

  11. #26
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    for the love of god can some one give me a hand

  12. #27
    This is just an example. It wont work because theres no algorithm for finding the path, the mouse will just move back and forth. I just typed this up in notepad but i dont see anything wrong with it. Should compile fine. I created a temporary maze variable that I filled with the worlds simplest maze to demonstrate. You'll obviously want to read your own data into 'maze' before you call your demo function.

    Tell me if this was what you're looking for... I'm still a little confused.

    //I wont argue about your use of iostream as its a matter of
    //preference but I'll leave all input as-is. I assume your supposed
    //to use iostream anyhow so I wont make suggestions about better
    //alternatives. Its my own opinion anyways...

    Code:
    //We define the size of the Maze here so if it changes,
    //we can alter it easily and in one place only
    #define SizeOfMazeX 10
    #define SizeOfMazeY 10
    
    //different states the any element of the maze may be in
    #define BORDER	0
    #define OPEN	1
    #define BLOCKED	2
    
    //The Maze array
    char maze[SizeOfMazeX][SizeOfMazeY] =
    {
    	'x','x','x','x','x','x','x','x','x','x',
    	'x','o','o','o','o','o','o','o','o','x',
    	'x','o','o','o','o','o','o','o','o','x',
    	'x','o','o','o','c','c','c','o','o','x',
    	'x','o','o','o','c','m','c','o','o','x',
    	'x','o','o','o','c','o','c','o','o','x',
    	'x','o','o','o','c','o','c','o','o','x',
    	'x','o','o','o','c','o','c','o','o','x',
    	'x','o','o','o','c','o','c','o','o','x',
    	'x','x','x','x','x','x','x','x','x','x',
    };
    
    //The position of our little rodent (0 by default)
    char MouseX = 0, MouseY = 0;
    
    	int main (void)
    	{
    		int x, y;
    
    		//Apparently we want to take the maze and convert its contents
    		//into Wall/Open/Exit data.
    		for (x = 0; x < SizeOfMazeX; x++)
    		{
    			for (y = 0; y < SizeOfMazeY; y++)
    			{
    				//I dont know why we're doing this but we're doing it anyhow
    				//We could just as easily compare the character values later
    				//instead of converting them to these predefined values...
    				//Any reason you do this?
    
    				//print out each element of the array (to show the maze)
    				cout << maze[x][y];
    
    				if (maze[x][y] == 'x')
    					maze[x][y] = BORDER;
    				else if (maze[x][y] == 'o')
    					maze[x][y] = OPEN;
    				else if (maze[x][y] == 'm')
    				{
    					//I assume that the 'm' is where we want the mouse
    					//to start. So we assign our Mouse coordinates to
    					//the position we're currently reading
    					MouseX = x;
    					MouseY = y;
    					//We'll, the mouse starts here so I assume its open...
    					maze[x][y] = OPEN;
    				}
    				else if (maze[x][y] == 'c')
    					maze[x][y] = BLOCKED;
    			}
    
    			//move to next line
    			cout << endl;
    		}
    
    		do
    		{
    			//Move around
    			//IMPORTANT: This wont work to solve the maze in any way.
    			//This is just an example. You said you would do your own
    			//algorithm to solve this. This just moves the mouse to
    			//the first empty space it finds near it. Using this code
    			//will just make the mouse run around in circles (back and
    			//for, back and forth). Put your own algorithm here.
    
    			if (maze[MouseX][MouseY-1] == OPEN)
    				MouseY --;
    			else if (maze[MouseX][MouseY+1] == OPEN)
    				MouseY ++;
    			else if (maze[MouseX-1][MouseY] == OPEN)
    				MouseX --;
    			else if (maze[MouseX+1][MouseY] == OPEN)
    				MouseX ++;
    
    		}while (maze[MouseX][MouseY] != BORDER);
    
    //		if (maze[x][y] == 0)
    		//We dont need to check that because if we got out of the
    		//while loop than we obviously reached the BORDER
    		cout << "maze finished" << endl;
    
    		return 0;
    	}
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  13. #28
    Apologies on something. All these lines:

    if (maze[MouseX][MouseY-1] == OPEN)

    should instead be:

    if (maze[MouseX][MouseY-1] != BLOCKED)

    otherwise mousey will never reach an exit square since its not technically "open".

    (That and to note that I changed to maze size from 20 to 10 for this example because it was easier to type in a 10x10 maze than a 20x20. Call me lazy. Just change the #defines for your own program.)
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  14. #29
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    I have a question. In that example code you wrote you made the x's equal the BORDER and the c is equal to blocked. Why? During this program you wrote if the mouse reaches a 'x' then the program will end. I need to program to end when the mouse reaches the 'c'. so i think instead of using that loop you should have put c equal to finish and the make the do loop say. Do once then check if the mouse has reached the finish (or cheese), and then just take out the border. I think that would be right. What do u think?

  15. #30
    Ahh, sorry, I misinterpreted your values. Your assumptions are correct. Re-written correctly (with the cheese and no borders):
    Code:
    //We define the size of the Maze here so if it changes,
    //we can alter it easily and in one place only
    #define SizeOfMazeX 10
    #define SizeOfMazeY 10
    
    //different states the any element of the maze may be in
    #define CHEESE	0
    #define OPEN	1
    #define BLOCKED	2
    
    //The Maze array
    char maze[SizeOfMazeX][SizeOfMazeY] =
    {
    	'x','x','x','x','x','x','x','x','x','x',
    	'x','o','o','o','o','o','o','o','o','x',
    	'x','o','o','o','o','o','o','o','o','x',
    	'x','o','o','o','x','x','x','o','o','x',
    	'x','o','o','o','x','m','x','o','o','x',
    	'x','o','o','o','x','o','x','o','o','x',
    	'x','o','o','o','x','o','x','o','o','x',
    	'x','o','o','o','x','o','x','o','o','x',
    	'x','o','o','o','x','c','x','o','o','x',
    	'x','x','x','x','x','x','x','x','x','x',
    };
    
    //The position of our little rodent (0 by default)
    char MouseX = 0, MouseY = 0;
    
    	int main (void)
    	{
    		int x, y;
    
    		//Apparently we want to take the maze and convert its contents
    		//into Wall/Open/Exit data.
    		for (x = 0; x < SizeOfMazeX; x++)
    		{
    			for (y = 0; y < SizeOfMazeY; y++)
    			{
    				//I dont know why we're doing this but we're doing it anyhow
    				//We could just as easily compare the character values later
    				//instead of converting them to these predefined values...
    				//Any reason you do this?
    
    				//print out each element of the array (to show the maze)
    				cout << maze[x][y];
    
    				if (maze[x][y] == 'x')
    					maze[x][y] = BLOCKED;
    				else if (maze[x][y] == 'o')
    					maze[x][y] = OPEN;
    				else if (maze[x][y] == 'm')
    				{
    					//I assume that the 'm' is where we want the mouse
    					//to start. So we assign our Mouse coordinates to
    					//the position we're currently reading
    					MouseX = x;
    					MouseY = y;
    					//We'll, the mouse starts here so I assume its open...
    					maze[x][y] = OPEN;
    				}
    				else if (maze[x][y] == 'c')
    					maze[x][y] = CHEESE;
    			}
    
    			//move to next line
    			cout << endl;
    		}
    
    		do
    		{
    			//Move around
    			//IMPORTANT: This wont work to solve the maze in any way.
    			//This is just an example. You said you would do your own
    			//algorithm to solve this. This just moves the mouse to
    			//the first empty space it finds near it. Using this code
    			//will just make the mouse run around in circles (back and
    			//for, back and forth). Put your own algorithm here.
    
    			if (maze[MouseX][MouseY-1] != BLOCKED)
    				MouseY --;
    			else if (maze[MouseX][MouseY+1] != BLOCKED)
    				MouseY ++;
    			else if (maze[MouseX-1][MouseY] != BLOCKED)
    				MouseX --;
    			else if (maze[MouseX+1][MouseY] != BLOCKED)
    				MouseX ++;
    
    		}while (maze[MouseX][MouseY] != CHEESE);
    
    		cout << "maze finished" << endl;
    
    		return 0;
    	}
    Does this help now? It still wont work because theres no mouse moving algorithm, but you said you have that covered...
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM