简体   繁体   中英

Right hand maze traversal in C

This is my first question, and yes, it is a "homework" assignment. I have been working on it for hours but cannot get the algorithm to work. The program I've written is supposed to contain a function that accepts a 12 by 12 array and finds the path to the end. The main function finds the start of the maze and changes it to an "x" to represent the position of the "player." The main function then calls the mazePrint function that accepts the array prints it after clearing the screen. Then it calls the mazeTraversal function which accepts the array. The first part of mazeTraversal attempts to locate the position of "x" and replace it with a "." The next part is supposed to determine which direction the "x" is facing. I used 4, 5, 6, and 8 (west, south, east, and north (look at the number pad)) to represent the way it is facing. Based on the way it is facing, mazeTraversal attempts to determine if there is an open path to the right, then in front, then to the left, and then behind and to then put an X in that position and change the way the x is facing. Something goes wrong after the second move when I run the program. Thanks for any help, and sorry if this is not the place for such questions.

#include <stdio.h>
#include <stdlib.h>

void mazePrint(char *maze[12][12]);
void mazeTraversal(char *maze[12][12]);
static int face = 6;

main()
{
    int i = 0;
    int j = 0; 
    int k = 0;
    int start;
    int xpos;

    char *mazeone[12][12] = {
       //0///1///2///3///4///5///6///7///8///9///10//11///
        "#","#","#","#","#","#","#","#","#","#","#","#",//0
        "#",".",".",".","#",".",".",".",".",".",".","#",//1
        ".",".","#",".","#",".","#","#","#","#",".","#",//2
        "#","#","#",".","#",".",".",".",".","#",".","#",//3
        "#",".",".",".",".","#","#","#",".","#",".",".",//4
        "#","#","#","#",".","#",".","#",".","#",".","#",//5
        "#","#",".","#",".","#",".","#",".","#",".","#",//6
        "#","#",".","#",".","#",".","#",".","#",".","#",//7
        "#",".",".",".",".",".",".",".",".","#",".","#",//8
        "#","#","#","#","#","#",".","#","#","#",".","#",//9
        "#",".",".",".",".",".",".","#",".",".",".","#",//10
        "#","#","#","#","#","#","#","#","#","#","#","#",};//11

    for (i = 0; i <12; i++)
        if (mazeone[i][0] == "." ) {
            start = i; 
            mazeone[start][0] = "x";
            xpos = start;
            break;
        }

    printf("X is the starting point.\n");
    printf("Press Space Bar to watch the X move.\n\n\n");
    getchar();
    mazePrint(mazeone);
    getchar();
    return 0;
}

void mazePrint(char *maze[12][12])
{   
    int x = 0; 
    int y = 0;

    system("cls");
    for (x = 0; x < 12; x++) {
        for (y = 0; y < 12; y++) {
            printf("%s", maze[x][y]);
        }
        printf("\n"); 
    }
    getchar(); 
    mazeTraversal(maze);
}

void mazeTraversal(char *maze[12][12])
{
    int x = 0; 
    int y = 0;

    for (x = 0; x < 12; x++) {
        for (y = 0; y < 12; y++) {
            if (maze[y][x] == "x")
                break;
        } 
        if(maze[y][x] == "x")
            break;
    }

    for (y = 0; y < 12; y++) {
        for (x = 0; x < 12; x++) {
            if (maze[y][x] == "x")
                break;
        } 
        if (maze[y][x] == "x")
            break;
    }

    maze[y][x] = ".";

    switch (face) {
        case 6:{
            if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            } else if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            } else if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            } else if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x"; 
                face = 4;
            }
        }
        case 8:{
            if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            } else if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            } else if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x"; 
                face = 4;
            } else if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            }
        }
        case 4:{
            if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            } else if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x"; 
                face = 4;
            } else if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            } else if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            }
        }
        case 5:{
            if (maze[y - 1][x] == ".") {
                maze[y - 1][x] = "x";
                face = 4;
            } else if (maze[y][x-1] == ".") {
                maze[y][x-1] = "x"; 
                face = 5;
            } else if (maze[y + 1][x] == ".") {
                maze[y + 1][x] = "x"; 
                face = 6;
            } else if (maze[y][x+1] == ".") {
                maze[y][x+1] = "x";
                face = 8;
            }
        }
    }

    mazePrint(maze);
}

You are missing the break; statements in the switch(face) after each case: block of code. Without the break; statements, your code will fall through to each of the next case: , which is not what you want.

I think you've got your directions wrong. You should be starting at maze[2][0] , with facing '6'. You want to advance to maze[2][1] , with facing still '6'. But if you look at the mazeTraversal code for direction 6, you get this case:

if(maze[y][x+1] == "."){     
   maze[y][x+1] = "x";     
   face = 8;
 }

So you are setting the resulting direction incorrectly.
One thing that might help keep it straight is to use an enumeration instead of random numeric codes:

enum Facing {
   face_EAST,
   face_SOUTH,
   face_WEST, 
   face_NORTH } face = face_EAST;

I might even forget about compass directions and use directions specific to the problem. That should help keep the direction straight in the code.

enum Facing {
   face_Xplus,
   face_Yplus, 
   face_Xminus, 
   face_Yminus } face = face_Xplus;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM