次の方法で共有


手順 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() メソッドを呼び出させます。必ず、プログラムでは、プレーヤーがクリックした 2 つ目のアイコンを表示してから、勝者をチェックするようにしてください。クリックされた 2 つ目のアイコンの色を設定している行を探して、次のコードに示すように、そのすぐ後で、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 が表示された絵合わせゲーム

続行または確認するには