Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ maze game help! (http://www.go4expert.com/forums/cpp-maze-game-help-t23952/)

Brett McIntyre 22Nov2010 01:49

C++ maze game help!
 
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;
  }


shabbir 22Nov2010 09:15

Re: C++ maze game help!
 
You submitted multiple times the same thread as articles and avoid that. I have moved your query into the forum

virxen 22Nov2010 11:58

Re: C++ maze game help!
 
-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
Quote:

.....
char Maze[ROW][COLUMN];
int x;


for (int i = 0; i < COLUMN; i++)
{
for (int j = 0; j < ROW; j++)
{

Maze[i][j] = 219;
.....
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


All times are GMT +5.5. The time now is 12:46.