Thread: Creating Maze Silmulator in C++ language

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    Creating Maze Silmulator in C++ language

    Do anyone know how to create a maze simulator using c++ coding and in visual studio 2008 application? Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yes.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by King Mir View Post
    Yes.
    Orh ok. But it run it window form application not in CLR console application. Can it been done also?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yes. And to be a tiny bit more helpful take a look at this.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by master5001 View Post
    Yes. And to be a tiny bit more helpful take a look at this.
    Huh? That is a google link.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kazaftan View Post
    Huh? That is a google link.
    Google is your friend.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    Google is your friend.
    Dots. I been searching for quite sometime already. But the maze still unable to load n display.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kazaftan View Post
    Dots. I been searching for quite sometime already. But the maze still unable to load n display.
    Wait! You mean you have an actual question about the subject? Why don't you ask it?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    Wait! You mean you have an actual question about the subject? Why don't you ask it?
    LOL! ya.i uable to load n display the maze out. Google example are mostly in CLR console application not in window form application. That why don't know how is it been done.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kazaftan View Post
    LOL! ya.i uable to load n display the maze out. Google example are mostly in CLR console application not in window form application. That why don't know how is it been done.
    I have no idea what you've done or what specifically you're trying to do, so all I can say is that typing "Windows Forms maze generator" got 25000 hits on Google, and the first page had several that looked promising if you were willing to deal with C#. If you need assistance with your code, you're going to have be way more specific than "I can't do it". Also since you seem to insist upon Windows Forms I would recommend you ask the question in the Windows forum.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    I have no idea what you've done or what specifically you're trying to do, so all I can say is that typing "Windows Forms maze generator" got 25000 hits on Google, and the first page had several that looked promising if you were willing to deal with C#. If you need assistance with your code, you're going to have be way more specific than "I can't do it". Also since you seem to insist upon Windows Forms I would recommend you ask the question in the Windows forum.
    orh ok. Thank you for your help. If i counter any problem i will post again.
    Last edited by kazaftan; 09-24-2008 at 08:37 PM. Reason: missing words

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Code:
    using System;
    
    namespace Mehroz
    {
    		public MazeSolver(int[,] iMaze)
    		{
    			m_iMaze=iMaze;
    			m_iRows=iMaze.GetLength(0);
    			m_iCols=iMaze.GetLength(1);
    		}
    
    		/// <summary>
    		/// Constructor 2: initializes the dimensions of maze, 
    		/// later, indexers may be used to set individual elements' values
    		/// </summary>
    		public MazeSolver(int iRows, int iCols)
    		{
    			m_iMaze=new int[iRows, iCols];
    			m_iRows=iRows;
    			m_iCols=iCols;
    		}
    			
    		/// <summary>
    		/// Properites:
    		/// </summary>
    		public int Rows
    		{
    			get { return m_iRows; }
    		}
    
    		public int Cols
    		{
    			get { return m_iCols; }
    		}
    		public int[,] GetMaze
    		{
    			get { return m_iMaze; }
    		}
    		public int PathCharacter
    		{
    			get { return iPath; }
    			set
    			{
    				if (value==0)
    					throw new Exception("Invalid path character specified");
    				else
    					iPath=value;
    			}
    		}
    		public bool AllowDiagonals
    		{
    			get {	return diagonal;	}
    			set {	diagonal=value;		}
    		}
    
    
    		/// <summary>
    		/// Indexer
    		/// </summary>
    		public int this[int iRow, int iCol]
    		{
    			get { return m_iMaze[iRow,iCol]; }
    			set 
    			{ 
    				m_iMaze[iRow,iCol]=value;
    				if (this.OnMazeChangedEvent!=null)	// trigger event
    					this.OnMazeChangedEvent(iRow, iCol);
    			}
    		}
    
    		/// <summary>
    		/// The function is used to get the contents of a given node in a given maze,
    		///  specified by its node no.
    		/// </summary>
    		private int GetNodeContents(int[,] iMaze, int iNodeNo)
    		{
    			int iCols=iMaze.GetLength(1);
    			return iMaze[iNodeNo/iCols,iNodeNo-iNodeNo/iCols*iCols];
    		}
    
    		/// <summary>
    		/// The function is used to change the contents of a given node in a given maze,
    		///  specified by its node no.
    		/// </summary>
    		private void ChangeNodeContents(int[,] iMaze, int iNodeNo, int iNewValue)
    		{
    			int iCols=iMaze.GetLength(1);
    			iMaze[iNodeNo/iCols,iNodeNo-iNodeNo/iCols*iCols]=iNewValue;
    		}
    		
    		/// <summary>
    		/// This public function finds the shortest path between two points
    		/// in the maze and return the solution as an array with the path traced 
    		/// by "iPath" (can be changed using property "PathCharacter")
    		/// if no path exists, the function returns null
    		/// </summary>
    		public int[,] FindPath(int iFromY, int iFromX, int iToY, int iToX)
    		{
    			int iBeginningNode = iFromY*this.Cols + iFromX;
    			int iEndingNode = iToY*this.Cols + iToX;
    			return ( Search(iBeginningNode, iEndingNode) ) ;
    		}
    
    	
    		/// <summary>
    		/// Internal function for that finds the shortest path using a technique
    		/// similar to breadth-first search.
    		/// It assigns a node no. to each node(2D array element) and applies the algorithm
    		/// </summary>
    		private enum Status
    		{	Ready,	Waiting,	Processed	}
    		private int[,] Search(int iStart, int iStop)
    		{
    			const int empty=0;
    		
    			int iRows=m_iRows;
    			int iCols=m_iCols;
    			int iMax=iRows*iCols;
    			int[] Queue=new int[iMax];
    			int[] Origin=new int[iMax];
    			int iFront=0, iRear=0;
    
    			//check if starting and ending points are valid (open)
    			if ( GetNodeContents(m_iMaze, iStart)!=empty || GetNodeContents(m_iMaze, iStop)!=empty )
    			{
    				return null;
    			}
    		
    			//create dummy array for storing status
    			int[,] iMazeStatus=new int[iRows,iCols];
    			//initially all nodes are ready
    			for(int i=0;i<iRows;i++)
    				for(int j=0;j<iCols;j++)
    					iMazeStatus[i,j]=(int)Status.Ready;
    
    			//add starting node to Q
    			Queue[iRear]=iStart; 
    			Origin[iRear]=-1; 
    			iRear++;
    			int iCurrent, iLeft, iRight, iTop, iDown, iRightUp, iRightDown, iLeftUp, iLeftDown;
    			while(iFront!=iRear)	// while Q not empty	
    			{
    				if (Queue[iFront]==iStop)		// maze is solved
    					break;
    
    				iCurrent=Queue[iFront];
    			
    				iLeft=iCurrent-1;
    				if (iLeft>=0 && iLeft/iCols==iCurrent/iCols) 	//if left node exists
    					if ( GetNodeContents(m_iMaze, iLeft)==empty ) 	//if left node is open(a path exists)
    						if (GetNodeContents(iMazeStatus, iLeft) == (int)Status.Ready)	//if left node is ready
    						{
    							Queue[iRear]=iLeft; //add to Q
    							Origin[iRear]=iCurrent;
    							ChangeNodeContents(iMazeStatus, iLeft, (int)Status.Waiting); //change status to waiting
    							iRear++;
    						}
    
    				iRight=iCurrent+1;
    				if (iRight<iMax && iRight/iCols==iCurrent/iCols) 	//if right node exists
    					if ( GetNodeContents(m_iMaze, iRight)==empty ) 	//if right node is open(a path exists)
    						if (GetNodeContents(iMazeStatus, iRight) == (int)Status.Ready)	//if right node is ready
    						{
    							Queue[iRear]=iRight; //add to Q
    							Origin[iRear]=iCurrent;
    							ChangeNodeContents(iMazeStatus, iRight, (int)Status.Waiting); //change status to waiting
    							iRear++;
    						}
    		
    				iTop=iCurrent-iCols;
    				if (iTop>=0 ) 	//if top node exists
    					if ( GetNodeContents(m_iMaze, iTop)==empty ) 	//if top node is open(a path exists)
    						if (GetNodeContents(iMazeStatus, iTop) == (int)Status.Ready)	//if top node is ready
    						{
    							Queue[iRear]=iTop; //add to Q
    							Origin[iRear]=iCurrent;
    							ChangeNodeContents(iMazeStatus, iTop, (int)Status.Waiting ); //change status to waiting
    							iRear++;
    						}
    
    				iDown=iCurrent+iCols;
    				if (iDown<iMax ) 	//if bottom node exists
    					if ( GetNodeContents(m_iMaze, iDown)==empty ) 	//if bottom node is open(a path exists)
    						if (GetNodeContents(iMazeStatus, iDown) == (int)Status.Ready)	//if bottom node is ready
    						{
    							Queue[iRear]=iDown; //add to Q
    							Origin[iRear]=iCurrent;
    							ChangeNodeContents(iMazeStatus, iDown, (int)Status.Waiting); //change status to waiting
    							iRear++;
    						}
    				if (diagonal==true)
    				{
    					iRightDown=iCurrent+iCols+1;
    					if (iRightDown<iMax && iRightDown>=0 && iRightDown/iCols==iCurrent/iCols+1 ) 	//if bottom-right node exists
    						if ( GetNodeContents(m_iMaze, iRightDown)==empty ) 	//if this node is open(a path exists)
    							if (GetNodeContents(iMazeStatus, iRightDown) == (int)Status.Ready)	//if this node is ready
    							{
    								Queue[iRear]=iRightDown; //add to Q
    								Origin[iRear]=iCurrent;
    								ChangeNodeContents(iMazeStatus, iRightDown, (int)Status.Waiting); //change status to waiting
    								iRear++;
    							}
    
    					iRightUp=iCurrent-iCols+1;
    					if (iRightUp>=0 && iRightUp<iMax && iRightUp/iCols==iCurrent/iCols-1) 	//if upper-right node exists
    						if ( GetNodeContents(m_iMaze, iRightUp)==empty ) 	//if this node is open(a path exists)
    							if (GetNodeContents(iMazeStatus, iRightUp) == (int)Status.Ready)	//if this node is ready
    							{
    								Queue[iRear]=iRightUp; //add to Q
    								Origin[iRear]=iCurrent;
    								ChangeNodeContents(iMazeStatus, iRightUp, (int)Status.Waiting); //change status to waiting
    								iRear++;
    							}
    
    					iLeftDown=iCurrent+iCols-1;
    					if (iLeftDown<iMax && iLeftDown>=0 && iLeftDown/iCols==iCurrent/iCols+1) 	//if bottom-left node exists
    						if ( GetNodeContents(m_iMaze, iLeftDown)==empty ) 	//if this node is open(a path exists)
    							if (GetNodeContents(iMazeStatus, iLeftDown) == (int)Status.Ready)	//if this node is ready
    							{
    								Queue[iRear]=iLeftDown; //add to Q
    								Origin[iRear]=iCurrent;
    								ChangeNodeContents(iMazeStatus, iLeftDown, (int)Status.Waiting); //change status to waiting
    								iRear++;
    							}
    
    					iLeftUp=iCurrent-iCols-1;
    					if (iLeftUp>=0 && iLeftUp<iMax && iLeftUp/iCols==iCurrent/iCols-1) 	//if upper-left node exists
    						if ( GetNodeContents(m_iMaze, iLeftUp)==empty ) 	//if this node is open(a path exists)
    							if (GetNodeContents(iMazeStatus, iLeftUp) == (int)Status.Ready)	//if this node is ready
    							{
    								Queue[iRear]=iLeftUp; //add to Q
    								Origin[iRear]=iCurrent;
    								ChangeNodeContents(iMazeStatus, iLeftUp, (int)Status.Waiting); //change status to waiting
    								iRear++;
    							}
    				}
    
    
    				//change status of current node to processed
    				ChangeNodeContents(iMazeStatus, iCurrent, (int)Status.Processed);
    				iFront++;
    			
    			}
    
    			//create an array(maze) for solution
    			int[,] iMazeSolved=new int[iRows,iCols];
    			for(int i=0;i<iRows;i++)
    				for(int j=0;j<iCols;j++)
    					iMazeSolved[i,j]=m_iMaze[i,j];
    
    			//make a path in the Solved Maze
    			iCurrent=iStop;
    			ChangeNodeContents(iMazeSolved, iCurrent, iPath);
    			for(int i=iFront; i>=0; i--)
    			{
    				if (Queue[i]==iCurrent)
    				{
    					iCurrent=Origin[i];
    					if (iCurrent==-1)		// maze is solved
    						return ( iMazeSolved );
    					ChangeNodeContents(iMazeSolved, iCurrent, iPath);
    				}
    			}
    
    			//no path exists
    			return null;
    		}
    	}
    
    }

    Above is in C# coding. How to convert it into C++ coding?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My C# knowledge is way less than my C++ knowledge, so beware, but: the main thing I see is deciding what you want the data structure to be to replace the [,]-style 2D array. A private vector-of-vector perhaps, with overloaded []? Obviously get/set would have to be replaced by member functions. The logic doesn't seem awful.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > How to convert it into C++ coding?
    How about attempting your own homework rather than seeing who's got the best google skills in your class.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    kazaftan, lemme spell it out for you, since you can't seem to be bother to read the AUP. This sounds like you're looking for someone to do your homework for you. This board take a very dim view of folks trying to get others to do their homework for them. A little bit of research and using your own brain will solve this problem for you.

    Don't try to convert someone else's solution from C#. Write your own. Use your brain. You can do it.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASM to C language
    By TAZIN in forum C Programming
    Replies: 22
    Last Post: 06-03-2009, 06:29 AM
  2. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  3. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  4. Enough language discussions.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-13-2004, 09:59 AM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM