From d3a0cc3a8ba6dfeb64d3faeffdeb6845b60e5840 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 20 Jan 2018 15:41:49 +0100 Subject: rearranged the dir for github - removed tools and pdfs - rearranged the projects dirs - added md files - other minor changes --- K/KGame.jack | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 K/KGame.jack (limited to 'K/KGame.jack') diff --git a/K/KGame.jack b/K/KGame.jack new file mode 100644 index 0000000..277afaf --- /dev/null +++ b/K/KGame.jack @@ -0,0 +1,63 @@ +/** +- K: a 2048 clone written in Jack language +- Coursework for Nand2Tetris (http://nand2tetris.org/) +- Author: Yuchen Pei (me@ypei.me) +- This file: the game I/O for the K game +*/ +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, key1, st; + var boolean exit; + let exit = false; + while (~exit) { + let st = board.getStatus(); + let key = 0; + while (key = 0) { + let key = Keyboard.keyPressed(); + } + let key1 = key; + while (~(key1 = 0)) { + let key1 = Keyboard.keyPressed(); + } + 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; + } +} -- cgit v1.2.3