+/TECS
git clone https://git.julia.blue/+/TECS
0f59f13
v1.0
1class Board { 2 field int x, y, width, height, size; 3 field Array matrix, next; 4 5 constructor Board new(int s){ 6 var int i; 7 let width = 512/s; 8 let x = (512-(width*s)) / 2; //marginally faster than calling Util.mod() 9 let height = 256/s; 10 let y = (256-(height*s)) / 2; 11 let i = 0; 12 let size = s; 13 do graphicsInit(); 14 let matrix = Array.new(width); 15 let next = Array.new(width); 16 while(i<width){ 17 let matrix[i] = Array.new(height); 18 let next[i] = Array.new(height); 19 let i = i+1; 20 } 21 return this; 22 } 23 24 method int getX(){ 25 return x; 26 } 27 28 method int getY(){ 29 return y; 30 } 31 32 method int getWidth(){ 33 return width; 34 } 35 36 method int getHeight(){ 37 return height; 38 } 39 40 method int getSize(){ 41 return size; 42 } 43 44 method boolean get(int a, int b){ 45 var Array temp; 46 let temp = matrix[a]; 47 return temp[b]; 48 } 49 50 method void set(int a, int b, boolean c){ 51 var Array temp; 52 let temp = next[a]; 53 let temp[b] = c; 54 return; 55 } 56 57/** computes next based on matrix */ 58 method void step(){ 59 var int neighbors, i, iup, idown, j, jup, jdown; 60 let i = 0; 61 let j = 0; 62 while(i<width){ 63 while(j<height){ 64 // inline the neighbor checks and mod computations because it's faster than a loop 65 let neighbors = 0; 66 let iup = Util.mod(i+1+width, width); 67 let idown = Util.mod(i-1+width, width); 68 let jup = Util.mod(j+1+height, height); 69 let jdown = Util.mod(j-1+height, height); 70 // using weakly typed true == -1 71 let neighbors = neighbors - get(iup, jup); 72 let neighbors = neighbors - get(iup, j); 73 let neighbors = neighbors - get(iup, jdown); 74 let neighbors = neighbors - get(i, jup); 75 let neighbors = neighbors - get(i, jdown); 76 let neighbors = neighbors - get(idown, jup); 77 let neighbors = neighbors - get(idown, j); 78 let neighbors = neighbors - get(idown, jdown); 79 if(get(i,j)){ 80 if((neighbors<2) | (neighbors>3)){ 81 do set(i, j, false); 82 } 83 } 84 else{ 85 if(neighbors = 3){ 86 do set(i, j, true); 87 } 88 } 89 let j = j+1; 90 } 91 let j = 0; 92 let i = i+1; 93 } 94 return; 95 } 96 97/** copies next into matrix */ 98 method void flip(){ 99 var int i, j; 100 var Array temp, tempNext; 101 let i = 0; 102 let j = 0; 103 while(i<width){ 104 let temp = matrix[i]; 105 let tempNext = next[i]; 106 while(j<height){ 107 let temp[j] = tempNext[j]; 108 //let tempNext[j] = 0; 109 let j = j+1; 110 } 111 let j = 0; 112 let i = i+1; 113 } 114 return; 115 } 116 117 method void clear(){ 118 var int i, j; 119 let i=0; 120 let j=0; 121 while(j<height){ 122 while(i<width){ 123 do set(i, j, false); 124 let i = i+1; 125 } 126 let j = j+1; 127 let i = 0; 128 } 129 do flip(); 130 return; 131 } 132 133 method void graphicsInit(){ 134 //add black border 135 do Screen.setColor(true); 136 do Screen.drawRectangle(0, 0, 511, 255); 137 do Screen.setColor(false); 138 do Screen.drawRectangle(x, y, x+(width*size)-1, y+(height*size)-1); 139 return; 140 } 141 142 method void draw(){ 143 var int i, j, temp; 144 let i=0; 145 let j=0; 146 while(j<height){ 147 while(i<width){ 148 let temp = matrix[i]; 149 do Screen.setColor(temp[j]); 150 do Screen.drawRectangle(x+(i*size), y+(j*size), x+((i+1)*size)-1, y+((j+1)*size)-1); 151 let i = i+1; 152 } 153 let j = j+1; 154 let i = 0; 155 } 156 return; 157 } 158 159 method void dispose(){ 160 do Util.dispose2DArray(matrix, width); 161 do Util.dispose2DArray(next, width); 162 return; 163 } 164}