Thursday, July 24, 2014

Whack A Mole Project

I’ve done variations in Whack A Mole over the years. I did a Whack Something Game for Windows Phone 7 some years ago for example. But somehow I put it aside and didn’t think about it when I was teaching the last year and a half. This week as the National CS Principles Summit this project came up in one of the presentations. This struck me as a particularly good idea for me to bring back into my own practice. So obviously I started coding. I came up with this.
What I have decided I want to do is use this for is to teach objects and classes. I created a MoleHole class. I may have to revisit that name before I go to students though. Suggestions are welcome. The reason for creating a class is that I could allow the class itself to handle showing and hiding the moles. My hope is that students will understand that having the object take care of itself will make things much easier.
The first think I did was to create  (using Paint) a couple of images – one to show a “mole” and the other an empty “hole.”
NoMole MoleNothing fancy and I may ask students to create their own. So many of them are much more artistic than I am. Doubtless one of them will use a picture of me if history is any indication.
Then I created a new User Control. I’m using Visual Studio and C# for this sample by the way. Once created I added the images to the project as resources and loaded the empty hole image as the initial image for the object background.
I have very little code in  the class. The constructor just sets the initial value of the Tag property which I could have avoided by putting that in the properties when I added the initial background image. The one method I wrote changes the background image and Tag value.
        public void Swap()
            if ((string)Tag == "Empty")
                BackgroundImage = global::WhackAMole.Properties.Resources.Mole;
                Tag = "Full";
                BackgroundImage = global::WhackAMole.Properties.Resources.NoMole;
                Tag = "Empty";

Very simple. I add the objects to the form dynamically and included in that is setting the event handler for the click event. The code for the event handler checks to see if there is a mole showing and if there is it increments the score and swaps out the image by asking the object to do it.

        private void Whack(object sender, EventArgs e)
            MoleHole hit = (MoleHole)sender;
            if ((string)hit.Tag == "Full")
                ScoreLabel.Text = score.ToString();

The mole appears or disappears based on a timer firing. It randomly picks a mole hole object and switches its image and tag, again, by asking the object to do it.

        private void timer1_Tick(object sender, EventArgs e)

Overall, including dynamically creating and loading the mole holes there are something around 30 lines of code. I think I can get students to manage this. And then I’ll want them to make it better, fancier, more interesting. We’ll see how it goes.


Mike Zamansky said...

Ahh Whack a Mole - the only carnival game from which I was asked to leave because I was winning too much....

To put in the "it seemed like a good idea at the time" - in my systems classes, many of the students final projects are networked.

One year, one group decided to write Whack a Mole. Each computer in the lab would be a hole and something would happen to indicate when the mole(s) would pop up.

I suggested "hey, wouldn't it be cool if you popped the cdrom trays when the moles popped up?"

As I said, it seemed like a good idea at the time.

Garth said...

This first year I did this project I thought it looked like an easy one so I sat in front of the computer and started coding. Opps. Plan, always plan before coding. This is the project that taught me not to give a seemingly easy project to my kids without working on the planning with them first. This is the project that taught me that the true objective in my programming classes is not typing code but building a project away from the computer.

Anonymous said...


I would suggest not to use Tag for storing the state of your class. Using an enum would be much better. I know that this forces you to explain enums. On the other hand various objects have a Tag property. Once your students learned to abuse that Tag property for storing all sorts of information they probably won't botter creating meaningful properties on their own. With your tag approach you might push them in the wrong direction.

Regards, Sascha

Alfred C Thompson II said...

I've never been sure what the Tag property is really there for. I should probably add a property instead and maybe that is what I'll do next.

Anonymous said...

The tag property comes in handy if you have a class, that you didn't implement yourself and don't want to subclass. I use the tag myself but only for storing data for a very short time. A good example is the event that is fired just before a popup menu is shown. During the event dispatch I store some data in the tag that I would need in onclick event. That is kind of lazy because I could create a variable for it.

Regards, Sascha