From 3571f998b28fbc8d9250ba04c983935f10a16c15 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 20 Jan 2018 15:02:59 +0100 Subject: removed dat files --- projects/11/K/Board.jack | 305 ++++++++++++ projects/11/K/Board.vm | 1183 ++++++++++++++++++++++++++++++++++++++++++++++ projects/11/K/K.txt | 25 + projects/11/K/KGame.jack | 34 ++ projects/11/K/KGame.vm | 74 +++ projects/11/K/Main.jack | 9 + projects/11/K/Main.vm | 11 + 7 files changed, 1641 insertions(+) create mode 100644 projects/11/K/Board.jack create mode 100644 projects/11/K/Board.vm create mode 100644 projects/11/K/K.txt create mode 100644 projects/11/K/KGame.jack create mode 100644 projects/11/K/KGame.vm create mode 100644 projects/11/K/Main.jack create mode 100644 projects/11/K/Main.vm (limited to 'projects/11/K') diff --git a/projects/11/K/Board.jack b/projects/11/K/Board.jack new file mode 100644 index 0000000..49d42d5 --- /dev/null +++ b/projects/11/K/Board.jack @@ -0,0 +1,305 @@ +class Board { + field Array grid; + field int nTurn; + field int seed; + + constructor Board new() { + 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 t[j] = 32; + let j = j + 1; + } + let i = i + 1; + } + let t = grid[0]; + let t[0] = 65; + let nTurn = 0; + 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; + } + return; + } + + method void transpose(Array xs, Array ys) { + var int i, j; + var Array t, s; + let i = 0; + while (i < 4) { + let j = 0; + let t = ys[i]; + while (j < 4) { + let s = xs[j]; + let t[j] = s[i]; + let j = j + 1; + } + let i = i + 1; + } + 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 getStrips(boolean isHorizontal){ + var Array xs; + let xs = new4(); + if (isHorizontal) { + do deepcopy(grid, xs); + } else { + do transpose(grid, xs); + } + return xs; + } + + method Array align(Array xs, boolean left){ + var int i, j; + let i = 0; + let j = 0; + if (left) { + while (i < 4) { + if (xs[i] > 64) { + let xs[j] = xs[i]; + let j = j + 1; + } + let i = i + 1; + } + while (j < 4) { + let xs[j] = 32; + let j = j + 1; + } + } else { + while (i < 4) { + if (xs[3 - i] > 64) { + let xs[3 - j] = xs[3 - i]; + let j = j + 1; + } + let i = i + 1; + } + while (j < 4) { + let xs[3 - j] = 32; + let j = j + 1; + } + } + return xs; + } + + method Array 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; + let xs[1] = xs[2] + 1; + let xs[2] = 32; + let xs[3] = 32; + } else { + let xs[3] = xs[3] + 1; + let xs[2] = xs[1] + 1; + let xs[1] = 32; + let xs[0] = 32; + } + return xs; + } + 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]; + let xs[3] = 32; + } else { + let xs[1] = xs[1] + 1; + let xs[0] = 32; + } + return xs; + } + if ((xs[2] = xs[3]) & (xs[2] > 64)) { + if (left) { + let xs[2] = xs[2] + 1; + let xs[3] = 32; + } else { + let xs[3] = xs[3] + 1; + let xs[2] = xs[1]; + let xs[1] = xs[0]; + let xs[0] = 32; + } + return xs; + } + 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[0] = 32; + } + return xs; + } + return xs; + } + + method void addTile(){ + var Array t; + var int r, c, parity, newTile; + /* + let t = grid[1]; + if (t[1] = 32) { + let t[1] = 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]; + 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] = newTile; + return; + } + + method void transform(char dir){ + var boolean isHorizontal, left; + var Array xs; + var int i; + if ((dir = 0) | (dir = 1)) { + let left = true; + } else { + let left = false; + } + if ((dir = 0) | (dir = 2)) { + let isHorizontal = true; + } else { + let isHorizontal = false; + } + let xs = getStrips(isHorizontal); + let i = 0; + while (i < 4) { + let xs[i] = reduce(align(xs[i], left), left); + let i = i + 1; + } + do arrange(xs, isHorizontal); + return; + } + + method void next(char dir){ + let nTurn = nTurn + 1; + do transform(dir); + do addTile(); + return; + } + + method void draw(){ + var int r, c, i, j; + var Array t; + let r = 9; + let c = 30; + + do Output.moveCursor(r - 1, c - 1); + do Output.printString("+----+"); + do Output.moveCursor(r + 4, c - 1); + do Output.printString("+----+"); + + let i = 0; + while (i < 4) { + 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]); + let j = j + 1; + } + do Output.printString("|"); + let i = i + 1; + } + + do Output.moveCursor(r + 6, c - 2); + do Output.printString("Turn: "); + do Output.printInt(nTurn); + return; + } + + method void dispose() { + do Memory.deAlloc(this); + return; + } +} diff --git a/projects/11/K/Board.vm b/projects/11/K/Board.vm new file mode 100644 index 0000000..272b52d --- /dev/null +++ b/projects/11/K/Board.vm @@ -0,0 +1,1183 @@ +function Board.new 3 +push constant 3 +call Memory.alloc 1 +pop pointer 0 +push constant 4 +call Array.new 1 +pop this 0 +push constant 0 +pop local 0 +label new.While0 +push local 0 +push constant 4 +lt +not +if-goto new.EndWhile0 +push constant 0 +pop local 1 +push constant 4 +call Array.new 1 +push this 0 +push local 0 +add +pop pointer 1 +pop that 0 +push this 0 +push local 0 +add +pop pointer 1 +push that 0 +pop local 2 +label new.While0.While0 +push local 1 +push constant 4 +lt +not +if-goto new.While0.EndWhile0 +push constant 32 +push local 2 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto new.While0.While0 +label new.While0.EndWhile0 +push local 0 +push constant 1 +add +pop local 0 +goto new.While0 +label new.EndWhile0 +push this 0 +push constant 0 +add +pop pointer 1 +push that 0 +pop local 2 +push constant 65 +push local 2 +push constant 0 +add +pop pointer 1 +pop that 0 +push constant 0 +pop this 1 +push constant 0 +pop this 2 +push pointer 0 +return +function Board.arrange 0 +push argument 0 +pop pointer 0 +push argument 2 +not +if-goto arrange.Else0 +push pointer 0 +push argument 1 +push this 0 +call Board.deepcopy 3 +pop temp 0 +goto arrange.Endif0 +label arrange.Else0 +push pointer 0 +push argument 1 +push this 0 +call Board.transpose 3 +pop temp 0 +label arrange.Endif0 +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 copy.While0 +push local 0 +push constant 4 +lt +not +if-goto copy.EndWhile0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push argument 2 +push local 0 +add +pop pointer 1 +pop that 0 +push local 0 +push constant 1 +add +pop local 0 +goto copy.While0 +label copy.EndWhile0 +push constant 0 +return +function Board.deepcopy 1 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label deepcopy.While0 +push local 0 +push constant 4 +lt +not +if-goto deepcopy.EndWhile0 +push pointer 0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push argument 2 +push local 0 +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 deepcopy.While0 +label deepcopy.EndWhile0 +push constant 0 +return +function Board.transpose 4 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label transpose.While0 +push local 0 +push constant 4 +lt +not +if-goto transpose.EndWhile0 +push constant 0 +pop local 1 +push argument 2 +push local 0 +add +pop pointer 1 +push that 0 +pop local 2 +label transpose.While0.While0 +push local 1 +push constant 4 +lt +not +if-goto transpose.While0.EndWhile0 +push argument 1 +push local 1 +add +pop pointer 1 +push that 0 +pop local 3 +push local 3 +push local 0 +add +pop pointer 1 +push that 0 +push local 2 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto transpose.While0.While0 +label transpose.While0.EndWhile0 +push local 0 +push constant 1 +add +pop local 0 +goto transpose.While0 +label transpose.EndWhile0 +push constant 0 +return +function Board.dispose4 2 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label dispose4.While0 +push local 0 +push constant 4 +lt +not +if-goto dispose4.EndWhile0 +push argument 1 +push local 0 +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 dispose4.While0 +label dispose4.EndWhile0 +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 new4.While0 +push local 1 +push constant 4 +lt +not +if-goto new4.EndWhile0 +push constant 4 +call Array.new 1 +push local 0 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto new4.While0 +label new4.EndWhile0 +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 +not +if-goto getStrips.Else0 +push pointer 0 +push this 0 +push local 0 +call Board.deepcopy 3 +pop temp 0 +goto getStrips.Endif0 +label getStrips.Else0 +push pointer 0 +push this 0 +push local 0 +call Board.transpose 3 +pop temp 0 +label getStrips.Endif0 +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 +not +if-goto align.Else0 +label align.Else0.While0 +push local 0 +push constant 4 +lt +not +if-goto align.Else0.EndWhile0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto align.Else0.While0.Else0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push argument 1 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +label align.Else0.While0.Else0 +push local 0 +push constant 1 +add +pop local 0 +goto align.Else0.While0 +label align.Else0.EndWhile0 +label align.Else0.While1 +push local 1 +push constant 4 +lt +not +if-goto align.Else0.EndWhile1 +push constant 32 +push argument 1 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto align.Else0.While1 +label align.Else0.EndWhile1 +goto align.Endif0 +label align.Else0 +label align.If0.While0 +push local 0 +push constant 4 +lt +not +if-goto align.If0.EndWhile0 +push argument 1 +push constant 3 +push local 0 +sub +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto align.If0.While0.Else0 +push argument 1 +push constant 3 +push local 0 +sub +add +pop pointer 1 +push that 0 +push argument 1 +push constant 3 +push local 1 +sub +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +label align.If0.While0.Else0 +push local 0 +push constant 1 +add +pop local 0 +goto align.If0.While0 +label align.If0.EndWhile0 +label align.If0.While1 +push local 1 +push constant 4 +lt +not +if-goto align.If0.EndWhile1 +push constant 32 +push argument 1 +push constant 3 +push local 1 +sub +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto align.If0.While1 +label align.If0.EndWhile1 +label align.Endif0 +push argument 1 +return +function Board.reduce 0 +push argument 0 +pop pointer 0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +eq +and +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else0 +push argument 2 +not +if-goto reduce.Else0.Else0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else0.Endif0 +label reduce.Else0.Else0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else0.Endif0 +push argument 1 +return +label reduce.Else0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else1 +push argument 2 +not +if-goto reduce.Else1.Else0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else1.Endif0 +label reduce.Else1.Else0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else1.Endif0 +push argument 1 +return +label reduce.Else1 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else2 +push argument 2 +not +if-goto reduce.Else2.Else0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else2.Endif0 +label reduce.Else2.Else0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else2.Endif0 +push argument 1 +return +label reduce.Else2 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else3 +push argument 2 +not +if-goto reduce.Else3.Else0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else3.Endif0 +label reduce.Else3.Else0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else3.Endif0 +push argument 1 +return +label reduce.Else3 +push argument 1 +return +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 +not +if-goto addTile.Else0 +push this 2 +neg +pop this 2 +label addTile.Else0 +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 +not +if-goto addTile.Else1 +push constant 1 +pop local 3 +goto addTile.Endif1 +label addTile.Else1 +push constant 1 +neg +pop local 3 +label addTile.Endif1 +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 this 0 +push local 1 +add +pop pointer 1 +push that 0 +pop local 0 +push constant 65 +pop local 4 +label addTile.While2 +push local 0 +push local 2 +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto addTile.EndWhile2 +push this 2 +push local 3 +add +pop this 2 +push this 2 +push constant 0 +lt +not +if-goto addTile.While2.Else0 +push constant 15 +pop this 2 +label addTile.While2.Else0 +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 this 0 +push local 1 +add +pop pointer 1 +push that 0 +pop local 0 +push constant 131 +push local 4 +sub +pop local 4 +goto addTile.While2 +label addTile.EndWhile2 +push local 4 +push local 0 +push local 2 +add +pop pointer 1 +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 +not +if-goto transform.Else0 +push constant 1 +neg +pop local 1 +goto transform.Endif0 +label transform.Else0 +push constant 0 +pop local 1 +label transform.Endif0 +push argument 1 +push constant 0 +eq +push argument 1 +push constant 2 +eq +or +not +if-goto transform.Else1 +push constant 1 +neg +pop local 0 +goto transform.Endif1 +label transform.Else1 +push constant 0 +pop local 0 +label transform.Endif1 +push pointer 0 +push local 0 +call Board.getStrips 2 +pop local 2 +push constant 0 +pop local 3 +label transform.While2 +push local 3 +push constant 4 +lt +not +if-goto transform.EndWhile2 +push pointer 0 +push pointer 0 +push local 2 +push local 3 +add +pop pointer 1 +push that 0 +push local 1 +call Board.align 3 +push local 1 +call Board.reduce 3 +push local 2 +push local 3 +add +pop pointer 1 +pop that 0 +push local 3 +push constant 1 +add +pop local 3 +goto transform.While2 +label transform.EndWhile2 +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 this 1 +push constant 1 +add +pop this 1 +push pointer 0 +push argument 1 +call Board.transform 2 +pop temp 0 +push pointer 0 +call Board.addTile 1 +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 local 0 +push constant 1 +sub +push local 1 +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 +call Output.printString 1 +pop temp 0 +push local 0 +push constant 4 +add +push local 1 +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 +call Output.printString 1 +pop temp 0 +push constant 0 +pop local 2 +label draw.While0 +push local 2 +push constant 4 +lt +not +if-goto draw.EndWhile0 +push constant 0 +pop local 3 +push local 0 +push local 2 +add +push local 1 +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 +pop temp 0 +push this 0 +push local 2 +add +pop pointer 1 +push that 0 +pop local 4 +label draw.While0.While0 +push local 3 +push constant 4 +lt +not +if-goto draw.While0.EndWhile0 +push local 4 +push local 3 +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 draw.While0.While0 +label draw.While0.EndWhile0 +push constant 1 +call String.new 1 +push constant 124 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push local 2 +push constant 1 +add +pop local 2 +goto draw.While0 +label draw.EndWhile0 +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 +push argument 0 +pop pointer 0 +push pointer 0 +call Memory.deAlloc 1 +pop temp 0 +push constant 0 +return diff --git a/projects/11/K/K.txt b/projects/11/K/K.txt new file mode 100644 index 0000000..dc75c82 --- /dev/null +++ b/projects/11/K/K.txt @@ -0,0 +1,25 @@ +from a to k + +- a title frame showing game name and instructions. space to continue. +- a main frame showing the game running +- a endofgame frame showing game results: win or lose + +- a Board class with cell being a 4 by 4 two-d array. +- arrange strings according to an orientation: arrange(4 strings, dir)=cells + - dir: 0: align to left, 1: align to right, 2: align to top, 3:align to bottom + - e.g.: arrange({"abc", "bc", "dc", "efeh", 0}) gives the following board: + abc_ + bc__ + dc__ + efeh + - arrange({"abc", "bc", "dc", "efeh", 0}, 3) gives the following + ___e + a__f + bbde + ccch +- a new tile of 'a' or 'b' appears each turn somewhere, using some quasirandomisation e.g. (23 * n^2 + 79) mod 16. + - addtile(char, x, y) +- the board transformation: board.trans(dir) = arrange(dir) . (fmap reduce) . getstring(dir). calls getstrings, then reduce on each row / column +- board.getstrings(dir)= the strings according to direction dir. Inverse of arrange. +- reduce(string)=string: reduce("baac")=="bbc"; reduce("aabb")=="bc"; and reduce("cbb")== "cc" instead of "d". +- when one of the hjkl keys is pressed get the direction dir. diff --git a/projects/11/K/KGame.jack b/projects/11/K/KGame.jack new file mode 100644 index 0000000..796f5b5 --- /dev/null +++ b/projects/11/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/11/K/KGame.vm b/projects/11/K/KGame.vm new file mode 100644 index 0000000..eef2df0 --- /dev/null +++ b/projects/11/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 run.While0 +push local 2 +not +not +if-goto run.EndWhile0 +label run.While0.While0 +push local 0 +push constant 130 +lt +push local 0 +push constant 133 +gt +or +not +if-goto run.While0.EndWhile0 +call Keyboard.keyPressed 0 +pop local 0 +goto run.While0.While0 +label run.While0.EndWhile0 +push local 0 +push constant 130 +sub +pop local 1 +label run.While0.While1 +push local 0 +push constant 0 +eq +not +not +if-goto run.While0.EndWhile1 +call Keyboard.keyPressed 0 +pop local 0 +goto run.While0.While1 +label run.While0.EndWhile1 +push this 0 +push local 1 +call Board.next 2 +pop temp 0 +push this 0 +call Board.draw 1 +pop temp 0 +goto run.While0 +label run.EndWhile0 +push constant 0 +return diff --git a/projects/11/K/Main.jack b/projects/11/K/Main.jack new file mode 100644 index 0000000..4136e80 --- /dev/null +++ b/projects/11/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/11/K/Main.vm b/projects/11/K/Main.vm new file mode 100644 index 0000000..0a56ae9 --- /dev/null +++ b/projects/11/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