aboutsummaryrefslogtreecommitdiff
path: root/K/KGame.jack
blob: 277afaf8f09d4ca71ad1fa8971f1f4eaf4a50056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
    }
}