aboutsummaryrefslogtreecommitdiff
path: root/projects/09/K
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-09 09:27:11 +0100
committerYuchen Pei <me@ypei.me>2018-01-09 09:27:11 +0100
commit8d610ca02dc7b76238fb582b1dac148505e38c05 (patch)
treedd14e609e72da9c40677f56c784c422e207b6893 /projects/09/K
parente2cc55782b6036ee31be39edefd77b8e39ac9369 (diff)
finished random number generator
- used the simple method here: https://www.coursera.org/learn/nand2tetris2/discussions/weeks/3/threads/LiyUrABwEee0IhLPmsG3Vg/replies/1MWytACWEeeMVQ6gf1FxsA - game crashes around turn 192. will investigate that after learning about the compiler
Diffstat (limited to 'projects/09/K')
-rw-r--r--projects/09/K/Board.jack54
-rw-r--r--projects/09/K/Board.vm149
2 files changed, 173 insertions, 30 deletions
diff --git a/projects/09/K/Board.jack b/projects/09/K/Board.jack
index cea3ae6..49d42d5 100644
--- a/projects/09/K/Board.jack
+++ b/projects/09/K/Board.jack
@@ -1,6 +1,7 @@
class Board {
field Array grid;
field int nTurn;
+ field int seed;
constructor Board new() {
var int i, j;
@@ -20,6 +21,7 @@ class Board {
let t = grid[0];
let t[0] = 65;
let nTurn = 0;
+ let seed = 0;
return this;
}
@@ -195,33 +197,43 @@ class Board {
method void addTile(){
var Array t;
+ var int r, c, parity, newTile;
+ /*
let t = grid[1];
if (t[1] = 32) {
let t[1] = 65;
}
- return;
- /*
- var int x, y, r, c;
- var char z;
- var Array t;
- let x = nTurn;
- let y = 23 * x * x + 79;
- let x = y - (y / 16 * 16);
- let r = x / 4;
- let c = x - (r * 4);
- let z = 65;
+ */
+ let seed = seed * 25173 + 13849;
+ if (seed < 0) {
+ let seed = - seed;
+ }
+
+ if (seed - (seed / 2 * 2) = 0) {
+ let parity = 1;
+ } else {
+ let parity = -1;
+ }
+
+ let seed = seed - (seed / 16 * 16);
+ let r = seed / 4;
+ let c = seed - (4 * r);
let t = grid[r];
- while (t[c] > 64) {
- let y = 23 * x * x + 79;
- let x = y - (y / 16 * 16);
- let r = x / 4;
- let c = x - (r * 4);
- let z = 131 - z;
+ let newTile = 65;
+
+ while (t[c] > 64){
+ let seed = seed + parity;
+ if (seed < 0) {
+ let seed = 15;
+ }
+ let seed = seed - (seed / 16 * 16);
+ let r = seed / 4;
+ let c = seed - (4 * r);
let t = grid[r];
+ let newTile = 131 - newTile;
}
- let t[c] = z;
+ let t[c] = newTile;
return;
- */
}
method void transform(char dir){
@@ -279,6 +291,10 @@ class Board {
do Output.printString("|");
let i = i + 1;
}
+
+ do Output.moveCursor(r + 6, c - 2);
+ do Output.printString("Turn: ");
+ do Output.printInt(nTurn);
return;
}
diff --git a/projects/09/K/Board.vm b/projects/09/K/Board.vm
index ce1e791..3b5983c 100644
--- a/projects/09/K/Board.vm
+++ b/projects/09/K/Board.vm
@@ -1,5 +1,5 @@
function Board.new 3
-push constant 2
+push constant 3
call Memory.alloc 1
pop pointer 0
push constant 4
@@ -72,6 +72,8 @@ push temp 0
pop that 0
push constant 0
pop this 1
+push constant 0
+pop this 2
push pointer 0
return
function Board.arrange 0
@@ -894,34 +896,132 @@ return
label IF_FALSE6
push argument 1
return
-function Board.addTile 1
+function Board.addTile 5
push argument 0
pop pointer 0
+push this 2
+push constant 25173
+call Math.multiply 2
+push constant 13849
+add
+pop this 2
+push this 2
+push constant 0
+lt
+if-goto IF_TRUE0
+goto IF_FALSE0
+label IF_TRUE0
+push this 2
+neg
+pop this 2
+label IF_FALSE0
+push this 2
+push this 2
+push constant 2
+call Math.divide 2
+push constant 2
+call Math.multiply 2
+sub
+push constant 0
+eq
+if-goto IF_TRUE1
+goto IF_FALSE1
+label IF_TRUE1
+push constant 1
+pop local 3
+goto IF_END1
+label IF_FALSE1
push constant 1
+neg
+pop local 3
+label IF_END1
+push this 2
+push this 2
+push constant 16
+call Math.divide 2
+push constant 16
+call Math.multiply 2
+sub
+pop this 2
+push this 2
+push constant 4
+call Math.divide 2
+pop local 1
+push this 2
+push constant 4
+push local 1
+call Math.multiply 2
+sub
+pop local 2
+push local 1
push this 0
add
pop pointer 1
push that 0
pop local 0
-push constant 1
+push constant 65
+pop local 4
+label WHILE_EXP0
+push local 2
push local 0
add
pop pointer 1
push that 0
-push constant 32
-eq
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push constant 1
+push constant 64
+gt
+not
+if-goto WHILE_END0
+push this 2
+push local 3
+add
+pop this 2
+push this 2
+push constant 0
+lt
+if-goto IF_TRUE2
+goto IF_FALSE2
+label IF_TRUE2
+push constant 15
+pop this 2
+label IF_FALSE2
+push this 2
+push this 2
+push constant 16
+call Math.divide 2
+push constant 16
+call Math.multiply 2
+sub
+pop this 2
+push this 2
+push constant 4
+call Math.divide 2
+pop local 1
+push this 2
+push constant 4
+push local 1
+call Math.multiply 2
+sub
+pop local 2
+push local 1
+push this 0
+add
+pop pointer 1
+push that 0
+pop local 0
+push constant 131
+push local 4
+sub
+pop local 4
+goto WHILE_EXP0
+label WHILE_END0
+push local 2
push local 0
add
-push constant 65
+push local 4
pop temp 0
pop pointer 1
push temp 0
pop that 0
-label IF_FALSE0
push constant 0
return
function Board.transform 4
@@ -1138,6 +1238,33 @@ add
pop local 2
goto WHILE_EXP0
label WHILE_END0
+push local 0
+push constant 6
+add
+push local 1
+push constant 2
+sub
+call Output.moveCursor 2
+pop temp 0
+push constant 6
+call String.new 1
+push constant 84
+call String.appendChar 2
+push constant 117
+call String.appendChar 2
+push constant 114
+call String.appendChar 2
+push constant 110
+call String.appendChar 2
+push constant 58
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+call Output.printString 1
+pop temp 0
+push this 1
+call Output.printInt 1
+pop temp 0
push constant 0
return
function Board.dispose 0