# Omnimaga

## Calculator Community => TI Calculators => Calculator C => Topic started by: fb39ca4 on July 28, 2010, 04:44:57 pm

Title: Code crashing calc
Post by: fb39ca4 on July 28, 2010, 04:44:57 pm
I've been working on a 3d maze program that uses bwang's ncaster. Right now, I have finished the code to generate the maze, but it is crashing my calc. Right now, it is just placed inside main(), to test that it doesn't result in any infinite loops.

Here's the code: (WARNING: VERY unoptimmized)
Code: [Select]
`void generate() {  int a, b;  short int maze[11][11];  for (a = 0; a == 10; a++) {    for (b = 0; b == 10; b++) {      maze[a][b] = 15;      if (a == 0)        maze[a][b] += 16;      if (a == 10)        maze[a][b] += 64;      if (b == 0)        maze[a][b] += 32;      if (b == 10)        maze[a][b] += 128;    }  }  int success = 0;  int x = 1;  int y = 1;  char new[4];  while (!isKeyPressed(KEY_NSPIRE_ESC)) {    if (!(maze[x][y] & 0x0010)) {      if ((maze[x][y] & 0x000F) == 15){        new[0] = 1;      }    }    if (!(maze[x][y] & 0x0020)) {      if ((maze[x][y] & 0x000F) == 15){        new[1] = 1;      }    }    if (!(maze[x][y] & 0x0040)) {      if ((maze[x][y] & 0x000F) == 15){        new[2] = 1;      }    }    if (!(maze[x][y] & 0x0080)) {      if ((maze[x][y] & 0x000F) == 15){        new[3] = 1;      }    }    if (new[0] || new[1] || new[2] || new[3]) {      while (!success) {        a = random();         if (new[a]) {          success = 1;          new[0] = new[1] = new[2] = new[3] = 0;        }      }    }    else      if ((maze[x][y] & 0xF000) == 0) {        break;      if ((maze[x][y] & 0x1000)) {        y--;      }      if ((maze[x][y] & 0x2000)) {        x++;      }      if ((maze[x][y] & 0x4000)) {        y++;      }      if ((maze[x][y] & 0x8000)) {        y--;      }      continue;    }    if (a == 0) {      maze[x][y] = ~(maze[x][y] & 0x0001);      y++;      maze[x][y] = ~(maze[x][y] & 0x0004);      maze[x][y] = ~(maze[x][y] & 0x1000);    }    if (a == 1) {      maze[x][y] = ~(maze[x][y] & 0x0002);      x++;      maze[x][y] = ~(maze[x][y] & 0x0008);      maze[x][y] = ~(maze[x][y] & 0x2000);    }    if (a == 2) {      maze[x][y] = ~(maze[x][y] & 0x0004);      y--;      maze[x][y] = ~(maze[x][y] & 0x0001);      maze[x][y] = ~(maze[x][y] & 0x4000);    }    if (a == 3) {      maze[x][y] = ~(maze[x][y] & 0x0008);      x--;      maze[x][y] = ~(maze[x][y] & 0x0002);      maze[x][y] = ~(maze[x][y] & 0x8000);    }  }}`
Title: Re: Code crashing calc
Post by: bwang on July 28, 2010, 04:55:01 pm
Why is your for loop for (a = 0; a == 10; a++)? Shouldn't it be for (a = 0; a <= 10; a++)?
Title: Re: Code crashing calc
Post by: fb39ca4 on July 28, 2010, 05:59:46 pm
I didn't think it would be a difference. Even so, the calc still crashes with <=.
Title: Re: Code crashing calc
Post by: bwang on July 28, 2010, 06:19:48 pm
Do you have a link to the algorithm you're using? The only thing I can think of at the moment would be x, y going out of bounds.
Title: Re: Code crashing calc
Post by: apcalc on July 28, 2010, 06:27:58 pm
In this code:

Code: [Select]
`  int a, b;  short int maze[11][11];  for (a = 0; a == 10; a++) {    for (b = 0; b == 10; b++) {      maze[a][b] = 15;      if (a == 0)        maze[a][b] += 16;      if (a == 10)        maze[a][b] += 64;      if (b == 0)        maze[a][b] += 32;      if (b == 10)        maze[a][b] += 128;    } }`
Could it be that you could be adding to a value that does not yet exist?  I have never used anything like this in C (the values might initilize to 0, I am not sure), but if you are adding, say 16, to an empty variable, it might crash.  If this is the case, just add:

Code: [Select]
`int x,y;for(x=0;x<11;x++) {for(y=0;y<11;y++) {maze[x][y]=0;}}`
I will try to look at the code a little close later if this does not fix the problem.  Good luck! :)
Title: Re: Code crashing calc
Post by: Madskillz on July 28, 2010, 08:19:11 pm
First off check your brackets and make sure they are ending in the right places. I havent had a chance to look at your code. Next thing check the bounds and make sure your not accessing something your not supposed too. If you could try and break it down in parts to make sure this for loop really is being populated correctly etc. Keep doing that until you pinpoint the problem. I'll take a look at your code later tonight if you still dont have it solved.
Title: Re: Code crashing calc
Post by: apcalc on July 28, 2010, 08:23:33 pm
Sorry, I misread your code with this thought to be problem.  My syntax (with spacing) is usually different for what I thought was the error.  Sorry for any inconvenience.
Title: Re: Code crashing calc
Post by: Madskillz on July 28, 2010, 10:49:55 pm
Provided you are calling this method within main, everything looks good at a glance. I dont see anything out of place within your brackets. Break it down like I said to pinpoint the problem.

Code: [Select]
`  for (a = 0; a == 10; a++) {    for (b = 0; b == 10; b++) {      maze[a][b] = 15;      if (a == 0)        maze[a][b] += 16;      if (a == 10)        maze[a][b] += 64;      if (b == 0)        maze[a][b] += 32;      if (b == 10)        maze[a][b] += 128;    }  }`Also it looks like your overwriting the value immediately, though this may be your intention, I'm not sure.
Title: Re: Code crashing calc
Post by: fb39ca4 on July 29, 2010, 04:42:07 am
I've been using visual studio to debug it, and have some errors with the random() function.
Also, if ypu want to see what this is supposed to do, look at this (http://www.mazeworks.com/mazegen/mazetut/index.htm) site.
Title: Re: Code crashing calc
Post by: bwang on July 29, 2010, 04:47:20 am
The Nspire has no random() function; the one in Ndless is called rand().
That could be a possible reason for your crashes. I've experienced crashes when calling non-existent functions before.
Title: Re: Code crashing calc
Post by: fb39ca4 on July 29, 2010, 10:18:30 am
I defined random() myself, because I was having trouble with the current rand() function.
Title: Re: Code crashing calc
Post by: apcalc on July 29, 2010, 10:21:11 am
Could you post the code to your random() function?
Title: Re: Code crashing calc
Post by: fb39ca4 on August 01, 2010, 12:19:54 pm
I've got it working now, after a few hours of debugging, and doing all the bitwise operations with macros, so now all I have to do is export the maze to ncaster format.
Title: Re: Code crashing calc
Post by: bwang on August 01, 2010, 04:03:12 pm
Yay! Now I hope Ncaster can handle the map...the current algorithm doesn't handle mazes well; it was more intended for open areas with structures that extend upward.
Title: Re: Code crashing calc
Post by: fb39ca4 on August 01, 2010, 08:23:10 pm
So will this mean a performance loss? Or is graphical errors? I've been getting some, though I hope it's just my own code.

EDIT:

I've done some more tinkering, and it seems that when the map is this: (also, height is the same, and alt is filled with zeroes)

Code: [Select]
`{{0,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,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};`
grahical errors occur. (namely, nothing gets drawn but the sprites)
Title: Re: Code crashing calc
Post by: bwang on August 02, 2010, 12:26:13 am
Title: Re: Code crashing calc
Post by: fb39ca4 on August 02, 2010, 10:25:13 am
Title: Re: Code crashing calc
Post by: bwang on August 02, 2010, 12:39:10 pm
Title: Re: Code crashing calc
Post by: fb39ca4 on August 02, 2010, 12:50:06 pm
My generate function does not make changes to anything but its internal variables. Aside from calling generate once, the only other change I made was to testlevel.c, as I described above.
Title: Re: Code crashing calc
Post by: bwang on August 02, 2010, 01:06:37 pm
Post testlevel.c.
Title: Re: Code crashing calc
Post by: fb39ca4 on August 02, 2010, 04:40:57 pm
Here it is.
Title: Re: Code crashing calc
Post by: bwang on August 02, 2010, 06:06:14 pm
Your height array is incorrect, I think. The rows have been shifted a bit, so now the spaces on your map are getting the wrong height data.
Title: Re: Code crashing calc
Post by: fb39ca4 on August 03, 2010, 02:45:27 pm
I fixed that, and now I can see the blocks, but the height of the camera looks wrong, and I can't move around. I'm attaching the whole source directory.
Title: Re: Code crashing calc
Post by: bwang on August 03, 2010, 04:14:42 pm
I messed around with your code, and discovered that you are doing something really weird in maze.c, which messes up the external variables in testlevel.c. The Nspire's support for global variables is pretty delicate at the moment, so if something doesn't work, try moving the contents of testlevel.c into main().
You probably want to have generate() take paramenters, so it should look something like:
Code: [Select]
`void generate(int* maze)`where maze is the array you are writing to.
Oh, and random thought: I feel slightly happier about Ncaster now. With a map that dense, I was expecting it to be unbearably slow, but apparently that's not happening :).