共用方式為


步驟 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 = 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();
    }
    

    這個方法會在 Visual C# 中使用另一個 foreach 迴圈,在 Visual Basic 中使用 For Each 迴圈,以通過 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
        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. 儲存並執行程式。 玩遊戲並將所有的圖示配對。 當您贏了,程式會顯示 MessageBox (如下列圖片所示),然後關閉此方塊。

    比對遊戲和 MessageBox

    配對遊戲和訊息方塊

若要繼續或檢視