From a356d68c1c314cddbd987616f12291fc24bebf37 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 8 Jan 2018 16:22:46 +0100 Subject: project 9 checkpoint - no more compiler error - but still some bugs: - tiles get stuck - segment error (possibly due to deep copy?) --- projects/09/K/Board.jack | 130 ++++-- projects/09/K/Board.vm | 1155 ++++++++++++++++++++++++++++++++++++++++++++++ projects/09/K/KGame.jack | 34 ++ projects/09/K/KGame.vm | 74 +++ projects/09/K/Main.jack | 9 + projects/09/K/Main.vm | 11 + 6 files changed, 1366 insertions(+), 47 deletions(-) create mode 100644 projects/09/K/Board.vm create mode 100644 projects/09/K/KGame.jack create mode 100644 projects/09/K/KGame.vm create mode 100644 projects/09/K/Main.jack create mode 100644 projects/09/K/Main.vm (limited to 'projects/09/K') diff --git a/projects/09/K/Board.jack b/projects/09/K/Board.jack index 9e3678e..c465f95 100644 --- a/projects/09/K/Board.jack +++ b/projects/09/K/Board.jack @@ -3,78 +3,106 @@ class Board { field int nTurn; constructor Board new() { - let grid = Array.new(4); var int i, j; + var Array t; + let grid = Array.new(4); let i = 0; while (i < 4) { let j = 0; let grid[i] = Array.new(4); + let t = grid[i]; while (j < 4) { - let grid[i][j] = 32; + let t[j] = 32; + let j = j + 1; } let i = i + 1; } - let grid[0][0] = 65; + let t = grid[0]; + let t[0] = 65; let nTurn = 0; return this; } method void arrange(Array xs, boolean isHorizontal) { if (isHorizontal) { - let grid = copy(xs) + do deepcopy(xs, grid); } else { - let grid = transpose(xs) + do transpose(xs, grid); } + do dispose4(xs); + return; } - method Array copy(Array xs) { - var Array ys; - let ys = Array.new(4); + method void copy(Array xs, Array ys) { var int i; let i = 0; while (i < 4) { let ys[i] = xs[i]; let i = i + 1; } - return ys; + return; } - method Array deepcopy(Array xs) { - var Array ys; - let ys = Array.new(4); + method void deepcopy(Array xs, Array ys) { var int i; let i = 0; while (i < 4) { - let ys[i] = copy(xs[i]); + do copy(xs[i], ys[i]); let i = i + 1; } - return ys; + return; } - mothod Array transpose(Array xs){ - var Array ys; + method void transpose(Array xs, Array ys) { var int i, j; + var Array t, s; let i = 0; - let ys = Array.new(4); while (i < 4) { - let ys[i] = Array.new(4) let j = 0; + let t = ys[i]; while (j < 4) { - let ys[i][j] = xs[j][i]; + let s = xs[j]; + let t[j] = s[i]; let j = j + 1; } let i = i + 1; } - return ys; + return; + } + + method void dispose4(Array xs){ + var int i; + var Array t; + let i = 0; + while (i < 4) { + let t = xs[i]; + do t.dispose(); + let i = i + 1; + } + do xs.dispose(); + return; + } + + method Array new4(){ + var Array xs; + var int i; + let xs = Array.new(4); + let i = 0; + while (i < 4) { + let xs[i] = Array.new(4); + let i = i + 1; + } + return xs; } - method Array getStrip(boolean isHorizontal){ + method Array getStrips(boolean isHorizontal){ var Array xs; + let xs = new4(); if (isHorizontal) { - let xs = deepcopy(grid); + do deepcopy(grid, xs); } else { - let xs = transpose(grid); + do transpose(grid, xs); } return xs; } @@ -83,9 +111,9 @@ class Board { var int i, j; let i = 0; let j = 0; - if (left = true) { + if (left) { while (i < 4) { - if (xs[i] > 96) { + if (xs[i] > 64) { let xs[j] = xs[i]; let j = j + 1; } @@ -97,7 +125,7 @@ class Board { } } else { while (i < 4) { - if (xs[3 - i] > 96) { + if (xs[3 - i] > 64) { let xs[3 - j] = xs[3 - i]; let j = j + 1; } @@ -112,8 +140,8 @@ class Board { } method Array reduce(Array xs, boolean left){ - if ((xs[0] = xs[1]) & (xs[2] = xs[3])) { - if (left = true) { + if ((xs[0] = xs[1]) & (xs[2] = xs[3]) & (xs[0] > 64) & (xs[2] > 64)) { + if (left) { let xs[0] = xs[0] + 1; let xs[1] = xs[2] + 1; let xs[2] = 32; @@ -126,8 +154,8 @@ class Board { } return xs; } - if (xs[0] = xs[1]) { - if (left = true) { + if ((xs[0] = xs[1]) & (xs[0] > 64)) { + if (left) { let xs[0] = xs[0] + 1; let xs[1] = xs[2]; let xs[2] = xs[3]; @@ -138,8 +166,8 @@ class Board { } return xs; } - if (xs[2] = xs[3]) { - if (left = true) { + if ((xs[2] = xs[3]) & (xs[2] > 64)) { + if (left) { let xs[2] = xs[2] + 1; let xs[3] = 32; } else { @@ -150,14 +178,14 @@ class Board { } return xs; } - if (xs[1] = xs[2]) { - if (left = true) { + if ((xs[1] = xs[2]) & (xs[1] > 64)) { + if (left) { let xs[1] = xs[1] + 1; let xs[2] = xs[3]; let xs[3] = 32; } else { let xs[2] = xs[2] + 1; - let xs[1] = xs[0] + let xs[1] = xs[0]; let xs[0] = 32; } return xs; @@ -166,43 +194,47 @@ class Board { } method void addTile(){ - var int x, y; + 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 = 97; - while (grid[r][c] > 96) { + let z = 65; + 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 = 195 - z; + let z = 131 - z; + let t = grid[r]; } - let grid[r][c] = z; + let t[c] = z; return; } method void transform(char dir){ var boolean isHorizontal, left; - if ((dir = 0) or (dir = 1)) { + var Array xs; + var int i; + if ((dir = 0) | (dir = 1)) { let left = true; } else { let left = false; } - if ((dir = 0) or (dir = 2)) { + if ((dir = 0) | (dir = 2)) { let isHorizontal = true; } else { let isHorizontal = false; } - var Array xs; - let xs = getStrip(isHorizontal); - var int i; + let xs = getStrips(isHorizontal); let i = 0; while (i < 4) { - xs[i] = reduce(align(xs[i], left), left); + let xs[i] = reduce(align(xs[i], left), left); + let i = i + 1; } do arrange(xs, isHorizontal); return; @@ -217,21 +249,25 @@ class Board { method void draw(){ var int r, c, i, j; + var Array t; let r = 9; let c = 30; let i = 0; while (i < 4) { let j = 0; do Output.moveCursor(r + i, c); + let t = grid[i]; while (j < 4) { - do Output.printChar(grid[i][j]); + do Output.printChar(t[j]); let j = j + 1; } let i = i + 1; } + return; } method void dispose() { do Memory.deAlloc(this); + return; } } diff --git a/projects/09/K/Board.vm b/projects/09/K/Board.vm new file mode 100644 index 0000000..398171f --- /dev/null +++ b/projects/09/K/Board.vm @@ -0,0 +1,1155 @@ +function Board.new 3 +push constant 2 +call Memory.alloc 1 +pop pointer 0 +push constant 4 +call Array.new 1 +pop this 0 +push constant 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END0 +push constant 0 +pop local 1 +push local 0 +push this 0 +add +push constant 4 +call Array.new 1 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 0 +push this 0 +add +pop pointer 1 +push that 0 +pop local 2 +label WHILE_EXP1 +push local 1 +push constant 4 +lt +not +if-goto WHILE_END1 +push local 1 +push local 2 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto WHILE_EXP1 +label WHILE_END1 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +push this 0 +add +pop pointer 1 +push that 0 +pop local 2 +push constant 0 +push local 2 +add +push constant 65 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 0 +pop this 1 +push pointer 0 +return +function Board.arrange 0 +push argument 0 +pop pointer 0 +push argument 2 +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push pointer 0 +push argument 1 +push this 0 +call Board.deepcopy 3 +pop temp 0 +goto IF_END0 +label IF_FALSE0 +push pointer 0 +push argument 1 +push this 0 +call Board.transpose 3 +pop temp 0 +label IF_END0 +push pointer 0 +push argument 1 +call Board.dispose4 2 +pop temp 0 +push constant 0 +return +function Board.copy 1 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END0 +push local 0 +push argument 2 +add +push local 0 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +return +function Board.deepcopy 1 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END0 +push pointer 0 +push local 0 +push argument 1 +add +pop pointer 1 +push that 0 +push local 0 +push argument 2 +add +pop pointer 1 +push that 0 +call Board.copy 3 +pop temp 0 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +return +function Board.transpose 4 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END0 +push constant 0 +pop local 1 +push local 0 +push argument 2 +add +pop pointer 1 +push that 0 +pop local 2 +label WHILE_EXP1 +push local 1 +push constant 4 +lt +not +if-goto WHILE_END1 +push local 1 +push argument 1 +add +pop pointer 1 +push that 0 +pop local 3 +push local 1 +push local 2 +add +push local 0 +push local 3 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto WHILE_EXP1 +label WHILE_END1 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +return +function Board.dispose4 2 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END0 +push local 0 +push argument 1 +add +pop pointer 1 +push that 0 +pop local 1 +push local 1 +call Array.dispose 1 +pop temp 0 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push argument 1 +call Array.dispose 1 +pop temp 0 +push constant 0 +return +function Board.new4 2 +push argument 0 +pop pointer 0 +push constant 4 +call Array.new 1 +pop local 0 +push constant 0 +pop local 1 +label WHILE_EXP0 +push local 1 +push constant 4 +lt +not +if-goto WHILE_END0 +push local 1 +push local 0 +add +push constant 4 +call Array.new 1 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto WHILE_EXP0 +label WHILE_END0 +push local 0 +return +function Board.getStrips 1 +push argument 0 +pop pointer 0 +push pointer 0 +call Board.new4 1 +pop local 0 +push argument 1 +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push pointer 0 +push this 0 +push local 0 +call Board.deepcopy 3 +pop temp 0 +goto IF_END0 +label IF_FALSE0 +push pointer 0 +push this 0 +push local 0 +call Board.transpose 3 +pop temp 0 +label IF_END0 +push local 0 +return +function Board.align 2 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +push constant 0 +pop local 1 +push argument 2 +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +label WHILE_EXP0 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END0 +push local 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push local 1 +push argument 1 +add +push local 0 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +label IF_FALSE1 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +label WHILE_EXP1 +push local 1 +push constant 4 +lt +not +if-goto WHILE_END1 +push local 1 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto WHILE_EXP1 +label WHILE_END1 +goto IF_END0 +label IF_FALSE0 +label WHILE_EXP2 +push local 0 +push constant 4 +lt +not +if-goto WHILE_END2 +push constant 3 +push local 0 +sub +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +push constant 3 +push local 1 +sub +push argument 1 +add +push constant 3 +push local 0 +sub +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +label IF_FALSE2 +push local 0 +push constant 1 +add +pop local 0 +goto WHILE_EXP2 +label WHILE_END2 +label WHILE_EXP3 +push local 1 +push constant 4 +lt +not +if-goto WHILE_END3 +push constant 3 +push local 1 +sub +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto WHILE_EXP3 +label WHILE_END3 +label IF_END0 +push argument 1 +return +function Board.reduce 0 +push argument 0 +pop pointer 0 +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +eq +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 3 +push argument 1 +add +pop pointer 1 +push that 0 +eq +and +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push argument 2 +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push constant 0 +push argument 1 +add +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 1 +push argument 1 +add +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 2 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 3 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +goto IF_END1 +label IF_FALSE1 +push constant 3 +push argument 1 +add +push constant 3 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 2 +push argument 1 +add +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 1 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 0 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +label IF_END1 +push argument 1 +return +label IF_FALSE0 +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +eq +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +push argument 2 +if-goto IF_TRUE3 +goto IF_FALSE3 +label IF_TRUE3 +push constant 0 +push argument 1 +add +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 1 +push argument 1 +add +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 2 +push argument 1 +add +push constant 3 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 3 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +goto IF_END3 +label IF_FALSE3 +push constant 1 +push argument 1 +add +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 0 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +label IF_END3 +push argument 1 +return +label IF_FALSE2 +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 3 +push argument 1 +add +pop pointer 1 +push that 0 +eq +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +if-goto IF_TRUE4 +goto IF_FALSE4 +label IF_TRUE4 +push argument 2 +if-goto IF_TRUE5 +goto IF_FALSE5 +label IF_TRUE5 +push constant 2 +push argument 1 +add +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 3 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +goto IF_END5 +label IF_FALSE5 +push constant 3 +push argument 1 +add +push constant 3 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 2 +push argument 1 +add +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 1 +push argument 1 +add +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 0 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +label IF_END5 +push argument 1 +return +label IF_FALSE4 +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +eq +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +if-goto IF_TRUE6 +goto IF_FALSE6 +label IF_TRUE6 +push argument 2 +if-goto IF_TRUE7 +goto IF_FALSE7 +label IF_TRUE7 +push constant 1 +push argument 1 +add +push constant 1 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 2 +push argument 1 +add +push constant 3 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 3 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +goto IF_END7 +label IF_FALSE7 +push constant 2 +push argument 1 +add +push constant 2 +push argument 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 1 +push argument 1 +add +push constant 0 +push argument 1 +add +pop pointer 1 +push that 0 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 0 +push argument 1 +add +push constant 32 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +label IF_END7 +push argument 1 +return +label IF_FALSE6 +push argument 1 +return +function Board.addTile 6 +push argument 0 +pop pointer 0 +push this 1 +pop local 0 +push constant 23 +push local 0 +call Math.multiply 2 +push local 0 +call Math.multiply 2 +push constant 79 +add +pop local 1 +push local 1 +push local 1 +push constant 16 +call Math.divide 2 +push constant 16 +call Math.multiply 2 +sub +pop local 0 +push local 0 +push constant 4 +call Math.divide 2 +pop local 2 +push local 0 +push local 2 +push constant 4 +call Math.multiply 2 +sub +pop local 3 +push constant 65 +pop local 4 +push local 2 +push this 0 +add +pop pointer 1 +push that 0 +pop local 5 +label WHILE_EXP0 +push local 3 +push local 5 +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto WHILE_END0 +push constant 23 +push local 0 +call Math.multiply 2 +push local 0 +call Math.multiply 2 +push constant 79 +add +pop local 1 +push local 1 +push local 1 +push constant 16 +call Math.divide 2 +push constant 16 +call Math.multiply 2 +sub +pop local 0 +push local 0 +push constant 4 +call Math.divide 2 +pop local 2 +push local 0 +push local 2 +push constant 4 +call Math.multiply 2 +sub +pop local 3 +push constant 131 +push local 4 +sub +pop local 4 +push local 2 +push this 0 +add +pop pointer 1 +push that 0 +pop local 5 +goto WHILE_EXP0 +label WHILE_END0 +push local 3 +push local 5 +add +push local 4 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push constant 0 +return +function Board.transform 4 +push argument 0 +pop pointer 0 +push argument 1 +push constant 0 +eq +push argument 1 +push constant 1 +eq +or +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push constant 0 +not +pop local 1 +goto IF_END0 +label IF_FALSE0 +push constant 0 +pop local 1 +label IF_END0 +push argument 1 +push constant 0 +eq +push argument 1 +push constant 2 +eq +or +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push constant 0 +not +pop local 0 +goto IF_END1 +label IF_FALSE1 +push constant 0 +pop local 0 +label IF_END1 +push pointer 0 +push local 0 +call Board.getStrips 2 +pop local 2 +push constant 0 +pop local 3 +label WHILE_EXP0 +push local 3 +push constant 4 +lt +not +if-goto WHILE_END0 +push local 3 +push local 2 +add +push pointer 0 +push pointer 0 +push local 3 +push local 2 +add +pop pointer 1 +push that 0 +push local 1 +call Board.align 3 +push local 1 +call Board.reduce 3 +pop temp 0 +pop pointer 1 +push temp 0 +pop that 0 +push local 3 +push constant 1 +add +pop local 3 +goto WHILE_EXP0 +label WHILE_END0 +push pointer 0 +push local 2 +push local 0 +call Board.arrange 3 +pop temp 0 +push constant 0 +return +function Board.next 0 +push argument 0 +pop pointer 0 +push pointer 0 +call Board.addTile 1 +pop temp 0 +push this 1 +push constant 1 +add +pop this 1 +push pointer 0 +push argument 1 +call Board.transform 2 +pop temp 0 +push constant 0 +return +function Board.draw 5 +push argument 0 +pop pointer 0 +push constant 9 +pop local 0 +push constant 30 +pop local 1 +push constant 0 +pop local 2 +label WHILE_EXP0 +push local 2 +push constant 4 +lt +not +if-goto WHILE_END0 +push constant 0 +pop local 3 +push local 0 +push local 2 +add +push local 1 +call Output.moveCursor 2 +pop temp 0 +push local 2 +push this 0 +add +pop pointer 1 +push that 0 +pop local 4 +label WHILE_EXP1 +push local 3 +push constant 4 +lt +not +if-goto WHILE_END1 +push local 3 +push local 4 +add +pop pointer 1 +push that 0 +call Output.printChar 1 +pop temp 0 +push local 3 +push constant 1 +add +pop local 3 +goto WHILE_EXP1 +label WHILE_END1 +push local 2 +push constant 1 +add +pop local 2 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +return +function Board.dispose 0 +push argument 0 +pop pointer 0 +push pointer 0 +call Memory.deAlloc 1 +pop temp 0 +push constant 0 +return diff --git a/projects/09/K/KGame.jack b/projects/09/K/KGame.jack new file mode 100644 index 0000000..796f5b5 --- /dev/null +++ b/projects/09/K/KGame.jack @@ -0,0 +1,34 @@ +class KGame{ + field Board board; + + constructor KGame new() { + let board = Board.new(); + do board.draw(); + return this; + } + + method void dispose() { + do board.dispose(); + do Memory.deAlloc(this); + return; + } + + method void run() { + var int key, dir; + var boolean exit; + let key = 0; + let exit = false; + while (~exit) { + while ((key < 130) | (key > 133)) { + let key = Keyboard.keyPressed(); + } + let dir = key - 130; + while (~(key = 0)) { + let key = Keyboard.keyPressed(); + } + do board.next(dir); + do board.draw(); + } + return; + } +} diff --git a/projects/09/K/KGame.vm b/projects/09/K/KGame.vm new file mode 100644 index 0000000..767c14a --- /dev/null +++ b/projects/09/K/KGame.vm @@ -0,0 +1,74 @@ +function KGame.new 0 +push constant 1 +call Memory.alloc 1 +pop pointer 0 +call Board.new 0 +pop this 0 +push this 0 +call Board.draw 1 +pop temp 0 +push pointer 0 +return +function KGame.dispose 0 +push argument 0 +pop pointer 0 +push this 0 +call Board.dispose 1 +pop temp 0 +push pointer 0 +call Memory.deAlloc 1 +pop temp 0 +push constant 0 +return +function KGame.run 3 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +push constant 0 +pop local 2 +label WHILE_EXP0 +push local 2 +not +not +if-goto WHILE_END0 +label WHILE_EXP1 +push local 0 +push constant 130 +lt +push local 0 +push constant 133 +gt +or +not +if-goto WHILE_END1 +call Keyboard.keyPressed 0 +pop local 0 +goto WHILE_EXP1 +label WHILE_END1 +push local 0 +push constant 130 +sub +pop local 1 +label WHILE_EXP2 +push local 0 +push constant 0 +eq +not +not +if-goto WHILE_END2 +call Keyboard.keyPressed 0 +pop local 0 +goto WHILE_EXP2 +label WHILE_END2 +push this 0 +push local 1 +call Board.next 2 +pop temp 0 +push this 0 +call Board.draw 1 +pop temp 0 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +return diff --git a/projects/09/K/Main.jack b/projects/09/K/Main.jack new file mode 100644 index 0000000..4136e80 --- /dev/null +++ b/projects/09/K/Main.jack @@ -0,0 +1,9 @@ +class Main { + function void main() { + var KGame game; + let game = KGame.new(); + do game.run(); + do game.dispose(); + return; + } +} diff --git a/projects/09/K/Main.vm b/projects/09/K/Main.vm new file mode 100644 index 0000000..0a56ae9 --- /dev/null +++ b/projects/09/K/Main.vm @@ -0,0 +1,11 @@ +function Main.main 1 +call KGame.new 0 +pop local 0 +push local 0 +call KGame.run 1 +pop temp 0 +push local 0 +call KGame.dispose 1 +pop temp 0 +push constant 0 +return -- cgit v1.2.3