ALL SOFTWARE EXERCISES
- EXERCISES HOME PAGE (893)
- Access 2010 (66)
- Access VBA Macros (17)
- Advanced VBA (29)
- DAX (0)
- Excel (0)
- Excel 2010 (83)
- Excel 2013 (10)
- Excel 2016 (0)
- Excel VBA Macros (37)
- Power BI - Excel 2013 (25)
- Power BI - Excel 2016 (24)
- Power BI Desktop (26)
- PowerPivot 2010 (26)
- Report Builder (42)
- SQL (192)
- SSAS - multidimensional (21)
- SSAS - tabular (29)
- SSIS Integration Services (40)
- SSRS 2012 (53)
- SSRS 2016 (43)
- Visual Basic (45)
- Visual C Sharp (65)
- WPF - Visual C# (20)
VISUAL C SHARP EXERCISES
- Creating forms (2)
- 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 (3)
- 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 (2)
- The form as a class (1)
- Data structures (4)
- Inheritance (4)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (1)
- Advanced LINQ (3)
- LINQ to SQL models (1)
- Writing LINQ to SQL (2)
- Grouping using LINQ (2)
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 the course 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!