Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Navigating a maze

chevy72350chevy72350 Posts: 2Member
I am working on a program that will navigate through a maze. The maze is # - walls and .-spaces that are navigatable. The object is to start on the left side at the dot and end at the right side at the dot. An x should navigate it. I am trying to get the x to move so that it solves the maze and the user can watch. Pretend the x has its right hand on the wall and has to follow the wall, which will get him out of the maze. However my x isn't moving and i am not sure why. Any help would be great.

#include
using std::right;
using std::left;
using std::endl;
using std::cout;
using std::cin;



int main(){

//creates the maze
char maze[][12]=
{{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};



char mark = 'x'; // the x that goes through the maze
int move; //moves the x
int row = 2;
int col = 0;


//start the maze at row 2 col 0
maze [2][0] = 'X';

//print the maze
for ( unsigned int row =0 ; row < 12; row++)
{
for (unsigned int col = 0; col < 12; col++)
{
cout << maze[row][col] << ' ';
}
cout << endl;
}

//while loop
while (maze[4][11] != mark) //while not at the end
{
for (int i = 0; i < 12; i++){
for (int j = 0; j < 12; j++){

if (maze [row][col] = '.'){
maze [row][col] = mark;
mark ++;

}

else {
move = (move + 1) % 4;
mark --;
}
for ( unsigned int row =0 ; row < 12; row++)
{
for (unsigned int col = 0; col < 12; col++)
{
cout << maze[row][col] << ' ';
}
cout << endl;
}
}
}

}}

Comments

  • nirnir Posts: 50Member
    : I am working on a program that will navigate through a maze. The maze is # - walls and .-spaces that are navigatable. The object is to start on the left side at the dot and end at the right side at the dot. An x should navigate it. I am trying to get the x to move so that it solves the maze and the user can watch. Pretend the x has its right hand on the wall and has to follow the wall, which will get him out of the maze. However my x isn't moving and i am not sure why. Any help would be great.
    :
    : #include
    : using std::right;
    : using std::left;
    : using std::endl;
    : using std::cout;
    : using std::cin;
    :
    :
    :
    : int main(){
    :
    : //creates the maze
    : char maze[][12]=
    : {{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
    : { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
    : { '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
    : { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
    : { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
    : { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
    : { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
    : { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
    : { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
    : { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
    : { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
    : { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};
    :
    :
    :
    : char mark = 'x'; // the x that goes through the maze
    : int move; //moves the x
    : int row = 2;
    : int col = 0;
    :
    :
    : //start the maze at row 2 col 0
    : maze [2][0] = 'X';
    :
    : //print the maze
    : for ( unsigned int row =0 ; row < 12; row++)
    : {
    : for (unsigned int col = 0; col < 12; col++)
    : {
    : cout << maze[row][col] << ' ';
    : }
    : cout << endl;
    : }
    :
    : //while loop
    : while (maze[4][11] != mark) //while not at the end
    : {
    : for (int i = 0; i < 12; i++){
    : for (int j = 0; j < 12; j++){
    : [blue] why do you do mark++? that is going to change your mark from x to y and if mark -- it will move it from y to x. This is a known algorithm which i'm sure that you can find some examples online. You can solve a maze in many ways one of them is backtracking
    [/blue]
    : if (maze [row][col] = '.'){
    : maze [row][col] = mark;
    : mark ++;
    :
    : }
    :
    : else {
    : move = (move + 1) % 4;
    : mark --;
    : }
    : for ( unsigned int row =0 ; row < 12; row++)
    : {
    : for (unsigned int col = 0; col < 12; col++)
    : {
    : cout << maze[row][col] << ' ';
    : }
    : cout << endl;
    : }
    : }
    : }
    :
    : }}
    :

  • BodkinBodkin Posts: 100Member
    Using the following pseudocode makes it easier to make the program.
    [code]Hand on wall? <---------------------------* <--* <--*
    yes: can go straight ahead? | | |
    yes: go ahead, finished? | | |
    no: ------------* | |
    yes: STOP | |
    | |
    no: turn counterclockwise ---* |
    no: turn clockwise, go ahead, finished? |
    no: ----------------------*
    yes: STOP
    [/code]

    [code]
    char *strMaze =
    "############"
    "#...#......#"
    "..#.#.####.#"
    "###.#....#.#"
    "#....###.#.."
    "####.#.#.#.#"
    "#..#.#.#.#.#"
    "##.#.#.#.#.#"
    "#........#.#"
    "######.###.#"
    "#......#...#"
    "############";
    // E,S,W,N
    int dirX[4] = {1,0,-1,0};
    int dirY[4] = {0,1,0,-1};

    void main() {
    // Top Left corner is (0,0)
    int x, y;
    int lookdir = 0, walldir;
    x=0;
    y=2;
    do {
    walldir = (lookdir+1)&3;
    if( *(strMaze + (y+dirY[walldir])*12 + (x+dirX[walldir]))=='#' ) {
    if( *(strMaze + ((y+dirY[lookdir])*12) + (x+dirX[lookdir]))=='.' ){
    cout << "Go ahead to (" << (x+dirX[lookdir]) << ", " << (y+dirY[lookdir]) << ")" << endl;
    x+=dirX[lookdir];
    y+=dirY[lookdir];
    }
    else {
    cout << "turn counterclockwise" << endl;
    lookdir = (lookdir+3)&3;
    }
    }
    else {
    cout << "turn clockwise" << endl;
    lookdir = (lookdir+1)&3;
    cout << "Go ahead to (" << (x+dirX[lookdir]) << ", " << (y+dirY[lookdir]) << ")" << endl;
    x+=dirX[lookdir];
    y+=dirY[lookdir];
    }
    } while(x<11);
    }[/code]
  • chevy72350chevy72350 Posts: 2Member
    bodkin, how do i get it to print the actual maze with an x at the spot it is currently at?
  • DonotaloDonotalo Posts: 715Member
    [b][red]This message was edited by Donotalo at 2006-2-18 10:22:31[/red][/b][hr]
    : bodkin, how do i get it to print the actual maze with an x at the spot it is currently at?
    :
    [purple]
    just save an 'x' at the respective position of the string array [blue]strMaze[/blue].
    [/purple]
    [hr][purple]~Donotalo()[/purple]



  • BodkinBodkin Posts: 100Member
    Code in green is new, code in red should be deleted.

    [code]
    #include
    [green]#include [/green]

    using namespace std;

    char *strMaze =
    "############"
    "#...#......#"
    "..#.#.####.#"
    "###.#....#.#"
    "#....###.#.."
    "####.#.#.#.#"
    "#..#.#.#.#.#"
    "##.#.#.#.#.#"
    "#........#.#"
    "######.###.#"
    "#......#...#"
    "############";
    // E,S,W,N
    int dirX[4] = {1,0,-1,0};
    int dirY[4] = {0,1,0,-1};
    [green]
    HANDLE hStdout;
    COORD cCoord;

    void writeAt(int x, int y, char ch) {
    cCoord.X = x;
    cCoord.Y = y;
    SetConsoleCursorPosition(hStdout, cCoord);
    cout << ch;
    }
    [/green]
    void main() {
    [green]hStdout = GetStdHandle(STD_OUTPUT_HANDLE);[/green]
    // Top Left corner is (0,0)
    int x, y;
    int lookdir = 0, walldir;
    [green] for(y=0; y<12; y++) {
    for(x=0; x<12; x++)
    cout << *(strMaze + y*12 + x);
    cout << endl;
    }[/green]
    x=0;
    y=2;
    [green] writeAt(x, y, 'X');
    Sleep(100);[/green]
    do {
    walldir = (lookdir+1)&3;
    if( *(strMaze + (y+dirY[walldir])*12 + (x+dirX[walldir]))=='#' ) {
    if( *(strMaze + ((y+dirY[lookdir])*12) + (x+dirX[lookdir]))=='.' ){
    [red]//cout << "Go ahead to (" << (x+dirX[lookdir]) << ", " <<
    // (y+dirY[lookdir]) << ")" << endl;[/red]
    [green] writeAt(x,y,'.');[/green]
    x+=dirX[lookdir];
    y+=dirY[lookdir];
    [green] writeAt(x, y, 'X');
    Sleep(100);[/green]
    }
    else {
    [red]//cout << "turn counterclockwise" << endl;[/red]
    lookdir = (lookdir+3)&3;
    }
    }
    else {
    [red]//cout << "turn clockwise" << endl;[/red]
    lookdir = (lookdir+1)&3;
    [red]//cout << "Go ahead to (" << (x+dirX[lookdir]) << ", " <<
    // (y+dirY[lookdir]) << ")" << endl;[/red]
    [green] writeAt(x, y, '.');[/green]
    x+=dirX[lookdir];
    y+=dirY[lookdir];
    [green] writeAt(x, y, 'X');
    Sleep(100);[/green]
    }
    } while(x<11);
    }
    [/code]
  • qemmieqemmie st vincentPosts: 1Member

    need some help please just getting started in programme
    would like some help to do a code for a maze in C

    The maze is to be created using an array which will store the structure of the maze required to use functions and take an incremental approach to the development of your final program

    The maze is of a fixed size and is displayed to the user. The user is required to assign a starting point and an ending point. The objective of the game to find a clear path from the starting point to the end point within the maze. The maze is then displayed showing the completed path.

    1. The maze must be of size 4X4 and must contain blocked paths.
    2. The path from starting point to end point must be clearly defined.
Sign In or Register to comment.