User Tools

Site Tools


Whack A Mole


In this lab you will create a touch-screen version of the Whack A Mole game. In the arcade game, moles pop out of holes and your job is to “whack” them by hitting them before they go back into their hole. In our version of the game, the moles are drawn on a screen. When they appear, you touch them with your finger to “whack” them. Perhaps we should rename the game “Pet the Mole”.

You will need to support three different configurations of the game: 9 moles, 6 moles, and 4 moles. The user chooses the configuration by sliding the switches appropriately. Watch the videos in the Videos Section to view the three different board configurations.

As was done with prior labs, the Whack a Mole game is conceptually organized and developed in two parts: display (wamDisplay) and control (wamControl). wam is an abbreviation for Whack A Mole and it helps shorten file-names, function-names, variable-names, etc.


  1. More practice with state machines.
  2. More practice with the coding standard.
  3. Learn how to use malloc() and free() to implement dynamically-sized arrays.
  4. Learn to make your code more flexible and responsive to user input by changing the mole layout according to user input via the slide switches. Your game will support at least three layouts: 9 moles, 6 moles, and 4 moles.
  5. More practice using the rand() and srand() functions.
  6. More practice with structs.
  7. Learn how to make your game more difficult as play progresses.
  8. More exposure to different implementation approaches (the implementation style for this game moves complexity to the display software and simplifies overall control).
  9. Whack Moles :-)

Program Organization

The organization of the game is similar to past labs where the display code is separated from the control code. However, in this lab, the display code will be significantly smarter than in past labs. Responsibilities for the display code include:

  1. drawing the mole display,
  2. supporting three different configurations of the mole board: 9 moles, 6 moles, and 4 moles,
  3. activating random moles,
  4. keeping track of the number of active moles,
  5. drawing and erasing moles as they wake up and go dormant, or get whacked.
  6. determining if a mole has been whacked by the user,
  7. keeping and displaying a tally of hits, misses, and the current level of difficulty.

The controller is quite simple (my controller only has three states). The responsibilities of the controller include:

  1. waiting for the user to touch the display,
  2. waiting for the touch analog-to-digital converters to settle,
  3. sending the touch coordinates to the display code.


Four files are provided: wamControl.h, wamDisplay.h, wamDisplay.c, and wamMain.c. Of these all but wamDisplay.c are complete and should not be modified by you. Some code is found in wamDisplay.c to get you started using malloc() to implement a dynamic array and to provide a definition for mole-info records (more on this later). All of these files can be found near the bottom of this lab in the Source Code Section.

Note: Much of the functionality of the various functions is described in the commented code in the provided files. Carefully read these comments as it will save you a lot of time.

Overall Implementation Strategy

This following sections discuss how the wamDisplay and wamControl code are organized. Links to source code are also provided.


  1. You must create a Whack a Mole game that supports 9, 6, and 4 moles with behavior that closely resembles the game shown in the videos. The number of moles is selected before starting the game by pushing the slide switches as discussed in wamMain.c. Pushing the slide switches should not interrupt game play.
  2. You must use the provided files and implement the functions advertised in the .h files.
  3. Use the .h files as provided, treat them as a software specification and only modify #define statements or add include statements as necessary. Otherwise, no modifications of the .h files are allowed. Also complete the wamDisplay.c file.
  4. The game can be forced to quit by pressing any push-button. Upon quitting, the game should go to the game-over screen.
  5. The game layout must be very close to the one shown in the video. It must also have a dedicated score screen at the bottom of the screen that is very similar to the one shown in the video. The score screen must display hit counts, miss counts, and the current difficulty level.
  6. Game play should become progressively more difficult during game play. Levels should automatically advance during game play as the hit-count increases. You make the game more difficult in at least two ways: increasing the number of active moles and decreasing the time they are present (ticksUntilDormant).
  7. The current difficulty level is represented on the score screen.
  8. Increment the Miss count each time a mole appears and then disappears without being whacked.
  9. Increment the Hit count each time the user successfully whacks a mole.
  10. You must have a start-screen and a game-over screen as shown in the video.
  11. You must use malloc() and free() to allocate and deallocate memory for the mole-info records when resizing the game.
  12. You must use the wamControl_setMsPerTick() function to inform the controller how many ms transpire between each tick. In turn, the wamDisplay software must use wamControl_getRandomMoleAsleepInterval() and wamControl_getRandomMoleAwakeInterval() to set the tick counts for ticksUntilAwake and ticksUntilDormant variables in the mole-info record.


The game will be completed in two milestones.

  • Milestone 1: Implement enough of the game so that you can:
    • display the mole screen with the score display for 9, 6, and 4 moles, using the slide switches as described in wamMain.c.
    • Fully implement the game-start and game-over screens.
    • test can be implemented as a separate run_test() function in wamDisplay.c. There is a wamDisplay_runMilestone1_test() function declaration provided for this purpose.
    • See the demonstration video for details.
  • Milestone 2:
    • Finish the game so that it meets all of the requirements.


  • Milestone 1 Passoff: 35%
  • Milestone 2 Passoff: 35%
  • Lab 7 Code Submission: 30%.

Submitting Source Code

Follow this procedure to submit your source code to learning-suite.


Video Demonstration of Milestone 1

Video Demonstration of Complete Game

Other Resources

The web has quite a bit of information on how to use rand() and pointers, including malloc() and free(). Here are a few curated links.

lab_7.txt · Last modified: 2019/04/10 14:15 by hutch