diff options
Diffstat (limited to 'projects')
-rw-r--r-- | projects/09/K/Board.jack | 250 | ||||
-rw-r--r-- | projects/09/K/Board.vm | 728 | ||||
-rw-r--r-- | projects/09/K/KGame.jack | 39 | ||||
-rw-r--r-- | projects/09/K/KGame.vm | 129 | ||||
-rw-r--r-- | projects/09/K/Main.jack | 1 | ||||
-rw-r--r-- | projects/09/K/Main.vm | 2 |
6 files changed, 742 insertions, 407 deletions
diff --git a/projects/09/K/Board.jack b/projects/09/K/Board.jack index 49d42d5..e9ed8a6 100644 --- a/projects/09/K/Board.jack +++ b/projects/09/K/Board.jack @@ -1,115 +1,80 @@ +/** +improvments: +- do in place transposition +- add a static string boardBar = "+----+" +- change printString("|") to printChar +- change the grid to static variable? +*/ class Board { field Array grid; - field int nTurn; - field int seed; + field int nTurn, seed, status; // status: 0: begin game; 1: in game; 2: lose; 3: win; + static String boardBar, strLost, strCont, strGameOver, strWon; constructor Board new() { - var int i, j; - var Array t; + var int i; 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 t[j] = 32; - let j = j + 1; - } let i = i + 1; } - let t = grid[0]; - let t[0] = 65; - let nTurn = 0; + do initBoard(); let seed = 0; return this; } - method void arrange(Array xs, boolean isHorizontal) { - if (isHorizontal) { - do deepcopy(xs, grid); - } else { - do transpose(xs, grid); - } - do dispose4(xs); - return; - } - - - 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; - } - - method void deepcopy(Array xs, Array ys) { - var int i; - let i = 0; - while (i < 4) { - do copy(xs[i], ys[i]); - let i = i + 1; - } + function void init() { + let boardBar = "+----+"; + let strLost = "You lost!"; + let strWon = "You won!"; + let strCont = "Press any key to continue"; + let strGameOver = "Game over!"; return; } - method void transpose(Array xs, Array ys) { + method void initBoard() { var int i, j; - var Array t, s; + var Array t; let i = 0; while (i < 4) { let j = 0; - let t = ys[i]; + let t = grid[i]; while (j < 4) { - let s = xs[j]; - let t[j] = s[i]; + let t[j] = 32; let j = j + 1; } let i = i + 1; } + let t = grid[0]; + let t[0] = 65; + let nTurn = 0; + let status = 0; 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 void transpose() { + do exch(0, 1); + do exch(0, 2); + do exch(0, 3); + do exch(1, 2); + do exch(1, 3); + do exch(2, 3); + return; } - method Array getStrips(boolean isHorizontal){ - var Array xs; - let xs = new4(); - if (isHorizontal) { - do deepcopy(grid, xs); - } else { - do transpose(grid, xs); - } - return xs; + method void exch(int i, int j){ + var int t; + var Array s1, s2; + let s1 = grid[i]; + let s2 = grid[j]; + let t = s1[j]; + let s1[j] = s2[i]; + let s2[i] = t; + return; } - method Array align(Array xs, boolean left){ + method void align(Array xs, boolean left){ var int i, j; let i = 0; let j = 0; @@ -138,10 +103,10 @@ class Board { let j = j + 1; } } - return xs; + return; } - method Array reduce(Array xs, boolean left){ + method void reduce(Array xs, boolean left){ if ((xs[0] = xs[1]) & (xs[2] = xs[3]) & (xs[0] > 64) & (xs[2] > 64)) { if (left) { let xs[0] = xs[0] + 1; @@ -154,7 +119,7 @@ class Board { let xs[1] = 32; let xs[0] = 32; } - return xs; + return; } if ((xs[0] = xs[1]) & (xs[0] > 64)) { if (left) { @@ -166,7 +131,7 @@ class Board { let xs[1] = xs[1] + 1; let xs[0] = 32; } - return xs; + return; } if ((xs[2] = xs[3]) & (xs[2] > 64)) { if (left) { @@ -178,7 +143,7 @@ class Board { let xs[1] = xs[0]; let xs[0] = 32; } - return xs; + return; } if ((xs[1] = xs[2]) & (xs[1] > 64)) { if (left) { @@ -190,9 +155,9 @@ class Board { let xs[1] = xs[0]; let xs[0] = 32; } - return xs; + return; } - return xs; + return; } method void addTile(){ @@ -204,6 +169,11 @@ class Board { let t[1] = 65; } */ + + if (~(status = 1)) { + return; + } + let seed = seed * 25173 + 13849; if (seed < 0) { let seed = - seed; @@ -237,8 +207,7 @@ class Board { } method void transform(char dir){ - var boolean isHorizontal, left; - var Array xs; + var boolean isVertical, left; var int i; if ((dir = 0) | (dir = 1)) { let left = true; @@ -246,55 +215,114 @@ class Board { let left = false; } if ((dir = 0) | (dir = 2)) { - let isHorizontal = true; + let isVertical = false; } else { - let isHorizontal = false; + let isVertical = true; + } + if (isVertical) { + do transpose(); } - let xs = getStrips(isHorizontal); let i = 0; while (i < 4) { - let xs[i] = reduce(align(xs[i], left), left); + do align(grid[i], left); + do reduce(grid[i], left); let i = i + 1; } - do arrange(xs, isHorizontal); + if (isVertical) { + do transpose(); + } return; } - method void next(char dir){ + method void next(int dir){ let nTurn = nTurn + 1; do transform(dir); - do addTile(); + do updateStatus(); return; } - method void draw(){ - var int r, c, i, j; - var Array t; - let r = 9; - let c = 30; + method int getStatus(){ + return status; + } - do Output.moveCursor(r - 1, c - 1); - do Output.printString("+----+"); - do Output.moveCursor(r + 4, c - 1); - do Output.printString("+----+"); + method void setStatus(int x){ + let status = x; + return; + } + method void updateStatus(){ + var int i, j; + var Array r; let i = 0; while (i < 4) { + let r = grid[i]; let j = 0; - do Output.moveCursor(r + i, c - 1); - do Output.printString("|"); - let t = grid[i]; while (j < 4) { - do Output.printChar(t[j]); + if (r[j] = 75) { + let status = 3; + return; + } + if (r[j] = 32) { + let status = 1; + return; + } let j = j + 1; } - do Output.printString("|"); let i = i + 1; } + let status = 2; + return; + } + + method void draw(){ + var int r, c, i, j; + var Array t; + let r = 9; + let c = 30; + + if (status = 0) { + do Output.moveCursor(r - 1, c - 1); + do Output.printChar(75); + do Output.moveCursor(r + 1, c - 1); + do Output.printString(strCont); + } else { if (status = 2) { + do Output.moveCursor(r - 1, c - 1); + do Output.printString(strGameOver); + do Output.moveCursor(r + 1, c - 1); + do Output.printString(strLost); + do Output.moveCursor(r + 3, c - 1); + do Output.printString(strCont); + } else { if (status = 3) { + do Output.moveCursor(r - 1, c - 1); + do Output.printString(strGameOver); + do Output.moveCursor(r + 1, c - 1); + do Output.printString(strWon); + do Output.moveCursor(r + 3, c - 1); + do Output.printString(strCont); + } else { + do Output.moveCursor(r - 1, c - 1); + do Output.printString(boardBar); + do Output.moveCursor(r + 4, c - 1); + do Output.printString(boardBar); + + let i = 0; + while (i < 4) { + let j = 0; + do Output.moveCursor(r + i, c - 1); + do Output.printChar(124); // 124 is | + let t = grid[i]; + while (j < 4) { + do Output.printChar(t[j]); + let j = j + 1; + } + do Output.printChar(124); + let i = i + 1; + } - do Output.moveCursor(r + 6, c - 2); - do Output.printString("Turn: "); - do Output.printInt(nTurn); + 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 3b5983c..ba236e1 100644 --- a/projects/09/K/Board.vm +++ b/projects/09/K/Board.vm @@ -1,5 +1,5 @@ -function Board.new 3 -push constant 3 +function Board.new 1 +push constant 4 call Memory.alloc 1 pop pointer 0 push constant 4 @@ -13,8 +13,6 @@ push constant 4 lt not if-goto WHILE_END0 -push constant 0 -pop local 1 push local 0 push this 0 add @@ -25,6 +23,166 @@ 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 pointer 0 +call Board.initBoard 1 +pop temp 0 +push constant 0 +pop this 2 +push pointer 0 +return +function Board.init 0 +push constant 6 +call String.new 1 +push constant 43 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 43 +call String.appendChar 2 +pop static 0 +push constant 9 +call String.new 1 +push constant 89 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 33 +call String.appendChar 2 +pop static 1 +push constant 8 +call String.new 1 +push constant 89 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 119 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 33 +call String.appendChar 2 +pop static 4 +push constant 25 +call String.new 1 +push constant 80 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +pop static 2 +push constant 10 +call String.new 1 +push constant 71 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 109 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 118 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 33 +call String.appendChar 2 +pop static 3 +push constant 0 +return +function Board.initBoard 3 +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 this 0 add pop pointer 1 @@ -73,134 +231,70 @@ pop that 0 push constant 0 pop this 1 push constant 0 -pop this 2 -push pointer 0 +pop this 3 +push constant 0 return -function Board.arrange 0 +function Board.transpose 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 +push constant 0 +push constant 1 +call Board.exch 3 pop temp 0 -goto IF_END0 -label IF_FALSE0 push pointer 0 -push argument 1 -push this 0 -call Board.transpose 3 +push constant 0 +push constant 2 +call Board.exch 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 +push constant 3 +call Board.exch 3 pop temp 0 -pop pointer 1 -push temp 0 -pop that 0 -push local 0 +push pointer 0 push constant 1 -add -pop local 0 -goto WHILE_EXP0 -label WHILE_END0 +push constant 2 +call Board.exch 3 +pop temp 0 +push pointer 0 +push constant 1 +push constant 3 +call Board.exch 3 +pop temp 0 +push pointer 0 +push constant 2 +push constant 3 +call Board.exch 3 +pop temp 0 push constant 0 return -function Board.deepcopy 1 +function Board.exch 3 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 +push this 0 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 +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 argument 2 push local 1 -push argument 1 add pop pointer 1 push that 0 -pop local 3 +pop local 0 +push argument 2 push local 1 -push local 2 add -push local 0 -push local 3 +push argument 1 +push local 2 add pop pointer 1 push that 0 @@ -208,106 +302,15 @@ 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 +push local 2 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 +push constant 0 return function Board.align 2 push argument 0 @@ -449,7 +452,7 @@ pop local 1 goto WHILE_EXP3 label WHILE_END3 label IF_END0 -push argument 1 +push constant 0 return function Board.reduce 0 push argument 0 @@ -591,7 +594,7 @@ pop pointer 1 push temp 0 pop that 0 label IF_END1 -push argument 1 +push constant 0 return label IF_FALSE0 push constant 0 @@ -691,7 +694,7 @@ pop pointer 1 push temp 0 pop that 0 label IF_END3 -push argument 1 +push constant 0 return label IF_FALSE2 push constant 2 @@ -791,7 +794,7 @@ pop pointer 1 push temp 0 pop that 0 label IF_END5 -push argument 1 +push constant 0 return label IF_FALSE4 push constant 1 @@ -891,14 +894,24 @@ pop pointer 1 push temp 0 pop that 0 label IF_END7 -push argument 1 +push constant 0 return label IF_FALSE6 -push argument 1 +push constant 0 return function Board.addTile 5 push argument 0 pop pointer 0 +push this 3 +push constant 1 +eq +not +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push constant 0 +return +label IF_FALSE0 push this 2 push constant 25173 call Math.multiply 2 @@ -908,13 +921,13 @@ pop this 2 push this 2 push constant 0 lt -if-goto IF_TRUE0 -goto IF_FALSE0 -label IF_TRUE0 +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 push this 2 neg pop this 2 -label IF_FALSE0 +label IF_FALSE1 push this 2 push this 2 push constant 2 @@ -924,17 +937,17 @@ call Math.multiply 2 sub push constant 0 eq -if-goto IF_TRUE1 -goto IF_FALSE1 -label IF_TRUE1 +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 push constant 1 pop local 3 -goto IF_END1 -label IF_FALSE1 +goto IF_END2 +label IF_FALSE2 push constant 1 neg pop local 3 -label IF_END1 +label IF_END2 push this 2 push this 2 push constant 16 @@ -978,12 +991,12 @@ pop this 2 push this 2 push constant 0 lt -if-goto IF_TRUE2 -goto IF_FALSE2 -label IF_TRUE2 +if-goto IF_TRUE3 +goto IF_FALSE3 +label IF_TRUE3 push constant 15 pop this 2 -label IF_FALSE2 +label IF_FALSE3 push this 2 push this 2 push constant 16 @@ -1024,7 +1037,7 @@ push temp 0 pop that 0 push constant 0 return -function Board.transform 4 +function Board.transform 3 push argument 0 pop pointer 0 push argument 1 @@ -1056,54 +1069,61 @@ 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 +not pop local 0 label IF_END1 -push pointer 0 push local 0 -call Board.getStrips 2 -pop local 2 +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +push pointer 0 +call Board.transpose 1 +pop temp 0 +label IF_FALSE2 push constant 0 -pop local 3 +pop local 2 label WHILE_EXP0 -push local 3 +push local 2 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 +push this 0 add pop pointer 1 push that 0 push local 1 call Board.align 3 +pop temp 0 +push pointer 0 +push local 2 +push this 0 +add +pop pointer 1 +push that 0 push local 1 call Board.reduce 3 pop temp 0 -pop pointer 1 -push temp 0 -pop that 0 -push local 3 +push local 2 push constant 1 add -pop local 3 +pop local 2 goto WHILE_EXP0 label WHILE_END0 -push pointer 0 -push local 2 push local 0 -call Board.arrange 3 +if-goto IF_TRUE3 +goto IF_FALSE3 +label IF_TRUE3 +push pointer 0 +call Board.transpose 1 pop temp 0 +label IF_FALSE3 push constant 0 return function Board.next 0 @@ -1118,10 +1138,93 @@ push argument 1 call Board.transform 2 pop temp 0 push pointer 0 -call Board.addTile 1 +call Board.updateStatus 1 pop temp 0 push constant 0 return +function Board.getStatus 0 +push argument 0 +pop pointer 0 +push this 3 +return +function Board.setStatus 0 +push argument 0 +pop pointer 0 +push argument 1 +pop this 3 +push constant 0 +return +function Board.updateStatus 3 +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 this 0 +add +pop pointer 1 +push that 0 +pop local 2 +push constant 0 +pop local 1 +label WHILE_EXP1 +push local 1 +push constant 4 +lt +not +if-goto WHILE_END1 +push local 1 +push local 2 +add +pop pointer 1 +push that 0 +push constant 75 +eq +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push constant 3 +pop this 3 +push constant 0 +return +label IF_FALSE0 +push local 1 +push local 2 +add +pop pointer 1 +push that 0 +push constant 32 +eq +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push constant 1 +pop this 3 +push constant 0 +return +label IF_FALSE1 +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 2 +pop this 3 +push constant 0 +return function Board.draw 5 push argument 0 pop pointer 0 @@ -1129,6 +1232,12 @@ push constant 9 pop local 0 push constant 30 pop local 1 +push this 3 +push constant 0 +eq +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 push local 0 push constant 1 sub @@ -1137,20 +1246,113 @@ push constant 1 sub call Output.moveCursor 2 pop temp 0 -push constant 6 -call String.new 1 -push constant 43 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 43 -call String.appendChar 2 +push constant 75 +call Output.printChar 1 +pop temp 0 +push local 0 +push constant 1 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 2 +call Output.printString 1 +pop temp 0 +goto IF_END0 +label IF_FALSE0 +push this 3 +push constant 2 +eq +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push local 0 +push constant 1 +sub +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 3 +call Output.printString 1 +pop temp 0 +push local 0 +push constant 1 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 1 +call Output.printString 1 +pop temp 0 +push local 0 +push constant 3 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 2 +call Output.printString 1 +pop temp 0 +goto IF_END1 +label IF_FALSE1 +push this 3 +push constant 3 +eq +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +push local 0 +push constant 1 +sub +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 3 +call Output.printString 1 +pop temp 0 +push local 0 +push constant 1 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 4 +call Output.printString 1 +pop temp 0 +push local 0 +push constant 3 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 2 +call Output.printString 1 +pop temp 0 +goto IF_END2 +label IF_FALSE2 +push local 0 +push constant 1 +sub +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push static 0 call Output.printString 1 pop temp 0 push local 0 @@ -1161,20 +1363,7 @@ push constant 1 sub call Output.moveCursor 2 pop temp 0 -push constant 6 -call String.new 1 -push constant 43 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 43 -call String.appendChar 2 +push static 0 call Output.printString 1 pop temp 0 push constant 0 @@ -1195,11 +1384,8 @@ push constant 1 sub call Output.moveCursor 2 pop temp 0 -push constant 1 -call String.new 1 push constant 124 -call String.appendChar 2 -call Output.printString 1 +call Output.printChar 1 pop temp 0 push local 2 push this 0 @@ -1226,11 +1412,8 @@ add pop local 3 goto WHILE_EXP1 label WHILE_END1 -push constant 1 -call String.new 1 push constant 124 -call String.appendChar 2 -call Output.printString 1 +call Output.printChar 1 pop temp 0 push local 2 push constant 1 @@ -1265,6 +1448,9 @@ pop temp 0 push this 1 call Output.printInt 1 pop temp 0 +label IF_END2 +label IF_END1 +label IF_END0 push constant 0 return function Board.dispose 0 diff --git a/projects/09/K/KGame.jack b/projects/09/K/KGame.jack index 796f5b5..e2b6d3d 100644 --- a/projects/09/K/KGame.jack +++ b/projects/09/K/KGame.jack @@ -14,20 +14,43 @@ class KGame{ } method void run() { - var int key, dir; + var int key, dir, key1, st; var boolean exit; - let key = 0; let exit = false; while (~exit) { - while ((key < 130) | (key > 133)) { + let st = board.getStatus(); + let key = 0; + while (key = 0) { let key = Keyboard.keyPressed(); } - let dir = key - 130; - while (~(key = 0)) { - let key = Keyboard.keyPressed(); + let key1 = key; + while (~(key1 = 0)) { + let key1 = Keyboard.keyPressed(); } - do board.next(dir); - do board.draw(); + if (key = 81) { + let exit = true; + } else { if ((st = 0)) { + do Screen.clearScreen(); + do board.setStatus(1); + do board.draw(); + } else { if ((st = 2) | (st = 3)) { + do Screen.clearScreen(); + do board.setStatus(0); + do board.draw(); + do board.initBoard(); + } else { if (key = 82) { // r for restart + do Screen.clearScreen(); + do board.initBoard(); + do board.draw(); + } else { if ((key > 129) & (key < 134)) { + do Screen.clearScreen(); + do board.next(key - 130); + do board.draw(); + do Sys.wait(300); + do Screen.clearScreen(); + do board.addTile(); + do board.draw(); + }}}}} } return; } diff --git a/projects/09/K/KGame.vm b/projects/09/K/KGame.vm index 767c14a..57fc333 100644 --- a/projects/09/K/KGame.vm +++ b/projects/09/K/KGame.vm @@ -20,26 +20,25 @@ call Memory.deAlloc 1 pop temp 0 push constant 0 return -function KGame.run 3 +function KGame.run 5 push argument 0 pop pointer 0 push constant 0 -pop local 0 -push constant 0 -pop local 2 +pop local 4 label WHILE_EXP0 -push local 2 +push local 4 not not if-goto WHILE_END0 +push this 0 +call Board.getStatus 1 +pop local 3 +push constant 0 +pop local 0 label WHILE_EXP1 push local 0 -push constant 130 -lt -push local 0 -push constant 133 -gt -or +push constant 0 +eq not if-goto WHILE_END1 call Keyboard.keyPressed 0 @@ -47,27 +46,123 @@ pop local 0 goto WHILE_EXP1 label WHILE_END1 push local 0 -push constant 130 -sub -pop local 1 +pop local 2 label WHILE_EXP2 -push local 0 +push local 2 push constant 0 eq not not if-goto WHILE_END2 call Keyboard.keyPressed 0 -pop local 0 +pop local 2 goto WHILE_EXP2 label WHILE_END2 +push local 0 +push constant 81 +eq +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push constant 0 +not +pop local 4 +goto IF_END0 +label IF_FALSE0 +push local 3 +push constant 0 +eq +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +call Screen.clearScreen 0 +pop temp 0 +push this 0 +push constant 1 +call Board.setStatus 2 +pop temp 0 push this 0 -push local 1 +call Board.draw 1 +pop temp 0 +goto IF_END1 +label IF_FALSE1 +push local 3 +push constant 2 +eq +push local 3 +push constant 3 +eq +or +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +call Screen.clearScreen 0 +pop temp 0 +push this 0 +push constant 0 +call Board.setStatus 2 +pop temp 0 +push this 0 +call Board.draw 1 +pop temp 0 +push this 0 +call Board.initBoard 1 +pop temp 0 +goto IF_END2 +label IF_FALSE2 +push local 0 +push constant 82 +eq +if-goto IF_TRUE3 +goto IF_FALSE3 +label IF_TRUE3 +call Screen.clearScreen 0 +pop temp 0 +push this 0 +call Board.initBoard 1 +pop temp 0 +push this 0 +call Board.draw 1 +pop temp 0 +goto IF_END3 +label IF_FALSE3 +push local 0 +push constant 129 +gt +push local 0 +push constant 134 +lt +and +if-goto IF_TRUE4 +goto IF_FALSE4 +label IF_TRUE4 +call Screen.clearScreen 0 +pop temp 0 +push this 0 +push local 0 +push constant 130 +sub call Board.next 2 pop temp 0 push this 0 call Board.draw 1 pop temp 0 +push constant 300 +call Sys.wait 1 +pop temp 0 +call Screen.clearScreen 0 +pop temp 0 +push this 0 +call Board.addTile 1 +pop temp 0 +push this 0 +call Board.draw 1 +pop temp 0 +label IF_FALSE4 +label IF_END3 +label IF_END2 +label IF_END1 +label IF_END0 goto WHILE_EXP0 label WHILE_END0 push constant 0 diff --git a/projects/09/K/Main.jack b/projects/09/K/Main.jack index 4136e80..7f86973 100644 --- a/projects/09/K/Main.jack +++ b/projects/09/K/Main.jack @@ -1,6 +1,7 @@ class Main { function void main() { var KGame game; + do Board.init(); let game = KGame.new(); do game.run(); do game.dispose(); diff --git a/projects/09/K/Main.vm b/projects/09/K/Main.vm index 0a56ae9..0441c7a 100644 --- a/projects/09/K/Main.vm +++ b/projects/09/K/Main.vm @@ -1,4 +1,6 @@ function Main.main 1 +call Board.init 0 +pop temp 0 call KGame.new 0 pop local 0 push local 0 |