# Arduino "Hunt The Wumpus"

## Overview & Parts

![](https://cdn-learn.adafruit.com/assets/assets/000/002/074/medium800/lcds___displays_Hunt_The_Wumpus_Splash.jpg?1396779005)

You can relive the early days of computer gaming on your Arduino&nbsp;with&nbsp;[Hunt the Wumpus](http://en.wikipedia.org/wiki/Hunt_the_Wumpus). &nbsp;This game is a particularly good fit for&nbsp;the RGB LCD shield:

- Different screens can have different backlight colors
- The LCD is large enough for selecting caves to move to or shoot into
- The Wumpus, bat, and pit can all have custom characters
- The D-Pad is well suited to handling menu navigation

You will need:  

- [Adafruit RGB LCD Shield](https://www.adafruit.com/products/716)  
- [Arduino Uno](https://www.adafruit.com/products/50)  

  
![](https://cdn-learn.adafruit.com/assets/assets/000/002/075/medium800/lcds___displays_Hunt_The_Wumpus_Room.jpg?1396779010)

Of course, you do run the risk of taking a wrong turn and&nbsp;being eaten by a Wumpus!

![](https://cdn-learn.adafruit.com/assets/assets/000/002/076/medium800/lcds___displays_Hunt_The_Wumpus_Game_Over.jpg?1396779019)

# Arduino "Hunt The Wumpus"

## Code

While coding Hunt the Wumpus, I ran into a problem with one of my functions. &nbsp;I wanted to use an [enum](http://en.wikipedia.org/wiki/Enumerated_type#C_and_syntactically_similar_languages) to represent different hazards (bats, pits, Wumpus) and have a common function to check a given cave for hazards and return the enum of the first one found. &nbsp;This helps with showing hazards in neighboring caves as well as checking a cave before the player moves into it.  
  
I started by declaring a function which returns an enum:

```auto
HazardType check_for_hazards(uint8_t room_idx) {
  if (room_idx == bat1_room || room_idx == bat2_room) {
    return BAT;
  } else if (room_idx == pit1_room || room_idx == pit2_room) {
    return PIT;
  } else if (room_idx == wumpus_room) {
    return WUMPUS;
  } else {
    return NONE;
  }
}
```

Unfortunately, this resulted in the following error message:  
```auto
Hunt_The_Wumpus:-1: error: 'HazardType' does not name a type
```

It turns out that this is a known issue with a&nbsp;[documented workaround](http://arduino.cc/playground/Code/Enum)&nbsp;and the fix was as simple as adding a header file&nbsp;Hunt\_The\_Wumpus.h&nbsp;to my project which contains the enum declaration and a function prototype:```auto
enum HazardType { NONE=0, BAT=1, PIT=2, WUMPUS=4 };

HazardType check_for_hazards(uint8_t room_idx);
```

And including the file at the top of the main Hunt\_The\_Wumpus.ino file:

```auto
#include "Hunt_The_Wumpus.h"
```

Another interesting area of the code is related to reading button presses. &nbsp;The RGB LCD shield library provides a function for reading which buttons are currently pressed as a bitmask. &nbsp;For menu navigation, it's important to understand clicks. &nbsp;By using a static unit8\_t to store the last state of the buttons, it's possible to determine which buttons have been pressed and then released. &nbsp;

```auto
void read_button_clicks() {
  static uint8_t last_buttons = 0;
  
  uint8_t buttons = lcd.readButtons();
  clicked_buttons = (last_buttons ^ buttons) & (~buttons);
  last_buttons = buttons;
}
```

# Arduino "Hunt The Wumpus"

## Downloads

[Download the latest code on Github](https://github.com/adafruit/Hunt-The-Wumpus)  
  
Here's a 'diff' for monochrome displays!  
```auto
*** Hunt_The_Wumpus.ino.orig 2012-11-24 12:43:10.936042927 -0500
--- Hunt_The_Wumpus.ino 2012-11-24 13:14:11.908126852 -0500
***************
*** 42,47 ****
--- 42,48 ----
#include
#include "Hunt_The_Wumpus.h"

+ #define MONO 1 // cheapskate with no RGB (or they were out of stock)

//! Map of the cavern layout.
/*!
***************
*** 270,276 ****
--- 271,281 ----
//! Initial game state, draw the splash screen.
void begin_splash_screen() {
lcd.clear();
+ #ifndef MONO
lcd.setBacklight(TEAL);
+ #else
+ lcd.setBacklight(RED); // MONO backlight ON is on RED line
+ #endif
lcd.print(F("HUNT THE WUMPUS"));

state = animate_splash_screen;
***************
*** 357,363 ****
--- 362,370 ----

void begin_bat_move() {
lcd.clear();
+ #ifndef MONO
lcd.setBacklight(BLUE);
+ #endif
lcd.write(BAT_ICON_IDX);
lcd.setCursor(5, 0);
lcd.print(F("Bats!"));
***************
*** 429,439 ****
--- 436,448 ----
lcd.write(ARROW_ICON_IDX);
lcd.print(arrow_count);

+ #ifndef MONO
if (adjacent_hazards) {
lcd.setBacklight(YELLOW);
} else {
lcd.setBacklight(TEAL);
}
+ #endif

lcd.setCursor(1, 1);
for (int i=0; i-->"));

arrow_count--;
***************
*** 575,581 ****
--- 590,598 ----

void draw_game_over_screen(uint8_t backlight, __FlashStringHelper *message, uint8_t icon) {
lcd.clear();
+ #ifndef MONO
lcd.setBacklight(backlight);
+ #endif
lcd.print(message);
lcd.setCursor(0, 1);
lcd.write(icon); 
```


## Related Guides

- [IR Sensor](https://learn.adafruit.com/ir-sensor.md)
- [MIDI for Makers](https://learn.adafruit.com/midi-for-makers.md)
- [How to Build a Testing Jig](https://learn.adafruit.com/how-to-build-a-testing-fixture.md)
- [Adafruit RS232 Pal](https://learn.adafruit.com/adafruit-rs232-pal.md)
- [Adafruit PCF8574 I2C GPIO Expander](https://learn.adafruit.com/adafruit-pcf8574.md)
- [Adafruit ESP32-S2 Reverse TFT Feather](https://learn.adafruit.com/esp32-s2-reverse-tft-feather.md)
- [Control Electronics with your Brain using NextMind](https://learn.adafruit.com/control-electronics-with-your-brain-using-nextmind.md)
- [Arduino "Hunt The Wumpus"](https://learn.adafruit.com/arduino-hunt-the-wumpus.md)
- [Adafruit TRRS Trinkey](https://learn.adafruit.com/adafruit-trrs-trinkey.md)
- [Adafruit DACx578 - 8 x Channel I2C DAC](https://learn.adafruit.com/adafruit-dac7578-8-x-channel-12-bit-i2c-dac.md)
- [Adafruit AS7331 UV / UVA / UVB / UVC Sensor](https://learn.adafruit.com/adafruit-as7331-uv-uva-uvb-uvc-sensor.md)
- [Adafruit IO IOT Hub with the Adafruit FunHouse](https://learn.adafruit.com/adafruit-io-hub-with-the-adafruit-funhouse.md)
- [Using Raspberry Pi Pico and Pico 2 with Arduino](https://learn.adafruit.com/using-raspberry-pi-pico-pico-2-with-arduino.md)
- [Arduboy Game Controller Hack](https://learn.adafruit.com/arduboy-game-controller.md)
- [Adafruit LTR-329 and LTR-303 Light Sensors](https://learn.adafruit.com/adafruit-ltr-329-ltr-303.md)
