Step 8: Add a Method to Verify Whether the Player Won

You created a fun game, but it needs an additional item. 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

  1. Add a CheckForWinner() method to your form, 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 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
    
    /// <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();
    }
    

    The method uses another foreach loop in Visual C# or For 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 a return statement to skip the rest of the method. If the loop gets through all of the labels without executing the return statement, that means that all of the icons were matched. The program shows a MessageBox, and then calls the form's Close() method to end the game.

  2. Next, have the label's Click event handler call the new CheckForWinner() method. Be sure that your program checks for a winner after it shows the second icon that the player clicks. Look for the line where you set the second clicked icon's color, and then call the CheckForWinner() 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 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
    
    // 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;
    }
    
  3. Save and run the program. Play the game and match all of the icons. When you win, the program displays a MessageBox (as shown in the following picture), and then closes the box.

    Matching game with MessageBox

    Matching game with MessageBox

To continue or review