Thread: Help! Problem with maze walls

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Question Help! Problem with maze walls

    Hello, what I am working on is a program that is supposed to read what the user inputs and accordingly, moves the "mouse" through the maze. The mouse should be able to move using asdw, keep track of x and y coordinates, etc. The problem I am having now is correcting the part where the mouse runs into the wall. When it runs into the wall it should be after the user presses 'w' in front of a wall. Then the user is prompted to type 'y' or 'n' based on whether or not they wanted to restart. If they say yes, the mouse is sent back to the starting square (0, 0) and if not, the program is terminated. However, what is happening now is the mouse is crashing into the wall one step before it actually crashes into the wall. For instance, when it approaches the wall(right in front of it) it will say it crashed when I didn't even tell it to go forward. Please help! How can I make it so that it will say it crashed when it actually "hits" the wall. The part that should be the problem is (in my opinion) is in the crashtest function. The functions from display.c are just used to...well, display it a certain way so they aren't the problem.

    What I am doing right now in crashtest function is getting receiving whether or not there are walls on the north, south, west, and east sides of the"mouse" and comparing it to the direction it is currently facing. Then returning whether or not it has crashed. I've also tried to make an "if the character is 'w'" code, but it didn't work because it would almost always be 'w' anyway except when just turning towards a wall (which would also cause a crash message).

    Code is:

    Code:
    #include<stdio.h>
    #include"display.h"
    
    
    void bordercheck(int *x, int *y);
    int getdir(char ch, int dir);
    void getxy(char ch, int dir, int *x, int *y, int *cellcount);
    int crashtest(int dir, int *North, int *South, int *West, int *East);
    void mcamutil(int dir, int *North, int *South, int *East, int *West, int *left, int *front, int *right);
    
    
    main(){
    int     x = 0;
    int     y = 0;
    int     dir = NORTH;
    int     border;
    char    ch,
            ch2;
    int     level;
    int     cellcount = 0;
    int     North,
            South,
            East,
            West;
    int     left,
            right,
            front;
    int     ctest;
    char    yon;
    
    
            printf("Please enter the level: \n");
            scanf("%d", &level);
    
    
            init_display(level);
    
    
            while((ch = getchar()) != 'q'){
    
    
                    dir = getdir(ch, dir);
    
    
                    getxy(ch, dir, &x, &y, &cellcount);
                    show_cells(cellcount);
    
    
                    lvl0_get_walls(y, x, &North, &East, &West, &South);
                    put_walls(y, x, North, East, West, South);
                    ctest = crashtest(dir, &North, &South, &West, &East);
    
    
                            if(ctest == 1){
                                    write_message("Crashed, restart(y/n)?: ", 3);
                                    scanf("%c", &yon);
                                            if(yon == 'y'){
                                                    x = 0;
                                                    y = 0;
                                            }
    
    
                                            if(yon == 'n'){
                                                    goto end;
                                            }
                            }
    
    
                    mcamutil(dir, &North, &South, &East, &West, &left, &front, &right);
    
    
                    mouse_cam(left, front, right);
    
    
                    bordercheck(&x, &y);
    
    
                    show_mouse(dir, y, x);
                    show_position(y, x);
    
    
            }
    end:
            return;
    
    
    }
    
    
    int crashtest(int dir, int *North, int *South, int *West, int *East){
    int TRUE = 1;
    int FALSE = 0;
    
    
            if(dir == NORTH){
                    if(*North == 1){
                            return TRUE;
                    }
                    else return FALSE;
            }
            if(dir == SOUTH){
                    if(*South == 1){
                            return TRUE;
                    }
                    else return FALSE;
            }
            if(dir == EAST){
                    if(*East == 1){
                            return TRUE;
                    }
                    else return FALSE;
            }
            if(dir == WEST){
                    if(*West == 1){
                            return TRUE;
                    }
                    else return FALSE;
            }
    
    
    }
    
    
    void mcamutil(int dir, int *North, int *South, int *East, int *West, int *left, int *front, int *right){
            if(dir == NORTH){
                    *left = *West;
                    *front = *North;
                    *right = *East;
            }
            if(dir == EAST){
                    *left = *North;
                    *front = *East;
                    *right = *South;
            }
            if(dir == SOUTH){
                    *left = *East;
                    *right = *West;
                    *front = *South;
            }
            if(dir == WEST){
                    *left = *South;
                    *right = *North;
                    *front = *West;
            }
    }
    
    
    void getxy(char ch, int dir, int *x, int *y, int *cellcount){
    
    
            if(ch == 'w'){
                    if(dir == NORTH)
                            *y = *y + 1;
                    if(dir == SOUTH)
                            *y = *y - 1;
                    if(dir == EAST)
                            *x = *x + 1;
                    if(dir == WEST)
                            *x = *x - 1;
    
    
                    *cellcount = *cellcount + 1;
    
    
            }
    
    
            else
                    return;
    
    
    }
    
    
    
    
    int getdir(char ch, int dir){
    
    
                    if(dir == NORTH){
                            if(ch == 'a')
                                    dir = WEST;
                            if(ch == 's')
                                    dir = SOUTH;
                            if(ch == 'd')
                                    dir = EAST;
    
    
                    return dir;
    
    
                    }
    
    
                    if(dir == WEST){
                            if(ch == 'a')
                                    dir = SOUTH;
                            if(ch == 's')
                                    dir = EAST;
                            if(ch == 'd')
                                    dir = NORTH;
    
    
                    return dir;
    
    
                    }
    
    
                    if(dir == EAST){
                            if(ch == 'a')
                                    dir = NORTH;
                            if(ch == 's')
                                    dir = WEST;
                            if(ch == 'd')
                                    dir = SOUTH;
    
    
                    return dir;
    
    
                    }
    
    
                    if(dir == SOUTH){
                            if(ch == 'a')
                                    dir = EAST;
                            if(ch == 's')
                                    dir = NORTH;
                            if(ch == 'd')
                                    dir = WEST;
    
    
                    return dir;
    
    
                    }
    
    
    
    
    }
    
    
    void bordercheck(int *x, int *y){
    
    
            if(*x >= 16){
                    *x = *x - 1;
            }
    
    
            if(*y >= 16){
                    *y = *y - 1;
            }
    
    
            if(*x < 0){
                    *x = *x + 1;
            }
    
    
            if(*y < 0){
                    *y = *y + 1;
            }
    
    
    
    
    }
    Also, the header file for display.h is:
    Code:
    /* NOTE: this file is under construction  *//*  This file contains the prototypes for useful funcitons in the display.o	library  */void init_display(int level);/* This function initializes the display environment and clears the screen    and draws the template maze from the local file maze.template.  The   parameter, level, determines the level of difficulty (see the spec).   For Level 0 and 1 it also initializes the maze from the local file   maze.dat. */void show_mouse(int dir, int row, int col);/* This function shows the mouse at the row and col position headed in   the direction dir. Available at all levels. */void write_message(char str[], int line);/* This function writes the given message at one of the lines (0 to 10)   on the bottom of the screen.  The string is truncated at 64 characters.   Available at all levels. */void clear_screen(void);/* This function clears the screen and shuts down the display environment   Available at all levels. */void show_position(int y, int x);/*  This functions displays the position under the X and Y on the right    of the screen.  Available at all levels. */void show_cells(int cells);/*  This function displays the number of cells explored under CELLS on the    right of the screen.  Available at Level 1 and higher. */void show_unique_cells(int cells);/*  This function displays the number of unique cells explored under    UNIQUE CELLS on the right of the screen. Available at Level 2 or higher. */void mouse_cam(int left, int front, int right);/*  This functions displays the mouse view of left, front and side walls     under MOUSE CAM on the right side of the screen. Available at all     levels. */void lvl0_get_walls(int y, int x, int *North, int *East, int *West, int *South);/*  This function indirectly returns the walls (1 for present, 0 of absent)    in the cell y, x from the internal maze.  Avaliable only at Level 0    or 1. */void put_walls(int y, int x, int North, int East, int West, int South);/*  This function shows the given North, East, West and South walls    (1 for present, 0 for absent) for the cell y, x on the screen.     Avaliable only at Level 1 or higher. */

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Your are doing the polling for the direction, then moving the player, then doing the crashtest.

    You need to get the direction, do the crashtest, then move the player only if he doesn't crash.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Thanks for the help! I figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-08-2011, 01:10 PM
  2. Desktop walls
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-03-2005, 05:36 PM
  3. Maze walls???
    By goofprogram in forum C Programming
    Replies: 8
    Last Post: 02-19-2002, 02:00 AM
  4. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 03:27 AM

Tags for this Thread