Step 8: Add a Method to Verify Whether the Player Won
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
You've created a fun game, but it needs an additional item to finish it. The game should end when the player wins, so you need to add a CheckForWinner()
method to verify whether the player won.
To add a method to verify whether the player won
Add a
CheckForWinner()
method to the bottom of your code, below thetimer1_Tick()
event handler, as shown in the following code./// <summary> /// Check every icon to see if it is matched, by /// comparing its foreground color to its background color. /// If all of the icons are matched, the player wins /// </summary> private void CheckForWinner() { // Go through all of the labels in the TableLayoutPanel, // checking each one to see if its icon is matched foreach (Control control in tableLayoutPanel1.Controls) { Label iconLabel = control as Label; if (iconLabel != null) { if (iconLabel.ForeColor == iconLabel.BackColor) return; } } // If the loop didn’t return, it didn't find // any unmatched icons // That means the user won. Show a message and close the form MessageBox.Show("You matched all the icons!", "Congratulations"); Close(); }
''' <summary> ''' Check every icon to see if it is matched, by ''' comparing its foreground color to its background color. ''' If all of the icons are matched, the player wins ''' </summary> Private Sub CheckForWinner() ' Go through all of the labels in the TableLayoutPanel, ' checking each one to see if its icon is matched For Each control In TableLayoutPanel1.Controls Dim iconLabel = TryCast(control, Label) If iconLabel IsNot Nothing AndAlso iconLabel.ForeColor = iconLabel.BackColor Then Exit Sub Next ' If the loop didn't return, it didn't find ' any unmatched icons ' That means the user won. Show a message and close the form MessageBox.Show("You matched all the icons!", "Congratulations") Close() End Sub
The method uses another
foreach
loop in Visual C# orFor Each
loop in Visual Basic to go through each label in the TableLayoutPanel. It uses the equality operator (==
in Visual C# and=
in Visual Basic) to check each label's icon color to verify whether it matches the background. If the colors match, the icon remains invisible, and the player hasn't matched all of the icons remaining. In that case, the program uses areturn
statement to skip the rest of the method. If the loop gets through all of the labels without executing thereturn
statement, that means that all of the icons on the form were matched. The program shows a MessageBox to congratulate the player on winning, and then calls the form'sClose()
method to end the game.Next, have the label's Click event handler call the new
CheckForWinner()
method. Be sure that your program checks for a winner immediately after it shows the second icon that the player chooses. Look for the line where you set the second chosen icon's color, and then call theCheckForWinner()
method right after that, as shown in the following code.// If the player gets this far, the timer isn't // running and firstClicked isn't null, // so this must be the second icon the player clicked // Set its color to black secondClicked = clickedLabel; secondClicked.ForeColor = Color.Black; // Check to see if the player won CheckForWinner(); // If the player clicked two matching icons, keep them // black and reset firstClicked and secondClicked // so the player can click another icon if (firstClicked.Text == secondClicked.Text) { firstClicked = null; secondClicked = null; return; }
' If the player gets this far, the timer isn't ' running and firstClicked isn't Nothing, ' so this must be the second icon the player clicked ' Set its color to black secondClicked = clickedLabel secondClicked.ForeColor = Color.Black ' Check to see if the player won CheckForWinner() ' If the player clicked two matching icons, keep them ' black and reset firstClicked and secondClicked ' so the player can click another icon If firstClicked.Text = secondClicked.Text Then firstClicked = Nothing secondClicked = Nothing Exit Sub End If
Save and run the program. Play the game and match all of the icons. When you win, the program displays a congratulatory MessageBox (as shown in the following picture), and then closes the box.
Matching game with MessageBox
To continue or review
To go to the next tutorial step, see Step 9: Try Other Features.
To return to the previous tutorial step, see Step 7: Keep Pairs Visible.