Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim cards As New List(Of Card) 'カードを生成 For i As Integer = 1 To 13 cards.Add(New Card With {.Number = i, .Mark = "スペード"}) cards.Add(New Card With {.Number = i, .Mark = "ダイヤ"}) cards.Add(New Card With {.Number = i, .Mark = "クラブ"}) cards.Add(New Card With {.Number = i, .Mark = "ハート"}) Next cards.Add(New Card With {.Mark = "ババ"}) 'カードをシャッフル Shuffle(cards) 'PictureBox1~12に画像を表示 Dim imagePath As String = Application.StartupPath & "\images" For i As Integer = 1 To 12 Dim pictureBox = DirectCast(Me.Controls("PictureBox" & i.ToString), PictureBox) pictureBox.Image = Image.FromFile(imagePath & "\" & cards(i - 1).FileName) pictureBox.Tag = cards(i - 1) Next '同じ数字の画像を非表示にする For i As Integer = 1 To 12 Dim pictureBox1 = DirectCast(Me.Controls("PictureBox" & i.ToString), PictureBox) If pictureBox1.Visible = False Then Continue For End If Dim card1 = DirectCast(pictureBox1.Tag, Card) For j As Integer = i + 1 To 12 Dim pictureBox2 = DirectCast(Me.Controls("PictureBox" & j.ToString), PictureBox) If pictureBox2.Visible = False Then Continue For End If Dim card2 = DirectCast(pictureBox2.Tag, Card) If card1.Number = card2.Number Then pictureBox1.Visible = False pictureBox2.Visible = False Exit For End If Next Next End Sub ''' <summary>カードをシャッフルします。</summary> Public Sub Shuffle(cards As List(Of Card)) Dim r As New Random For i As Integer = 0 To cards.Count - 1 Dim target As Integer = r.Next(0, cards.Count) Dim temp As Card = cards(target) cards(target) = cards(i) cards(i) = temp Next End Sub End Class Public Class Card Public Property Number As Integer Public Property Mark As String Public ReadOnly Property FileName As String Get Select Case Mark Case "スペード" Return "s" & Number.ToString("00") & ".png" Case "ダイヤ" Return "d" & Number.ToString("00") & ".png" Case "クラブ" Return "c" & Number.ToString("00") & ".png" Case "ハート" Return "h" & Number.ToString("00") & ".png" Case Else Return "joker.png" End Select End Get End Property End Class