Thread: Maze Solving Cont....PLZ

  1. #16
    Registered User
    Join Date
    Nov 2005
    Posts
    15
    ok..i have onyl been doing c for a week.. and not much.. this is really the first time i have sat down and started programming.

    is the following right....

    i think i should scan in my file using %x, so that it is in hex form...then i can & it with S_Wall..(S_Wall being 0x8)..

    ????

    i have been trying to & my starting point sqaure with S_Wall.. this will tell me whether south is a wall or not..

    i want 4 if statements for the 4 directions.

    i have done this but i know it is wrong..


    if
    (maze[xstart][ystart] & S_Wall) = = 0
    move to [start-1][ystart] (move in the south dircection)

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    15
    sorry that last line is:
    move to [start+1][ystart] (move in the south dircection)

  3. #18
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Hex, binary, or decimal does not matter. Bit shifting and bit masks work the binary representation of the value. It's base 2. Hex is base 16, octal is base 8, and decimal is base 10. So how you load the data make's no matter. It's still base 2.

  4. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    15
    no..it is
    move to [start][ystart+1] (move in the south dircection)is it not????

  5. #20
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ok...ok...I will resist the urge to post about using a linear array here. I think I've already confused enough people on the game programming board with those types of posts.

    Do what all of us said and your code will work. - perhaps a new sig for me eh

    I leave this thread in the very capable hands of my partners in crime of cprog.

  6. #21
    Registered User
    Join Date
    Nov 2005
    Posts
    15

    code so far

    here is my whole prog so far... im sure you can see what it does...
    can you tell me where i ma going wrong (probably everywhere)
    Code:
    #include<stdio.h>
    int main(void)
    {
    int maze[10][10];
    int nrow,ncolumn,xstart,ystart,xend,yend,i,j;
    FILE *fp;
    
    fp=fopen("Input.txt","r");
    if (fp == NULL)
    {
    printf("Cannot open Input.txt\n");
    exit(1);
    }
    fscanf(fp,"%d %d",&nrow,&ncolumn);
    fscanf(fp,"%x %x",&xstart,&ystart);
    fscanf(fp,"%d %d",&xend,&yend);
    
    for (i=0; i<(nrow); i++)
    {
    for(j=0; j<(ncolumn); j++)
    {
    fscanf(fp,"%d",&maze[j][i]);
    }
    }
    close(fp);
    printf("%x",maze[xstart][ystart]);
    }
    /*FROM HERE IT IS NOT WORKING*/
    void function(int xstart, int ystart, int maze int tester)
    {
    #define W_EAST  0x1
    #define W_SOUTH 0x2
    #define W_WEST  0x4
    #define W_NORTH 0x8
    #define CHECKED 0x10
    
    (maze[xstart][ystart] & W_NORTH)
    }
    thanks to all for the time you are putting in to help me

    jonny

  7. #22
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    (maze[xstart][ystart] & W_NORTH)

    what do you suppose you are doing with that? Does that solve any task you had in mind? Maybe its there for looks?

    Oh I just thought of a useful bit of information I learned with I was 15 going through a huge bush maze in a girls pants. If you always turn right, the end will be near. Use that information to your advantage my friend.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #23
    Registered User
    Join Date
    Nov 2005
    Posts
    15
    i thought it would tell me whether there is a wall or not..
    i thought it did the following
    say maze[xstart][ystart] is 6
    in binary this is 0 1 1 0
    i want to & this with 1 0 0 0 (north wall)
    then the result of this will be 0 0 0 0 which will tell ne that there isnt a wall at north so i can do something like move to maze[xstart][ystart+1] and do the loops again.

    how would i go about the above

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You will want to set up movement checks. If you want to move a direction, see if that wall is closed or not, then see if that direction is in bounds of the array. If it is, then do the move.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by jonnybgood
    i thought it would tell me whether there is a wall or not..
    i thought it did the following
    say maze[xstart][ystart] is 6
    in binary this is 0 1 1 0
    i want to & this with 1 0 0 0 (north wall)
    then the result of this will be 0 0 0 0 which will tell ne that there isnt a wall at north so i can do something like move to maze[xstart][ystart+1] and do the loops again.

    how would i go about the above
    what I meant was...its not doing a damn thing. perhaps putting it in an if statement would get you the results..

    Code:
    if( whatever & 6 )
    {
       // DO SOMETHING
    }
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  11. #26
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    (maze[xstart][ystart] & W_NORTH)
    & is an operator...like +. You wouldn't have a line of code like:
    Code:
    4 + 3;
    would you? I mean...it's doing nothing. It's adding the numbers and the result is just left in limbo. You need to use the result somewhere, somehow.
    If you understand what you're doing, you're not learning anything.

  12. #27
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's a generic solve-the-maze type program that mostly follows your guidelines. The input file is a bit different (it's in hex), but maybe it will help. Let me know if you have any questions about it at all because understanding is the most important thing:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define W_EAST  0x1
    #define W_SOUTH 0x2
    #define W_WEST  0x4
    #define W_NORTH 0x8
    #define CHECKED 0x10
    
    unsigned char **maze = NULL;
    int dimx, dimy;
    int curx, cury;
    int goalx, goaly;
    
    char *dirs = NULL;
    int curdir;
    
    void read_file(void)
    {
      char *vals = "0123456789ABCDEF";
      char buf[4096];
      FILE *fp;
      int i, j;
    
      if(!(fp = fopen("maze.txt", "r")))
      {
        puts("Couldn't open maze.txt for reading!");
        exit(EXIT_FAILURE);
      }
    
      fgets(buf, sizeof(buf), fp);
      sscanf(buf, "%d %d", &dimx, &dimy);
      fgets(buf, sizeof(buf), fp);
      sscanf(buf, "%d %d", &curx, &cury);
      fgets(buf, sizeof(buf), fp);
      sscanf(buf, "%d %d", &goalx, &goaly);
    
      printf("Maze is %d by %d. Attempt to go from %d,%d to %d,%d\n",
        dimx, dimy, cury, curx, goaly, goalx);
    
      if(!(maze = malloc(sizeof(char *) * dimy)))
      {
        puts("Memory allocation error!");
        exit(EXIT_FAILURE);
      }
    
      for(i = 0;i < dimy;++i)
      {
        if(!(maze[i] = malloc(dimx)))
        {
          puts("Memory allocation error!");
          exit(EXIT_FAILURE);
        }
    
        fgets(buf, sizeof(buf), fp);
    
        for(j = 0;j < dimx;++j)
          maze[i][j] = strchr(vals, buf[j]) - vals;
      }
    
      fclose(fp);
    }
    
    void print_maze(void)
    {
      int i, j;
    
      for(i = 0;i < dimy;++i)
      {
        for(j = 0;j < dimx;++j)
          printf("%X ", maze[i][j]);
        putchar('\n');
      }
    }
    
    int try_coord(int y, int x)
    {
      unsigned char val = maze[y][x];
    
      maze[y][x] |= CHECKED;
    
      if(y == goaly && x == goalx)
        return 1;
    
      if(!(val & W_NORTH) && !(maze[y - 1][x] & CHECKED))
        if(try_coord(y - 1, x))
        {
          dirs[curdir++] = 'N';
          return 1;
        }
      if(!(val & W_SOUTH) && !(maze[y + 1][x] & CHECKED))
        if(try_coord(y + 1, x))
        {
          dirs[curdir++] = 'S';
          return 1;
        }
      if(!(val & W_EAST) && !(maze[y][x + 1] & CHECKED))
        if(try_coord(y, x + 1))
        {
          dirs[curdir++] = 'E';
          return 1;
        }
      if(!(val & W_WEST) && !(maze[y][x - 1] & CHECKED))
        if(try_coord(y, x - 1))
        {
          dirs[curdir++] = 'W';
          return 1;
        }
    
      return 0;
    }
    
    void print_solution(void)
    {
      puts("\nSolution:");
      while(--curdir > -1)
        printf("%c ", dirs[curdir]);
      putchar('\n');
    }
    
    int main(void)
    {
      read_file();
    
      if(!(dirs = calloc(1, dimx * dimy)))
      {
        puts("Memory allocation error!");
        return 0;
      }
      curdir = 0;
    
      print_maze();
    
      if(!try_coord(cury, curx))
        puts("No solution found");
      else
        print_solution();
    
      return EXIT_SUCCESS;
    }
    Running it I get:
    Code:
    Maze is 5 by 5. Attempt to go from 4,0 to 1,4
    D C 8 A B
    5 7 5 C B
    4 A 1 5 F
    5 C 1 6 9
    7 7 6 A 3
    
    Solution:
    N N E E S S E E N W N N E
    I didn't free the memory I allocated which makes me a bad person. Don't follow that bad example
    Last edited by itsme86; 11-10-2005 at 05:07 PM.
    If you understand what you're doing, you're not learning anything.

  13. #28
    Registered User
    Join Date
    Nov 2005
    Posts
    15
    id ask you to go through the whole thing if you wouldnt mind....
    i cant really make sense of it...i will go through it slowly but it would help a great deal if you could possibly go through it and comment areas..

    thanks a huge amount.

  14. #29
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Why do you have to use all this bitwise operation bologna? If your maze looks like this:

    1111111111
    1000000001
    1111101011
    1000001000
    1010101111
    1011111111

    Why would you need to know if north south east west were open from the current room?

    East -> PlayerColumn+1
    West -> PlayerColumn-1
    North -> PlayerRow-1
    South -> PlayerRow+1

    Code:
    //Returns true on SUCCESS
    
    bool CheckMove(int rowoffset,int colofset,bool makethemove)
    {
      //Newrow and newcol represent what maze cell we are testing
      int newrow=rowoffset+PlayerRow;
      int newcol=coloffset+PlayerCol;
    
      //Bounds check the new cell against the array
      if (newrow>MazeHeight  || newrow<0 ) return false;
      if (newcol>MazeWidth || newcol<0) return false;
    
      //Check the maze
      //If cell is empty and makethemove is true then move player to maze cell
      if (Maze[newrow][newcol]==0)
      {
        if (makethemove)
        {
          PlayerCol=newcol;
          PlayerRow=newrow;
          return true;
        } else return true;
        
      } else return false;
    }
    Same thing as has been posted, just does it in one function.

  15. #30
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where do you check if there's a wall in the way? It's possible to have two adjacent cells that you can enter, but not necessarily directly from one to the other.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solving maze using rec'
    By gavra in forum C Programming
    Replies: 14
    Last Post: 07-13-2008, 09:20 AM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  3. Solving A Maze Using C Language!!!!
    By jonnybgood in forum C Programming
    Replies: 6
    Last Post: 11-08-2005, 12:15 PM
  4. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM