-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputUtil.jack
More file actions
62 lines (57 loc) · 1.19 KB
/
InputUtil.jack
File metadata and controls
62 lines (57 loc) · 1.19 KB
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
class InputUtil {
static int Ir,Ic;
function void init() {
let Ir = 1;
let Ic = 1;
return;
}
function void eraseMessage() {
var String s;
do Output.moveCursor(Ir,Ic);
let s = " ";
do Output.printString(s);
do s.dispose();
return;
}
function void showMessage(String s) {
do InputUtil.eraseMessage();
do Output.moveCursor(Ir,Ic);
do Output.printString(s);
do s.dispose();
return;
}
// reads only valid inputs!
// returns 0 for quit and 1 to 9 for moves!
function int readInput(String s) {
var char c;
var int choice;
do InputUtil.eraseMessage();
while(true) {
do Output.moveCursor(Ir,Ic);
do Output.printString(s);
let c = Keyboard.readChar();
// 48 to 57
if((c>47) & (c<58)) {
do s.dispose();
return c-48;
}
}
return 0;// Never reach here!
}
function int waitForZeroOrOne(String s) {
var char c;
var int choice;
do InputUtil.eraseMessage();
while(true) {
do Output.moveCursor(Ir,Ic);
do Output.printString(s);
let c = Keyboard.readChar();
// 48 to 57
if((c>47) & (c<50)) {
do s.dispose();
return c-48;
}
}
return 0;// Never reach here!
}
}