Exercise: Write classes to enable you to play Owl 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!

Category ==> Visual Basic  (45 exercises)
Topic ==> Creating classes  (4 exercises)
Level ==> Harder than average
Course ==> Adv VB | Classes & LINQ
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.

Right-click on your project in Solution Explorer and choose to add the form from the above folder (you can alternatively press SHIFT + ALT + A ).

When you set this to be your start-up form and run your application, you should see this:

Owl pairs form

You should see a form containing 18 greyed out owls, with only the Deal button enabled.

When you click on the Deal button, your system should randomly assign 9 owl pictures to get something like this:

Random owls

Each time you "deal" the owls you'll get a different combination.

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

Playing game

Here someone has already guessed 3 pairs, but the fourth guess at the bottom right is wrong, and the unmatched pair will revert back to grey in a second.

When a user has detected all of the pairs, your system should show a congratulatory message then close down the form.  Your task is to add code to this form so that you can play pairs! 

To start with, the 9 owl pictures that you'll need are in the folder above.  To add them, right-click on your project to show its properties, click on the Resources tab (as below) then add your existing image files:

Adding images

Select the menu option shown to add images to your project.

Visual Studio should add the images that you select, and give them names:

Images of owls

The images are added to a folder called Resources.

You can assign an image to a button in code like this:

' show an owl on a button

Dim b As Button = CType(sender, Button)

b.Image = My.Resources.owl1

In this example, we convert the sender of the event to a button, then change its Image property to be the resource called owl1.

You can use the Tag property of each button to keep track of which image is assigned to it (this property isn't used for any other purpose).

At some stage you'll need to loop over all the buttons, doing something to each.  Here's one way to do this (this example would remove the image from every button):

' remove images from all buttons

Dim i As Integer

For i = 1 To 18

Dim b As Button = CType(Me.Controls("Button" + i.ToString), Button)

b.Image = Nothing

 

Next i

There's no right or wrong way to write the classes for this project, but the answer uses the following two classes:

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

After an unsuccessful guess, you'll want to hide the two buttons chosen.  One command to wait a second to give your user time to view the choices they've made would be:

' if not a match, hide both after a bit of a wait

System.Threading.Thread.Sleep(1000)

To ensure that you don't allocate more than two owls to any combination of buttons initially, the system uses this variable:

'holds unassigned buttons

OwlsLeft = "112233445566778899"

There are many more elegant ways to do this, without doubt, and you should feel free to use one of them if you can think of it!

And with all these hints - good luck!

This page has 0 threads Add post