Public Class Form1
    Private cnt As Integer = 0
    Private maru, batu As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        initial_setup()
        same_no()
        label10.Text = "正解：" & maru & "  間違い：" & batu & "   未解答：" & (10 - maru - batu)
    End Sub

    ' 数字の設定
    Private Sub initial_setup()
        ' 配列を作成
        Dim select_no(9) As Integer
        Dim rnd As Random = New Random() ' 乱数を作成
        Dim i, j, num As Integer
        Dim flag As Boolean = False

        i = 0
        While (i <= 8)
            num = rnd.Next(9) + 1           ' 乱数で0〜8を作成し1を加えて1〜9にする
            For j = 0 To 8
                If select_no(j) = num Then  ' 同じ数字を2回使わないために
                    flag = True             ' 乱数で作成した数字があるかどうかをチェックする
                End If
            Next
            If flag = False Then            ' 同じ数字がなければ配列に入れる
                select_no(i) = num
                i += 1
            Else
                flag = False                '2回目の数字作成に備え変数をFalseにリセットする
            End If
        End While

        ' 整数を文字列に変換してラベルに設定
        Label1.Text = select_no(0).ToString
        Label2.Text = select_no(1).ToString
        Label3.Text = select_no(2).ToString
        Label4.Text = select_no(3).ToString
        Label5.Text = select_no(4).ToString
        Label6.Text = select_no(5).ToString
        Label7.Text = select_no(6).ToString
        Label8.Text = select_no(7).ToString
        Label9.Text = select_no(8).ToString

        ' ラベルの境界線をリセット
        Label1.BorderStyle = BorderStyle.FixedSingle
        Label2.BorderStyle = BorderStyle.FixedSingle
        Label3.BorderStyle = BorderStyle.FixedSingle
        Label4.BorderStyle = BorderStyle.FixedSingle
        Label5.BorderStyle = BorderStyle.FixedSingle
        Label6.BorderStyle = BorderStyle.FixedSingle
        Label7.BorderStyle = BorderStyle.FixedSingle
        Label8.BorderStyle = BorderStyle.FixedSingle
        Label9.BorderStyle = BorderStyle.FixedSingle

        ' まるばつを非表示にリセット
        PictureBox1.Visible = False
        PictureBox2.Visible = False
    End Sub

    '同じ数字の作成−２つだけ同じ数字を配置する
    Private Sub same_no()
        Dim flag As Boolean = False
        Dim rnd As Random = New Random()                ' 専用のRandomオブジェクトを作成

        While flag = False
            Dim pos As Integer = rnd.Next(9) + 1        ' 表示するLabelを選択
            Dim sameno As String = rnd.Next(9) + 1          ' ペアの片方の数字を作成

            Select Case pos
                Case 1
                    If sameno <> Label1.Text Then       ' 作成した数字とすでにある数字が同じでないかチェック
                        Label1.Text = sameno            ' 同じでなければその数字を使う
                        flag = True
                    End If                              '同じなら違うRavelを使うために	
                Case 2                                  'もう一度ループをまわす
                    If sameno <> Label2.Text Then
                        Label2.Text = sameno
                        flag = True
                    End If
                Case 3
                    If sameno <> Label3.Text Then
                        Label3.Text = sameno
                        flag = True
                    End If
                Case 4
                    If sameno <> Label4.Text Then
                        Label4.Text = sameno
                        flag = True
                    End If
                Case 5
                    If sameno <> Label5.Text Then
                        Label5.Text = sameno
                        flag = True
                    End If
                Case 6
                    If sameno <> Label6.Text Then
                        Label6.Text = sameno
                        flag = True
                    End If
                Case 7
                    If sameno <> Label7.Text Then
                        Label7.Text = sameno
                        flag = True
                    End If
                Case 8
                    If sameno <> Label8.Text Then
                        Label8.Text = sameno
                        flag = True
                    End If
                Case 9
                    If sameno <> Label9.Text Then
                        Label9.Text = sameno
                        flag = True
                    End If
            End Select
        End While
    End Sub

    ' 判定処理
    Private judge1 As String = ""
    Private Sub judge(ByVal msg As String)
        If judge1 = "" Then           ' すでにクリックされているラベルの有無をチェック
            judge1 = msg              ' なければ、クリックされたラベルの数字を格納
        Else                              ' すでにクリックされているラベルがあれば判定を行う
            If msg = judge1 Then ' 正解の場合
                pictureBox2.Visible = True
                pictureBox1.Visible = False
                maru += 1

            Else ' 不正解の場合
                pictureBox1.Visible = True
                pictureBox2.Visible = False
                batu += 1
            End If
            judge1 = ""
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        initial_setup()
        same_no()
        cnt += 1
        If cnt = 10 Then
            Timer1.Enabled = False
            PictureBox3.Visible = True
        End If
        label10.Text = "正解：" & maru & "  間違い：" & batu & "   未解答：" & (10 - maru - batu)
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Label1.BorderStyle = BorderStyle.Fixed3D
        judge(Label1.Text)
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
        Label2.BorderStyle = BorderStyle.Fixed3D
        judge(Label2.Text)
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
        Label3.BorderStyle = BorderStyle.Fixed3D
        judge(Label3.Text)
    End Sub

    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
        Label4.BorderStyle = BorderStyle.Fixed3D
        judge(Label4.Text)
    End Sub

    Private Sub Label5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label5.Click
        Label5.BorderStyle = BorderStyle.Fixed3D
        judge(Label5.Text)
    End Sub

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
        Label6.BorderStyle = BorderStyle.Fixed3D
        judge(Label6.Text)
    End Sub

    Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click
        Label7.BorderStyle = BorderStyle.Fixed3D
        judge(Label7.Text)
    End Sub

    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click
        Label8.BorderStyle = BorderStyle.Fixed3D
        judge(Label8.Text)
    End Sub

    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
        Label9.BorderStyle = BorderStyle.Fixed3D
        judge(Label9.Text)
    End Sub
End Class
