WISE OWL EXERCISES
VISUAL C# EXERCISES
- Creating forms (4)
- Coding form events (1)
- Laying out your code (2)
- C# variables (4)
- Enumerations and constants (2)
- Conditions (2)
- Modular code (3)
- Arrays (2)
- Looping (2)
- Files and folders (3)
- Properties in C# (3)
- Using lists (3)
- Validating forms (6)
- Toolbars, menus and status bars (1)
- FileDialogs and StreamReaders (1)
- Debugging and trapping errors (1)
- Introduction to DataGridViews (1)
- DataGridView events (3)
- Complex DataGridViews (2)
- Creating classes (4)
- The form as a class (1)
- Data structures (6)
- Inheritance (5)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (2)
- Advanced LINQ (2)
- Entity Frameworks (1)
- LINQ with Entity Frameworks (4)
- Grouping using LINQ (2)
- LINQ to SQL (2)
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.
The answer to the exercise will be included and explained if you attend one of more of the courses listed below!
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:
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.
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:
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
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:
|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!