-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHunt_The_Wumpus.ino
613 lines (505 loc) · 15.8 KB
/
Hunt_The_Wumpus.ino
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// -------------------------------------------------------------------------------
// An Arduino sketch that implements the classic game Hunt the Wumpus
//
// http://en.wikipedia.org/wiki/Wumpus
//
// In this variant, there are twenty rooms layed out like a d20; therefore, each room is connected
// to three other rooms. Two rooms are bottomless pits; if you fall in to a bottomless pit you lose.
// Two rooms contain giant bats; if you enter a room with a giant bat it will pick you up and carry
// you to a random room. One room contains a wumpus; if you bump into the Wumpus, it will eat you and
// you lose. You have 2 arrows; if you shoot the Wumpus, you win. If you run out of arrows, you lose.
//
// MIT license.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// ******************************************************
// Designed for the Adafruit RGB LCD Shield Kit
// http://www.adafruit.com/products/716
// or
// Adafruit Negative RGB LCD Shield Kit
// http://www.adafruit.com/products/714
// ******************************************************
//
//
// --------------------------------------------------------------------------------
// Dependencies
// --------------------------------------------------------------------------------
// Adafruit Industries's RGB 16x2 LCD Shield library:
// https://github.com/adafruit/Adafruit-RGB-LCD-Shield-Library
// Adafruit Industries's MCP23017 I2C Port Expander library:
// https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
// --------------------------------------------------------------------------------
#include <Wire.h>
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include "Hunt_The_Wumpus.h"
//! Map of the cavern layout.
/*!
The first dimension represents the cave number and the second dimension represents
the connected caves. NOTE - C indexing starts at 0, but cave number display starts
at 1 - so the first line below means cave 1 is connected to caves 2, 5, and 8.
*/
const uint8_t cave[20][3] = { { 2, 8, 14}, // 0
{ 7, 13, 19}, // 1
{12, 18, 0}, // 2
{16, 17, 19}, // 3
{11, 14, 18}, // 4
{13, 15, 18}, // 5
{ 9, 14, 16}, // 6
{ 1, 15, 17}, // 7
{ 0, 10, 16}, // 8
{ 6, 11, 19}, // 9
{ 8, 12, 17}, // 10
{ 4, 9, 13}, // 11
{ 2, 10, 15}, // 12
{ 1, 5, 11}, // 13
{ 0, 4, 6}, // 14
{ 5, 7, 12}, // 15
{ 3, 6, 8}, // 16
{ 3, 7, 10}, // 17
{ 2, 4, 5}, // 18
{ 1, 3, 9} }; // 19
//! Index in the map of the room the player is in.
uint8_t player_room;
//! Hazards in each room
uint8_t hazards[20];
//! Count of how many arrows the player has left.
uint8_t arrow_count;
//! Index in the map of the room the arrow is shot into.
/*!
This index is only valid/current during the state changes
associated with firing an arrow into a room.
*/
uint8_t arrow_room;
//! The current state.
void (*state)() = NULL;
//! The state that the game was in prior to the current state.
void (*last_state)() = NULL;
//! The time in milliseconds since the last state change.
unsigned long last_state_change_time;
//! The current time in milliseconds since boot.
unsigned long time;
//! Array of columns bracketing menu options.
/*!
The first dimension is the menu item index, the second column is the
tuple of columns which bracket the menu item text.
*/
const uint8_t menu_col[4][2] = { {0, 3},
{3, 6},
{6, 9},
{9, 15} };
//! The currently selected menu index.
uint8_t selected_menu_idx;
//! The LCD display object.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
//! Enum of backlight colors.
enum BackLightColor { RED=0x1, YELLOW=0x3, GREEN=0x2, TEAL=0x6, BLUE=0x4, VIOLET=0x5, WHITE=0x7 };
//! The bitmask of currently clicked buttons.
uint8_t clicked_buttons;
//! Array of custom bitmap icons.
/*!
Custom icons created using: http://www.quinapalus.com/hd44780udg.html
*/
byte icons[4][8] = { { 0x0e, 0x15, 0x1f, 0x11, 0x0e, 0x11, 0x11 },
{ 0x0a, 0x1f, 0x15, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x11, 0x0a, 0x0a, 0x0a, 0x0a },
{ 0x00, 0x04, 0x02, 0x1f, 0x02, 0x04, 0x00 } };
//! Index into the bitmap array for the wumpus icon.
const int WUMPUS_ICON_IDX = 0;
//! Index into the bitmap array for the bat icon.
const int BAT_ICON_IDX = 1;
//! Index into the bitmap array for the pit icon.
const int PIT_ICON_IDX = 2;
//! Index into the bitmap array for the arrow icon.
const int ARROW_ICON_IDX = 3;
//! Perform one time setup for the game and put the game in the splash screen state.
void setup() {
// LCD has 16 columns & 2 rows
lcd.begin(16, 2);
// Define custom icons
lcd.createChar(WUMPUS_ICON_IDX, icons[WUMPUS_ICON_IDX]);
lcd.createChar(BAT_ICON_IDX, icons[BAT_ICON_IDX]);
lcd.createChar(PIT_ICON_IDX, icons[PIT_ICON_IDX]);
lcd.createChar(ARROW_ICON_IDX, icons[ARROW_ICON_IDX]);
// Use Pin 0 to seed the random number generator
randomSeed(analogRead(0));
// Initial game state
state = begin_splash_screen;
}
//! Main loop of execution.
void loop() {
time = millis();
// Record time of state change so animations
// know when to stop
if (last_state != state) {
last_state = state;
last_state_change_time = time;
}
// Read in which buttons were clicked
read_button_clicks();
// Call current state function
state();
delay(10);
}
//! Return a bitmask of clicked buttons.
/*!
Examine the bitmask of buttons which are currently pressed and compare against
the bitmask of which buttons were pressed last time the function was called.
If a button transitions from pressed to released, return it in the bitmask.
\return the bitmask of clicked buttons
*/
void read_button_clicks() {
static uint8_t last_buttons = 0;
uint8_t buttons = lcd.readButtons();
clicked_buttons = (last_buttons ^ buttons) & (~buttons);
last_buttons = buttons;
}
//! Print a cave number to the lcd.
/*!
Print a cave number to the lcd by adding one to the index number and left
padding with a single space if needed to make the cave number take up two
places.
\param cave_idx the index of the cave in the array
*/
void print_cave_number(uint8_t cave_idx) {
if (cave_idx < 9) {
lcd.print(' ');
}
lcd.print(cave_idx + 1);
}
//! Clear the current menu selection indicator characters
/*!
Erase the characters bracketing the current menu selection.
*/
void clear_current_menu() {
lcd.setCursor(menu_col[selected_menu_idx][0], 1);
lcd.print(' ');
lcd.setCursor(menu_col[selected_menu_idx][1], 1);
lcd.print(' ');
}
//! Highlight the current menu selection using indicator characters
/*!
Draw characters bracketing the current menu selection.
*/
void highlight_current_menu() {
lcd.setCursor(menu_col[selected_menu_idx][0], 1);
lcd.write(0x7E);
lcd.setCursor(menu_col[selected_menu_idx][1], 1);
lcd.write(0x7F);
}
//! Check for left and right button clicks and update the menu index as needed.
void handle_menu() {
if (clicked_buttons & (BUTTON_LEFT | BUTTON_UP)) {
selected_menu_idx = (selected_menu_idx > 0) ? selected_menu_idx - 1 : 3;
} else if (clicked_buttons & (BUTTON_RIGHT | BUTTON_DOWN)) {
selected_menu_idx = (selected_menu_idx < 3) ? selected_menu_idx + 1 : 0;
}
}
//! Initial game state, draw the splash screen.
void begin_splash_screen() {
lcd.clear();
lcd.setBacklight(TEAL);
lcd.print(F("HUNT THE WUMPUS"));
state = animate_splash_screen;
}
//! Animate the splash screen.
/*!
Blink the text "PRESS SELECT" and wait for the user to press the select button.
*/
void animate_splash_screen() {
static boolean blink = true;
static unsigned long last_blink_time;
if (time - last_blink_time >= 1000) {
lcd.setCursor(0, 1);
if (blink) {
lcd.write(0x7E);
lcd.print(F(" PRESS SELECT "));
lcd.write(0x7F);
} else {
lcd.print(F(" "));
}
blink = !blink;
last_blink_time = time;
}
if (clicked_buttons & BUTTON_SELECT) {
state = start_game;
}
}
//! Put the given hazard in a random room
/*
The function generates a random room into which it puts the given hazard.
It keeps trying until it finds a room that doesn't already have another
hazard.
*/
void init_hazard(HazardType hazard)
{
for(;;) {
int index = random(20);
if (!hazards[index])
{
hazards[index] = (uint8_t)hazard;
break;
}
}
}
//! Initialize a new game.
/*
Randomize locations and reset variables.
*/
void start_game() {
lcd.clear();
for (int i = 0; i < 20; i++) {
hazards[i] = 0;
}
init_hazard(WUMPUS);
init_hazard(PIT);
init_hazard(PIT);
init_hazard(BAT);
init_hazard(BAT);
// Make sure the player starts in a room with no hazards.
// It's not fun to be killed before you make the first move.
do {
player_room = random(20);
} while (hazards[player_room]);
arrow_count = 2;
selected_menu_idx = 0;
state = begin_move_room;
}
//! Delay to show a status before continuing with the room move.
void status_delay() {
if (time - last_state_change_time >= 3000) {
state = begin_move_room;
}
}
//! Check for hazards when a player changes rooms
void begin_move_room() {
switch (hazards[player_room]) {
case BAT:
state = begin_bat_move;
break;
case PIT:
state = game_over_pit;
break;
case WUMPUS:
state = game_over_wumpus;
break;
default:
state = enter_new_room;
break;
}
}
// -------------------------------------------------------------------------------
// Bat states
// -------------------------------------------------------------------------------
void begin_bat_move() {
lcd.clear();
lcd.setBacklight(BLUE);
lcd.write(BAT_ICON_IDX);
lcd.setCursor(5, 0);
lcd.print(F("Bats!"));
lcd.setCursor(0, 1);
lcd.write(BAT_ICON_IDX);
state = animate_bat_move;
}
void animate_bat_move() {
static unsigned long last_animate_time;
if (time - last_animate_time > 200) {
lcd.scrollDisplayRight();
last_animate_time = time;
}
if (time - last_state_change_time >= 3000) {
state = end_bat_move;
}
}
void end_bat_move() {
hazards[player_room] = 0;
init_hazard(BAT);
player_room = random(20);
state = begin_move_room;
}
// -------------------------------------------------------------------------------
// Moving states / functions
// -------------------------------------------------------------------------------
void enter_new_room() {
int adjacent_hazards = NONE;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Room "));
print_cave_number(player_room);
for (int i=0; i<3; i++) {
adjacent_hazards |= hazards[cave[player_room][i]];
}
lcd.print(' ');
if (adjacent_hazards & WUMPUS) {
lcd.write(WUMPUS_ICON_IDX);
} else {
lcd.print(' ');
}
if (adjacent_hazards & BAT) {
lcd.write(BAT_ICON_IDX);
} else {
lcd.print(' ');
}
if (adjacent_hazards & PIT) {
lcd.write(PIT_ICON_IDX);
} else {
lcd.print(' ');
}
lcd.setCursor(14, 0);
lcd.write(ARROW_ICON_IDX);
lcd.print(arrow_count);
if (adjacent_hazards) {
lcd.setBacklight(YELLOW);
} else {
lcd.setBacklight(TEAL);
}
lcd.setCursor(1, 1);
for (int i=0; i<3; i++) {
print_cave_number(cave[player_room][i]);
lcd.print(' ');
}
selected_menu_idx = 0;
lcd.setCursor(menu_col[selected_menu_idx][0], 1);
lcd.print('[');
lcd.setCursor(menu_col[selected_menu_idx][1], 1);
lcd.print(']');
state = begin_input_move;
}
void begin_input_move() {
lcd.setCursor(0, 0);
lcd.print(F("Room "));
print_cave_number(player_room);
lcd.print(' ');
lcd.setCursor(10, 1);
lcd.print(F("Shoot"));
state = input_move;
}
void input_move() {
if (clicked_buttons) {
clear_current_menu();
if (clicked_buttons & BUTTON_SELECT) {
if (selected_menu_idx < 3) {
player_room = cave[player_room][selected_menu_idx];
state = begin_move_room;
} else {
state = begin_input_arrow;
}
} else {
handle_menu();
}
highlight_current_menu();
}
}
// -------------------------------------------------------------------------------
// Arrow shooting states / functions
// -------------------------------------------------------------------------------
void begin_input_arrow() {
lcd.setBacklight(WHITE);
lcd.setCursor(0, 0);
lcd.print(F("Shoot at"));
lcd.setCursor(10, 1);
lcd.print(F("Move "));
state = input_arrow;
}
void input_arrow() {
if (clicked_buttons) {
clear_current_menu();
if (clicked_buttons & BUTTON_SELECT) {
if (selected_menu_idx < 3) {
arrow_room = cave[player_room][selected_menu_idx];
state = being_shooting_arrow;
} else {
state = cancel_input_arrow;
}
} else {
handle_menu();
}
highlight_current_menu();
}
}
void cancel_input_arrow() {
int adjacent_hazards = NONE;
for (int i=0; i<3; i++) {
adjacent_hazards |= hazards[cave[player_room][i]];
}
if (adjacent_hazards) {
lcd.setBacklight(YELLOW);
} else {
lcd.setBacklight(TEAL);
}
state = begin_input_move;
}
void being_shooting_arrow() {
lcd.clear();
lcd.setBacklight(VIOLET);
lcd.print(F(">-->"));
arrow_count--;
state = animate_shooting_arrow;
}
void animate_shooting_arrow() {
static unsigned long last_animate_time;
if (time - last_animate_time > 200) {
lcd.scrollDisplayRight();
last_animate_time = time;
}
if (time - last_state_change_time >= 3000) {
if (hazards[arrow_room] == WUMPUS) {
state = game_over_win;
} else {
state = arrow_missed;
}
}
}
void arrow_missed() {
lcd.clear();
lcd.print(F("Missed..."));
if (arrow_count <= 0) {
state = game_over_arrow;
} else {
state = status_delay;
}
}
// -------------------------------------------------------------------------------
// Game over states / functions
// -------------------------------------------------------------------------------
void draw_game_over_screen(uint8_t backlight, const __FlashStringHelper *message, uint8_t icon) {
lcd.clear();
lcd.setBacklight(backlight);
lcd.print(message);
lcd.setCursor(0, 1);
lcd.write(icon);
lcd.setCursor(3, 1);
lcd.print(F("GAME OVER"));
lcd.setCursor(15, 1);
lcd.write(icon);
}
void game_over_arrow() {
draw_game_over_screen(RED, F(" Out of arrows"), ARROW_ICON_IDX);
state = game_over_delay;
}
void game_over_pit() {
draw_game_over_screen(RED, F(" Fell in a pit"), PIT_ICON_IDX);
state = game_over_delay;
}
void game_over_wumpus() {
draw_game_over_screen(RED, F("Eaten by Wumpus"), WUMPUS_ICON_IDX);
state = game_over_delay;
}
void game_over_win() {
draw_game_over_screen(GREEN, F(" You win!"), WUMPUS_ICON_IDX);
state = game_over_delay;
}
void game_over_delay() {
if (time - last_state_change_time >= 3000) {
state = begin_splash_screen;
}
}