Whack-a-mole
to continue. (After that any entries made into the worksheet will be automatically saved, and you can re-load the partially-completed worksheet in future – at which point you will be asked to auto-save it again).

Preliminaries

Total hints used: /

Iteration 1 – Create an array of strings

Any array is a simple form of 'data structure' – which is a mechanism for holding multiple, related, data elements (also called 'items') such that they may be read or modified all together, but also read or modified individually. We are going to write a simple version of the game 'Whack-a-mole', using an array to represent a line of ten holes from which a mole might briefly appear.

Total hints used: /

Iteration 2 – put a value into a specific element

Total hints used: /

Iteration 3 – display the array of holes in a more useful way

Total hints used: /

Iteration 4 – put the mole in a random hole

Total hints used: /

Iteration 5 – Move the mole randomly, once a second

Total hints used: /

Iteration 6 – Read a keypress dynamically and whack the mole if the digit matches

Total hints used: /

Iteration 7 – Keep the score

Total hints used: /

Congratulations! You've completed the exercise, written a simple version of Whack-a-mole, and learned how to use an array.

Optional extensions

If you have time, try to refine and/or extend the game in the following ways:


This worksheet is copyright © Richard Pawson 2025, and protected by the Creative Commons license: Attribution-NonCommercial-NoDerivatives 4.0 International. If you copy and modify this worksheet you may not distribute your modified version (outside your own teaching institution) without the author's permission. Please email the author to report errors or suggest improvements.