C++ maze game help!

Discussion in 'C++' started by Brett McIntyre, Nov 21, 2010.

?

sorry, i clicked poll and am not sure what it is. plz ignore.

Poll closed Dec 11, 2010.
  1. Find a recursive path to solving the maze

    0 vote(s)
    0.0%
  2. prompt user to enter start and finish points

    0 vote(s)
    0.0%
  3. maze is outputted randomly

    0 vote(s)
    0.0%
  4. nothing

    0 vote(s)
    0.0%
  1. Brett McIntyre

    Brett McIntyre New Member

    Joined:
    Nov 21, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to finish a maze game i started,
    i cannot seem to implement the idea i have in mind fr this game, am having trouble with what i want,
    i would like to prompt the user to plot their own start and finish points for the maze game, nd for the game to end after they have made their way from their plotted start to finish point,
    i would also like to have a recursive approach to finding this path, also when the user hits the maze wall they should not be able to pass,
    currently the user can pass :snobby:, and my player can only move a limited amount of 3-5 spaces...:sosp:, dont know y...am not the best that y am asking for help..please anyone who can lend a hand and spear some of their time, i would truly be great full. [/mark]

    What the article / tutorials /code snippet does, why it's useful, the problem it solves etc.


    Code:
    ////--a maze game that i would like to (1).prompt the user to enter a start location and a ////finish location 
    ////-----------------------------------(2).Use a recursive approach to find a path in the ////maze from the specified start to the specified finish 
    ////-----------------------------------(3).If a path exists between the two points specified, ////display the path to the screen, otherwise issue an appropriate error message to the user
    ////-----------------------------------(4).If the player meets a wall in maze the player ////cannot pass, i dont understand how to do this.
     ////----PLEASE HELP ME COMPLETE MY PROBLEMS I MENTIONED ABOVE-----------
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    using namespace std;
    
    #define COLUMN 20 //This is a GLOBAL SYMBOLIC CONSTANT
    #define ROW 70 //This is a GLOBAL SYMBOLIC CONSTANT
    
    int main()
    {
        
    
            char Maze[ROW][COLUMN];
            int x;
        
        
        for (int i = 0; i < COLUMN; i++)
        {
            for (int j = 0; j < ROW; j++)
            {
                
                Maze[i][j] = 219;
                cout<<"\n";
                
            }
        
        }
    
        srand (time(NULL));
        for (int i = 0; i < COLUMN; i++)
        {
            for (int j = 0; j < ROW; j++)
            {
                
                x = 1 + rand() % 4;
    
                if (x == 1)
                {
                    Maze [i][j] =' ';
                }
    
                cout<< Maze[i][j];
                
    
            }
            cout<<"\n";
        
        }
        ////////////////////---would like to use this and have user chose their own start point so they cna start in any position
        ////--------before this is possible i need a border of the x and y cos, so user can know their options,
        /*
        int xPos ;
        int yPos ;
        cout << "setYour start point for y co's" << endl;
        cin >> yPos;
        cout << "setYour start point for X co's" << endl;
        cin >> xPos;
        */
    
        ////////////////------USER SET FINISH POINTS--SO WHEN USER FINDS END OF MAZE(FINISHPOINT) GAME WILL END.
        /*
        int finishpointx;
        int finishpointy;
        cout << "setYour finish point for y co's" << endl;
        cin >> finishpointy;
        cout << "setYour finish point for X co's" << endl;
        cin >> finishpointx;
        */
    
    
    
         char movement;
         int xPos = 2;
         int yPos = 2;
     
    do {
      
       Maze[xPos][yPos] = 'O';
      
       system("Cls");
    
    
    for (int i = 0; i < COLUMN; i++)
          {
        for (int j = 0; j < ROW; j++)
            {
            cout << Maze[i][j];
            }
            cout << endl;
            }
      
           
      
          cout << "Do you want your character to move [L]eft, [R]ight, [U]p or [D]own";
          cout << "[E]xit:";
          cin >> movement;
    
    Maze[xPos][yPos] = 'X';
    
    
    switch (movement)/////-----movement has a flaw, the player "o" only moves a limited amount of spaces..am trying to fix..
    {
    
          case 'U'://UP
          if (xPos > 0)
          { xPos--;}
          break;
      
              case 'D'://DOWN
              if (xPos < 4)
              { xPos++; }
            break;
      
                  case 'L'://LEFT
                  if (yPos > 0)
                  { yPos--; }
                  break;
      
                      case 'R'://RIGHT
                      if (yPos < 4)
                      {yPos++;}
                      break;
      
                          case 'E':
                          break;
    
    }}
    
          while(movement != 'E');
         // cin.get();
          return 0;
      }
    A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

    Blocks of code should be set as style "Formatted" like this.
    Code: Cpp
    Code:
    ////--a maze game that i would like to (1).prompt the user to enter a start location and a ////finish location 
    ////-----------------------------------(2).Use a recursive approach to find a path in the ////maze from the specified start to the specified finish 
    ////-----------------------------------(3).If a path exists between the two points specified, ////display the path to the screen, otherwise issue an appropriate error message to the user
    ////-----------------------------------(4).If the player meets a wall in maze the player ////cannot pass, i dont understand how to do this.
     ////----PLEASE HELP ME COMPLETE MY PROBLEMS I MENTIONED ABOVE-----------
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    using namespace std;
    
    #define COLUMN 20 //This is a GLOBAL SYMBOLIC CONSTANT
    #define ROW 70 //This is a GLOBAL SYMBOLIC CONSTANT
    
    int main()
    {
    	
    
            char Maze[ROW][COLUMN];
    	    int x;
    	
    	
    	for (int i = 0; i < COLUMN; i++)
    	{
    		for (int j = 0; j < ROW; j++)
    		{
    			
    			Maze[i][j] = 219;
    			cout<<"\n";
    			
    		}
    	
    	}
    
    	srand (time(NULL));
    	for (int i = 0; i < COLUMN; i++)
    	{
    		for (int j = 0; j < ROW; j++)
    		{
    			
    			x = 1 + rand() % 4;
    
    			if (x == 1)
    			{
    				Maze [i][j] =' ';
    			}
    
    			cout<< Maze[i][j];
    			
    
    		}
    		cout<<"\n";
    	
    	}
    	////////////////////---would like to use this and have user chose their own start point so they cna start in any position
    	////--------before this is possible i need a border of the x and y cos, so user can know their options,
    	/*
    	int xPos ;
        int yPos ;
    	cout << "setYour start point for y co's" << endl;
    	cin >> yPos;
    	cout << "setYour start point for X co's" << endl;
        cin >> xPos;
    	*/
    
    	////////////////------USER SET FINISH POINTS--SO WHEN USER FINDS END OF MAZE(FINISHPOINT) GAME WILL END.
    	/*
    	int finishpointx;
    	int finishpointy;
    	cout << "setYour finish point for y co's" << endl;
    	cin >> finishpointy;
    	cout << "setYour finish point for X co's" << endl;
    	cin >> finishpointx;
    	*/
    
    
    
         char movement;
         int xPos = 2;
         int yPos = 2;
     
    do {
      
       Maze[xPos][yPos] = 'O';
      
       system("Cls");
    
    
    for (int i = 0; i < COLUMN; i++)
          {
        for (int j = 0; j < ROW; j++)
    		{
    		cout << Maze[i][j];
    		}
            cout << endl;
            }
      
           
      
          cout << "Do you want your character to move [L]eft, [R]ight, [U]p or [D]own";
          cout << "[E]xit:";
          cin >> movement;
    
    Maze[xPos][yPos] = 'X';
    
    
    switch (movement)/////-----movement has a flaw, the player "o" only moves a limited amount of spaces..am trying to fix..
    {
    
          case 'U'://UP
          if (xPos > 0)
          { xPos--;}
          break;
      
    		  case 'D'://DOWN
    		  if (xPos < 4)
    		  { xPos++; }
    		break;
      
    			  case 'L'://LEFT
    			  if (yPos > 0)
    			  { yPos--; }
    			  break;
      
    				  case 'R'://RIGHT
    				  if (yPos < 4)
    				  {yPos++;}
    				  break;
      
    					  case 'E':
    					  break;
    
    }}
    
          while(movement != 'E');
         // cin.get();
          return 0;
      }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You submitted multiple times the same thread as articles and avoid that. I have moved your query into the forum
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    -first of all make sure that the user enters valid positions for start and finish points
    positions must be inside the array

    -your for -loops are all wrong

    i---->Rows and not Columns!!!
    j----->Columns and not Rows!!!

    change all for-loops like this
    Code:
    .....
            char Maze[ROW][COLUMN];
            int x;
        
        
        for (int i = 0; i < ROW; i++)
        {
            for (int j = 0; j < COLUMN; j++)
            {
                
                Maze[i][j] = 219;
    .....
    
    -your maze creation algorithm is not good at all.You need to implement a new algorithm

    -when you move inside the maze you must check before you move these:
    1) is the new position a wall?if yes then movement is invalid
    2) is the new position outside the maze?if yes then movement is invalid
    3) is the new position revisited?if yes then movement is invalid
    4) are there any more moves?if no then i ended in a dead end

    -repost your code with changes
     
    Last edited: Nov 22, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice