class Board { field int x, y, width, height, size; field Array matrix, next; constructor Board new(int s){ var int i; let width = 512 / s; let x = (512 - (width * s)) / 2; //marginally faster than calling Math.mod() let height = 256/s; let y = (256 - (height * s)) / 2; let i = 0; let size = s; do graphicsInit(); let matrix = Array.new(width); let next = Array.new(width); while(i < width){ let matrix[i] = Array.new(height); let next[i] = Array.new(height); let i = i + 1; } return this; } method int getX(){ return x; } method int getY(){ return y; } method int getWidth(){ return width; } method int getHeight(){ return height; } method int getSize(){ return size; } method boolean get(int a, int b){ var Array temp; let temp = matrix[a]; return temp[b]; } method void set(int a, int b, boolean c){ var Array temp; let temp = next[a]; let temp[b] = c; return; } /** computes next based on matrix */ method void step(){ var int neighbors, i, iup, idown, j, jup, jdown; let i = 0; let j = 0; while(i < width){ while(j < height){ // inline the neighbor checks and mod computations because it's faster than a loop let neighbors = 0; let iup = Math.mod(i + 1 + width, width); let idown = Math.mod(i - 1 + width, width); let jup = Math.mod(j + 1 + height, height); let jdown = Math.mod(j - 1 + height, height); // using weakly typed true == -1 let neighbors = neighbors - get(iup, jup); let neighbors = neighbors - get(iup, j); let neighbors = neighbors - get(iup, jdown); let neighbors = neighbors - get(i, jup); let neighbors = neighbors - get(i, jdown); let neighbors = neighbors - get(idown, jup); let neighbors = neighbors - get(idown, j); let neighbors = neighbors - get(idown, jdown); if(get(i,j)){ if((neighbors < 2) | (neighbors > 3)){ do set(i, j, false); } } else{ if(neighbors == 3){ do set(i, j, true); } } let j = j + 1; } let j = 0; let i = i + 1; } return; } /** copies next into matrix */ method void flip(){ var int i, j; var Array temp, tempNext; let i = 0; let j = 0; while(i < width){ let temp = matrix[i]; let tempNext = next[i]; while(j < height){ let temp[j] = tempNext[j]; let j = j + 1; } let j = 0; let i = i + 1; } return; } method void clear(){ var int i, j; let i = 0; let j = 0; while(j < height){ while(i < width){ do set(i, j, false); let i = i + 1; } let j = j + 1; let i = 0; } do flip(); return; } method void graphicsInit(){ //add black border do Screen.setColor(true); do Screen.drawRectangle(0, 0, 511, 255); do Screen.setColor(false); do Screen.drawRectangle(x, y, x + (width * size) - 1, y + (height * size) - 1); return; } method void draw(){ var int i, j, temp; let i = 0; let j = 0; while(j < height){ while(i < width){ let temp = matrix[i]; do Screen.setColor(temp[j]); do Screen.drawRectangle(x + (i * size), y + (j * size), x + ((i + 1) * size) - 1, y + ((j + 1) * size) - 1); let i = i + 1; } let j = j + 1; let i = 0; } return; } function void dispose2DArray(Array arr, int size){ var int i, temp; let i=0; while(i