Error while running VM code in VMEmulator

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error while running VM code in VMEmulator

sarthak
Here is my code for the snake game. snake.zip
When the snake reaches its food (which is just a black pixel, near the middle of the right side of the screen on VM Emulator) the code shows the following error:
'That' segment must be in the Heap or Screen range in snake.eat.19
It is pointing to some error in in the method eat inside the snake class. How to get rid of this error
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error while running VM code in VMEmulator

cadet1620
Administrator
sarthak wrote
Here is my code for the snake game. snake.zip
When the snake reaches its food (which is just a black pixel, near the middle of the right side of the screen on VM Emulator) the code shows the following error:
'That' segment must be in the Heap or Screen range in snake.eat.19
It is pointing to some error in in the method eat inside the snake class. How to get rid of this error
The "that" segment is used by the Jack compiler to access arrays. Look for an array that has not been allocated. Since the problem is in VM line 19 of snake.eat(), it's going to be an early line in that routine.

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error while running VM code in VMEmulator

sarthak
I removed the above error.
Now in the eat method, I am copying the values of x array into a new array cpy then I dispose and then re-create array x with length increased (length = length + 1) but when I copy the x array back from cpy I am getting wrong array for x. All values in x array are 0.Same is true for y and Mov arrays also.
Eat method will be executed if we reach the food pixel near the right side of the screen on VM Emulator.
snake.zip
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error while running VM code in VMEmulator

cadet1620
Administrator
My eyes are bleary tonight; looking at your code I don't see what's causing the new arrays to all be 0.

Here are some things that you can do that will make your life easier and may help you find the bug.

You are doing many array size increases and they all have many array copies in them. Write growArray() and copyArray() methods so that the logic in eat() is easier to see. (They can be functions in class snake or in a new class ArrayUtils.)

Write a DEBUG_printArray() function so that the debug code doesn't clutter eat().  I like to name debug only code DEBUG_whatever, and tab it way to the right so that it is easy to ignore and easy to delete when I no longer need it.

And I see a bug you'll hit after getting eat() working:  don't call fd.dispose(). You need the fd object to call fd.placeFood().

My version would look something like this:
  //--------------------------------------------------------------------------------
  // Grow Array 'a' by inserting a 0 at the beginning.  'a' is 'len' long.
  // Returns a new Array 'len'+1 long.  'a' is disposed.

  function Array growArray(Array a, int len)
  {
    var Array newA;
    let newA = Array.new(len+1);
    do snake.copyArray(newA, 1, a, 0, len);
    do a.dispose();
    return newA;
  }

  //--------------------------------------------------------------------------------
  // Copy 'len' words from Array 'src' starting at [srcIndex] to
  // Array 'dest' starting at [destIndex].
  // The data being copied must not overlap.

  function void copyArray(Array dest, int destIndex, Array src, int srcIndex, int len)
  {
    var int i;      // Initialized to 0.
    while (len > 0) {
      let dest[destIndex+i] = src[srcIndex+i];
      let i = i+1;
      let len = len-1;
    }
    return;
  }

  //--------------------------------------------------------------------------------
  // DEBUG routine to print an Array.
  // 'title' MUST BE a string constant; it will be disposed!

  function void DEBUG_printArray(Array a, int len, String title)
  {
    var int i;      // Initialized to 0;
    do Output.printString(title);
    do Output.println();
    while (i < len){
      do Output.printInt(a[i]);
      do Output.printString(", ");
      let i = i+1;
    }
    do title.dispose();         // Prevent string constant memory leak.
    return;
  }

  //--------------------------------------------------------------------------------
  // Comment about how eat() works...

  method void eat()
  {
                                    do snake.DEBUG_printArray(x, length, "Initial array x is:");
                                    do snake.DEBUG_printArray(y, length, "Initial array y is:");
                                    do snake.DEBUG_printArray(Mov, length, "Initial array Mov is:");
    let x = snake.growArray(x, length);
    let y = snake.growArray(y, length);
    let Mov = snake.growArray(Mov, length);
    let length = length + 1;

    let Mov[0] = Mov[1];
    if (Mov[0] = 1){
      let x[0] = x[1] + 1;
      let y[0] = y[1];
    }
    if (Mov[0] = 2){
      let x[0] = x[1] - 1;
      let y[0] = y[1];
    }
    if (Mov[0] = 3){
      let y[0] = y[1] + 1;
      let x[0] = x[1];
    }
    if (Mov[0] = 3){
      let y[0] = y[1] - 1;
      let x[0] = x[1];
    }
                                    do snake.DEBUG_printArray(x, length, "Initial array x is:");
                                    do snake.DEBUG_printArray(y, length, "Initial array y is:");
                                    do snake.DEBUG_printArray(Mov, length, "Initial array Mov is:");
    return;
  }
(This code has been compiled, but not tested beyond that.)

--Mark







Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error while running VM code in VMEmulator

cadet1620
Administrator
In reply to this post by sarthak
Took another look at your code before heading to bed and see this problem that might explain strange loop behavior:
    let count = 1;
>>>   while(count < length + 1){   <<<
      let x[count] = cpy[count-1];
      let count = count + 1;
    }
Jack language has undefined operator precedence. This needs to be written:
    while(count < (length + 1)){
so that the compiler doesn't do the compare and then add 1 to the compare's result.

--Mark
Loading...