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