Visual C# | Creating classes exercise | Create C# classes to play Pairs

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> Visual C#  (79 exercises)
Version ==> Any version of C#
Topic ==> Creating classes  (4 exercises)
Level ==> Harder than average
Subject ==> C# training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

The idea behind this exercise is to create a game of pairs.  At the end of this explanation of how the game should work some ideas are given - it's up to you whether you take advantage of these!

Let's start with how your system should work.  When you first start, you should see a form containing 18 buttons:

Inital form

Initially only the Deal button is available.

When you click on the Deal button, your system should randomly assign the owl pictures available (see later in this exercise for how to get these) to the buttons as 9 pairs:

The owl buttons

One iteration of the program - each one will look different.

When you click on the Start button you can start playing:

In middle of pairs game

Here we've already got 2 pairs, but the third guess is wrong, and the unmatched pair will revert back to grey in a second.

When you've got all of the pairs, the system should show a congratulatory message, then close down the form.

Using the Pictures

The 9 owl pictures that you'll need are in the folder above.  You can add them in to your project's resources as follows:

Adding images to resources

Right-click on your project's Resources folder and choose to add existing items.

Thereafter you can assign an image to a button using code like this:

b.Image = Properties.Resources.owl1;

You can remove an image by setting the Image property of a button to null.

To keep track of which image is assigned to which button, the answer uses the Tag property of each button, which is a string of text used for no other purpose than to tag any form control.

A suggestion for how to loop over the buttons follows ...

Looping over Buttons

If you know how to loop over collections, do so.  Otherwise, you can loop over the 18 buttons on a form as follows:

// loop over all 18 buttons

int i;

for (i = 1; i <= 18;="">


// refer to the button by name (note that you have to cast the

// control to a button

Button b = (Button)this.Controls["Button" + i.ToString()];


// do something to button (here we remove the image)

b.Image = null;


Classes used

There's no right or wrong way to solve this, but here are the classes used in the answer:

Class How used
PairButton Used to refer to each button.
PairTurn Used for a single turn (ie the action of choosing two pictures).

Some other useful ideas ...

  • After an unsuccessful guess, you'll want to hide the two buttons chosen.  Here's one command to do this:

// wait for 1000 milliseconds (1 second)


  • To allocate the buttons initially, the answer creates this variable:

// holds unassigned buttons

owlsLeft = "112233445566778899";

Whenever an owl picture is assigned, the relevant number is removed from the list.  This ensures each owl is assigned twice, and twice only.  Inelegant, but it works!


Good luck!

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post