步骤 8:添加方法以验证玩家是否获胜

您已创建了一个有趣的游戏,但它需要添加附加项目。该游戏应当在玩家获胜时结束,因此您需要添加 CheckForWinner() 方法以验证玩家是否获胜。

添加方法以验证玩家是否获胜

  1. CheckForWinner() 方法添加到窗体,如下面的代码所示。

    ''' <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 As Label = TryCast(control, Label)
            If iconLabel IsNot Nothing Then
                If (iconLabel.ForeColor = iconLabel.BackColor) Then
                    Return
                End If
            End If
        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();
    }
    

    该方法使用 foreach 循环(Visual C# 中)或 For Each 循环(Visual Basic 中)遍历 TableLayoutPanel 中的每个标签。它使用相等运算符(在 Visual C# 中为 ==,在 Visual Basic 中为 =)检查每个标签的图标颜色以验证它是否与背景匹配。如果颜色匹配,图标将保持不可见,玩家还没有匹配所有剩余的图标。在这种情况下,程序使用 return 语句跳过其余方法。如果循环遍历所有标签而不执行 return 语句,则意味着所有图标都匹配。程序显示一个 MessageBox,然后调用窗体的 Close() 方法来结束游戏。

  2. 接下来,让标签的 Click 事件处理程序调用新的 CheckForWinner() 方法。确保程序在显示玩家单击的第二个图标后检查是否有赢家。查找设置第二个单击图标颜色的行,然后紧靠其后调用 CheckForWinner() 方法,如下面的代码所示。

    ' 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
        Return
    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. 保存并运行程序。玩游戏并匹配所有图标。当您获胜时,程序显示一个 MessageBox(如下图所示),然后关闭该框。

    具有 MessageBox 的匹配游戏

    具有 MessageBox 的匹配游戏

继续或查看