While coding Hunt the Wumpus, I ran into a problem with one of my functions.  I wanted to use an enum 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.  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:
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:
Hunt_The_Wumpus:-1: error: 'HazardType' does not name a type
It turns out that this is a known issue with a documented workaround and the fix was as simple as adding a header file Hunt_The_Wumpus.h to my project which contains the enum declaration and a function prototype:
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:
#include "Hunt_The_Wumpus.h"
Another interesting area of the code is related to reading button presses.  The RGB LCD shield library provides a function for reading which buttons are currently pressed as a bitmask.  For menu navigation, it's important to understand clicks.  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.  
void read_button_clicks() {
  static uint8_t last_buttons = 0;
  
  uint8_t buttons = lcd.readButtons();
  clicked_buttons = (last_buttons ^ buttons) & (~buttons);
  last_buttons = buttons;
}

This guide was first published on Oct 03, 2012. It was last updated on Oct 03, 2012.

This page (Code) was last updated on Nov 30, 2021.

Text editor powered by tinymce.