-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.jack
118 lines (105 loc) · 3.73 KB
/
Utils.jack
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class Utils {
/* Colors */
function int WHITE() { return 0; }
function int BLACK() { return 1; }
/* Piece constant values. */
function int PAWN() { return 10; }
function int BISHOP() { return 11; }
function int KNIGHT() { return 12; }
function int ROOK() { return 13; }
function int QUEEN() { return 14; }
function int KING() { return 15; }
function int PIECES() { return 6; } // total unique pieces
// number of rows/cols for a chess board.
function int CHESSN() { return 8; }
/* Screen helpers. */
function int ROWS() { return 256; }
function int COLS() { return 512; }
// documentation says there are 23 rows x 64 cols for
// writing text using the Output class API.
function int TEXTROWS() { return 23; }
function int TEXTCOLS() { return 64; }
function int FONTWIDTH() { return 8; }
function int FONTHEIGHT() { return 11; }
function void printStringCentered(String s, int row) {
var int col;
let col = (Utils.TEXTCOLS() - s.length()) / 2;
do Output.moveCursor(row, col);
do Output.printString(s);
return;
}
function void printStringAt(String s, int row, int col) {
do Output.moveCursor(row, col);
do Output.printString(s);
return;
}
function void printStringAtPx(String s, int px, int py) {
var int row, col;
// Converts from pixels to rows and columns for the text
// output which uses a fixed font.
let row = py / Utils.FONTHEIGHT();
let col = px / Utils.FONTWIDTH();
do Utils.printStringAt(s, row, col);
return;
}
// draws a non-filled rectangle, thickness of border = 1.
function void drawRectangleBorder(int x1, int y1, int x2, int y2) {
do Screen.setColor(true);
do Screen.drawLine(x1, y1, x2, y1); // vertical1
do Screen.drawLine(x1, y2, x2, y2); // vertical2
do Screen.drawLine(x2, y1, x2, y2); // horizontal1
do Screen.drawLine(x1, y1, x1, y2); // horizontal2
return;
}
/* Keyboard keys */
function int LEFTARROWKEY() { return 130; }
function int UPARROWKEY() { return 131; }
function int RIGHTARROWKEY() { return 132; }
function int DOWNARROWKEY() { return 133; }
function int ESCKEY() { return 140; }
function int SPACEKEY() { return 32; }
/* Math */
function int mod(int n, int p) {
return n - ((n / p) * p);
}
/* Memory */
function int WORD() { return 16; }
// Uses the knowledge of how hack memory is organized, in
// words of 16bits and 256 x 512 pixels.
// Converts from virtual system of 16 rows by 32 columns
// into the exact pixel location.
function int getLocation(int row, int col) {
// multiply row by 32 because horizontally multiplying
// by 32 gets us right underneath, one pixel lower.
// but since we want to have 16 rows not 256 pixels
// vertically too, multiply by 16 to get to next row.
return (row * (32 * 16)) + col;
}
/* Extra Sys */
function void error(String str) {
do Utils.debug(str);
do Sys.error(-1);
return;
}
function void debug(String str) {
do Output.moveCursor(0, 0);
do Output.printString(str);
return;
}
function String concat(String str1, String str2) {
var int i;
var String str;
let str = String.new(str1.length() + str2.length());
let i = 0;
while (i < str1.length()) {
do str.appendChar(str1.charAt(i));
let i = i + 1;
}
let i = 0;
while (i < str2.length()) {
do str.appendChar(str2.charAt(i));
let i = i + 1;
}
return str;
}
}