c - My code for solving a maze goes into an infinite loop -


currently, have maze solving algorithm, , goes inifinite loop reason, have been on hours, can't seem figure out. doing wrong. going post of other necessary functions "could" poential problem along maze solving code.

main.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "mazegen.h" #define  maxsize 100  int main(int argc, char**argv) {     char*readfile;     char maze[maxsize][maxsize];     int rows=0;     int cols=0;     int x;     int y;     file*fp;     int counter;     int length=0;     counter = 0;      fp = fopen(argv[1], "r");      if(fp == null)     {        printf("cannot open file\n");        exit(0);     }      readfile = malloc(sizeof(char)*maxsize);        while(fgets(readfile,maxsize,fp) != null)     {         for(cols=0; cols <maxsize; cols++)          {             maze[rows][cols] = readfile[cols];         }         rows++;          counter++;        }      fclose(fp);      length = strlen(readfile);      mazesolution(maze,counter, length);       for(x=0; x<rows; x++)     {        for(y=0; y<cols; y++)        {             printf("%c", maze[x][y]);        }     }       free(readfile);     return 0; } 

mazesolve.c

#include "stack.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define  buffersize 500 #define  flag   void mazesolution(char maze[100][100], int counter, int counter2) {    stack*currentcell;     int i;    int j;     currentcell = create();      /*push(currentcell, 1,2);    push(currentcell, 3,4);    pop(currentcell);    printstack(currentcell);*/     for(i=0; i<counter; i++)    {        for(j=0; j<counter2; j++)        {            if(maze[i][j] == 's')            {                push(currentcell,i,j);                = counter;                break;           }          }    }       while(currentcell != null)    {        if(maze[i][j] == 'f')       {            break;       }        if (maze[i][j] == ' ')        {           maze[i][j] = '.';       }        if (i>0 && maze[i-1][j] == ' ')        {           push(currentcell,i-1, j);           i--;       }        else if (j>0 && maze[i][j-1] == ' ')        {           push(currentcell,i, j-1);           j--;       }        else if (i+1<counter && maze[i+1][j] == ' ')        {           push(currentcell,i+1, j);           i++;        }         else if (j+1<counter2 && maze[i][j+1] == ' ')         {           push(currentcell,i, j+1);           j++;        }         else         {           pop(currentcell);        }    }     if (listlength(currentcell->list) > 0)    {        printstack(currentcell);    }     else     {         printf("no solution\n");     }  } 

stack.c

#include "stack.h" #include <stdio.h> #include <stdlib.h>  stack*create() {     stack*mystack;      mystack = malloc(sizeof(stack)+1);      mystack->list = createlist();      return mystack; }  node*push(stack*thestack, int xpos, int ypos) {    node*top;     if(thestack == null)    {        printf("empty stack.error\n");        exit(0);    }     top = addfront(thestack->list, xpos, ypos);     return top; }  void pop (stack*thestack) {     node*thehead;      if(thestack == null)     {         printf("empty stack. error\n");          exit(0);     }      thehead = removefromfront(thestack->list);      thestack->list = thehead;   }  void peek (stack*thestack) {     getfromfront(thestack->list); }   void printstack(stack*thestack) {     if(thestack == null)     {         printf("error empty stack. cannot print stack\n");         exit (1);     }     printlist(thestack->list);  } void destroystack(stack*thestack) {     destroylist(thestack->list);      free(thestack);  } linkedlist.c  #include "linkedlist.h" #include <stdio.h> #include <stdlib.h>   node*createlist() {     node*dummynode;       dummynode = malloc(sizeof(node));      dummynode->next = null;      return dummynode;  }  node*addfront(node*list, int xpos, int ypos) {    node*newnode;     if(list == null)    {        printf("add front error\n");        exit(0);    }     newnode = initnode(xpos, ypos);     newnode->next = list->next;     list->next = newnode;     return list; }  void printlist(node*thelist) {     node*currentposition;      currentposition = thelist->next;      while(currentposition != null)     {         printf("this value of current node : %d,%d\n",currentposition->xpos, currentposition->ypos);          currentposition= currentposition->next;     }  }  node*initnode(int xpos, int ypos) {     node*newnode;      newnode = malloc(sizeof(node));      newnode->xpos = xpos;      newnode->ypos = ypos;      newnode->next = null;      return newnode;  }  void destroylist(node*thelist) {     node*temp;      while(thelist->next != null)     {         temp = thelist->next;         free(thelist);         thelist=temp;     } }  node*removefromfront(node*thelist)  {     node*temp;      if (thelist == null)      {         printf("error\n");         return null;     }      temp = thelist->next;     thelist->next = null;      return temp; }  int listlength(node*list) {     int length;     node*ptr;     length = 0;      ptr = list->next;      while(ptr != null)     {         length++;         ptr = ptr->next;     }       printf("this length of list : %d\n",length);      return length; }  void getfromfront(node*thelist) {       node*temp;      int value1;      int value2;      temp = thelist->next;       if(temp == null)      {          printf("empty list.\n");          exit(0);       }      value1 = thelist->next->xpos;      value2 = thelist->next->ypos;      printf("this value front : %d,%d\n", value1, value2);   } 

looks may several things: while loops. while loops use expression

while (<insert code> != null) { <insert body of code> } 

using != null expression tricky @ times because unless condition true, continue going in infinite loop. noticed in code, don't have kind of exit statement in while loops. in experience while loops main causes of infinite loops. doesn't seem condition ever false (= null). example:

while(fgets(readfile,maxsize,fp) != null) {     for(cols=0; cols <maxsize; cols++)      {         maze[rows][cols] = readfile[cols];     }     rows++;      counter++;    }  

if fgets method not become null, never exit while loop. did not post fgets method , i'm afraid can't whether or not that's problem located. same concept true second while loop.

a strategy use when debugging infinite loops having system.out.println (i'm used java haha), inside while loop. doing allows me see computer doing inside while loop.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -