Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | Visual C# (55 exercises) |
Topic ==> | Creating classes (3 exercises) |
Level ==> | Harder than average |
Subject ==> | C# training |
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.
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:
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:
One iteration of the program - each one will look different.
When you click on the Start button you can start playing:
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.
The 9 owl pictures that you'll need are in the folder above. Having added them into your project, you can drag them into a resources file:
Select the images and drag them into an open .resx file.
Thereafter you could access an image to assign to a button using something like this:
public static System.Drawing.Image ImageToUse(int OwlNumber)
{
switch (OwlNumber)
{
case 1:
return Resource1.owl1;
case 2:
return Resource1.owl2;
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 ...
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;
}
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)
System.Threading.Thread.Sleep(1000);
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 find other training resources for the subject of this exercise here:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.