Author Topic: Code crashing calc  (Read 32899 times)

0 Members and 1 Guest are viewing this topic.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Code crashing calc
« 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);
    }
  }
}
« Last Edit: July 28, 2010, 04:45:08 pm by fb39ca4 »

Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: Code crashing calc
« Reply #1 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++)?

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Code crashing calc
« Reply #2 on: July 28, 2010, 05:59:46 pm »
I didn't think it would be a difference. Even so, the calc still crashes with <=.

Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: Code crashing calc
« Reply #3 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.

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Code crashing calc
« Reply #4 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! :)


Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: Code crashing calc
« Reply #5 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.
« Last Edit: July 28, 2010, 08:19:45 pm by Madskillz »

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Code crashing calc
« Reply #6 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.
« Last Edit: July 28, 2010, 09:56:58 pm by apcalc »


Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: Code crashing calc
« Reply #7 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.
« Last Edit: July 28, 2010, 11:03:32 pm by Madskillz »

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Code crashing calc
« Reply #8 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 site.
« Last Edit: July 29, 2010, 04:42:41 am by fb39ca4 »

Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: Code crashing calc
« Reply #9 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.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Code crashing calc
« Reply #10 on: July 29, 2010, 10:18:30 am »
I defined random() myself, because I was having trouble with the current rand() function.

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Code crashing calc
« Reply #11 on: July 29, 2010, 10:21:11 am »
Could you post the code to your random() function?


Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Code crashing calc
« Reply #12 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.

Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: Code crashing calc
« Reply #13 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.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Code crashing calc
« Reply #14 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,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,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,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,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,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,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,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