-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.jack
204 lines (184 loc) · 6.55 KB
/
Game.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Game controller.
- manages keyboard event loop
- handles transitions between menus
- runs the game lifecycle "ticks"
*/
class Game {
field Lifecycle lifecycle;
field Editor editor;
field int x, y;
static int MENU_NONE, MENU_MAIN, MENU_EXAMPLES, MENU_STILL_LIFES, MENU_OSCILLATORS, MENU_SPACESHIPS, MENU_NEW_GAME, MENU_RUNNING;
static int SPEED_SLOW, SPEED_FAST;
static char KEY_NONE, KEY_SPACE, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN;
static char KEY_E, KEY_G, KEY_H, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S;
constructor Game new() {
let x = 0;
let y = 0;
return this;
}
// static initialiser; sets all static variables
function void init() {
let MENU_MAIN = 1;
let MENU_EXAMPLES = 2;
let MENU_STILL_LIFES = 3;
let MENU_OSCILLATORS = 4;
let MENU_SPACESHIPS = 5;
let MENU_NEW_GAME = 6;
let MENU_RUNNING = 7;
let SPEED_SLOW = 650;
let SPEED_FAST = 300;
let KEY_SPACE = 32;
let KEY_LEFT = 130;
let KEY_UP = 131;
let KEY_RIGHT = 132;
let KEY_DOWN = 133;
let KEY_E = 69;
let KEY_G = 71;
let KEY_H = 72;
let KEY_L = 76;
let KEY_M = 77;
let KEY_N = 78;
let KEY_O = 79;
let KEY_P = 80;
let KEY_Q = 81;
let KEY_R = 82;
let KEY_S = 83;
return;
}
// runs the game
method void run() {
var char key;
var boolean quit, running, arrow, canBeRunning;
var int menu, speed;
var String test;
let lifecycle = Lifecycle.new();
let editor = Editor.new(lifecycle);
let quit = false;
let running = false;
let menu = MENU_MAIN;
let speed = SPEED_SLOW;
do Menu.showMainMenu();
while (~quit) {
let key = Keyboard.keyPressed();
let arrow = (key = KEY_UP) | (key = KEY_DOWN) | (key = KEY_LEFT) | (key = KEY_RIGHT);
let canBeRunning = (menu = MENU_STILL_LIFES) | (menu = MENU_SPACESHIPS) | (menu = MENU_OSCILLATORS) | (menu = MENU_RUNNING);
if (menu = MENU_MAIN) {
if (key = KEY_N) {
let menu = MENU_NEW_GAME;
do Menu.showNewGameMenu();
do lifecycle.clear();
do editor.startEditing();
}
if (key = KEY_E) {
let menu = MENU_EXAMPLES;
do Menu.showExamplesMenu();
}
}
if (menu = MENU_NEW_GAME) {
if (key = KEY_LEFT) { do editor.moveLeft(); }
if (key = KEY_RIGHT) { do editor.moveRight(); }
if (key = KEY_UP) { do editor.moveUp(); }
if (key = KEY_DOWN) { do editor.moveDown(); }
if (key = KEY_SPACE) { do editor.toggleCell(); }
if (key = KEY_R) {
let menu = MENU_RUNNING;
do editor.stopEditing();
do lifecycle.save();
do Menu.showRunningMenu();
let running = true;
}
}
if (menu = MENU_RUNNING) {
if (key = KEY_R) {
do lifecycle.restore();
}
}
if (menu = MENU_EXAMPLES) {
if (key = KEY_L) {
let menu = MENU_STILL_LIFES;
do Menu.showStillLifesMenu();
do Examples.buildStillLifes(lifecycle, 9);
let running = true;
}
if (key = KEY_O) {
let menu = MENU_OSCILLATORS;
do Menu.showOscillatorsMenu();
do Examples.buildOscillators(lifecycle);
let speed = SPEED_SLOW;
let running = true;
}
if (key = KEY_S) {
let menu = MENU_SPACESHIPS;
let speed = SPEED_FAST;
do Menu.showSpaceshipsMenu();
}
}
if (menu = MENU_SPACESHIPS) {
if (key = KEY_G) {
do Examples.addGlider(lifecycle);
let running = true;
}
if (key = KEY_L) {
do Examples.addLightWeightSpaceship(lifecycle);
let running = true;
}
if (key = KEY_M) {
do Examples.addMiddleWeightSpaceship(lifecycle);
let running = true;
}
if (key = KEY_H) {
do Examples.addHeavyWeightSpaceship(lifecycle);
let running = true;
}
}
// pause or unpause
if ((key = KEY_P) & canBeRunning) {
let running = ~running;
}
// quit or move back to previous menu
if (key = KEY_Q) {
if (menu = MENU_MAIN) {
do Menu.showFarewellMessage();
let quit = true;
}
if ((menu = MENU_EXAMPLES) | (menu = MENU_NEW_GAME)) {
let menu = MENU_MAIN;
do lifecycle.clear();
do Menu.showMainMenu();
}
if (menu = MENU_RUNNING) {
let menu = MENU_NEW_GAME;
do Menu.showNewGameMenu();
do lifecycle.restore();
do draw();
do editor.startEditing();
}
if ((menu = MENU_STILL_LIFES) | (menu = MENU_OSCILLATORS) | (menu = MENU_SPACESHIPS)) {
let menu = MENU_EXAMPLES;
do Menu.showExamplesMenu();
do lifecycle.clear();
}
let running = false;
}
if (~(key = KEY_NONE) & ~arrow) { do KeyboardHelper.waitForKeyReleased(key); }
if (arrow) { do Sys.wait(150); }
if (running) {
do draw();
do lifecycle.tick();
do KeyboardHelper.waitForTimePeriodOrKeyPress(speed);
}
}
return;
}
// draws cells on the grid from the lifecycle's current iteration
method void draw() {
do Grid.fillFromArray(lifecycle.getCurrentIteration());
return;
}
method void dispose() {
do lifecycle.dispose();
do Memory.deAlloc(this);
return;
}
}