サイコロプログラム例

プログラム例
'定数の宣言
Private Const Sx As Integer = 100          'サイコロの始点x座標
Private Const Sy As Integer = 100          'サイコロの始点y座標
Private Const Ex As Integer = 1300         'サイコロの終点x座標
Private Const Ey As Integer = 1300         'サイコロの終点y座標
Private Const SLong As Integer = 1200   'サイコロの長さ
Private Const Radius1 As Integer = 250  '円の半径(1のとき)
Private Const Radius6 As Integer = 130  '円の半径(6のとき)
Private Const Radius As Integer = 150   '円の半径(その他)

Private Const White = &HFFFFFF         '色の設定 白
Private Const Black = &H0&               '色の設定 黒
Private Const Red = &HFF&               '色の設定 赤

Private Sub Command1_Click()

    '変数の宣言
    Dim Num As Integer    'サイコロの目を入れる変数
    Dim box(5) As Integer '各サイコロの目を入れる配列変数
    Dim kaisuu As Integer '振る回数を入れる変数
  Dim i As Integer      '作業用フラグ
    
    'サイコロを振る回数をkaisuuに入れる
    kaisuu = Val(Text1.Text)
    
    'テキストボックスに何も入力してない時
    'エラーメッセージを出力
    If Text1.Text = "" Then
        MsgBox ("ちゃんと入力してください。")
    End If
    
    'テキストボックスの入力値が0回の時
    'このプロシージャを抜ける
    If kaisuu = 0 Then
        Exit Sub
    End If
    
    '繰り返す回数
    For i = 1 To kaisuu Step 1
	'乱数の初期化
      Randomize
        'サイコロの目は?
        Num = Int((6 * Rnd) + 1)  '1から6までの整数のどれかをNumにいれる
    
        Select Case Num
            Case 1
                box(0) = box(0) + 1
                Sikoro1           'サブプロシージャSikoro1へ
            Case 2
                box(1) = box(1) + 1
                Sikoro2           'サブプロシージャSikoro2へ
            Case 3
                box(2) = box(2) + 1
                Sikoro3           'サブプロシージャSikoro3へ
            Case 4
                box(3) = box(3) + 1
                Sikoro4           'サブプロシージャSikoro4へ
            Case 5
                box(4) = box(4) + 1
                Sikoro5           'サブプロシージャSikoro5へ
            Case 6
                box(5) = box(5) + 1
                Sikoro6           'サブプロシージャSikoro6へ
        End Select
    Next i
    
    '出た目の割合を表示
    With Form1
        .Label11 = Format(box(0) / kaisuu * 100, "0.0") & "%"
        .Label12 = Format(box(1) / kaisuu * 100, "0.0") & "%"
        .Label13 = Format(box(2) / kaisuu * 100, "0.0") & "%"
        .Label14 = Format(box(3) / kaisuu * 100, "0.0") & "%"
        .Label15 = Format(box(4) / kaisuu * 100, "0.0") & "%"
        .Label16 = Format(box(5) / kaisuu * 100, "0.0") & "%"
        
    '回数を表示
        .Label17 = Format(box(0) & "回")
        .Label18 = Format(box(1) & "回")
        .Label19 = Format(box(2) & "回")
        .Label20 = Format(box(3) & "回")
        .Label21 = Format(box(4) & "回")
        .Label22 = Format(box(5) & "回")
        
    End With
    
    '回数だけ求めるとき
    'Form1.Print "1の目が出た回数は" & box(0) & "回です。"
    'Form1.Print "2の目が出た回数は" & box(1) & "回です。"
    'Form1.Print "3の目が出た回数は" & box(2) & "回です。"
    'Form1.Print "4の目が出た回数は" & box(3) & "回です。"
    'Form1.Print "5の目が出た回数は" & box(4) & "回です。"
    'Form1.Print "6の目が出た回数は" & box(5) & "回です。"
    
End Sub

'終了
Private Sub Command2_Click()
    End
End Sub

Private Sub Sikoro1()

    'サイコロ1の描写
    Form1.Line (Sx, Sy)-(Ex, Ey), White, BF
    Form1.Line (Sx, Sy)-(Ex, Ey), Black, B
    FillColor = Red  ' FillColorを赤に設定します
    FillStyle = 0    ' FillStyleを0-塗りつぶしにします
    Form1.Circle (Sx + SLong * (1 / 2), Sy + SLong * (1 / 2)), Radius1, Red '円の描写
    FillStyle = 1    ' FillStyleを1-透明にします

End Sub

Private Sub Sikoro2()

    'サイコロ2の描写
    Form1.Line (Sx, Sy)-(Ex, Ey), White, BF
    Form1.Line (Sx, Sy)-(Ex, Ey), Black, B
    FillColor = Black  ' FillColorを赤に設定します
    FillStyle = 0    ' FillStyleを0-塗りつぶしにします
    Form1.Circle (Sx + SLong * (1 / 3), Sy + SLong * (1 / 3)), Radius, Black '円の描写
    Form1.Circle (Sx + SLong * (2 / 3), Sy + SLong * (2 / 3)), Radius, Black '円の描写
    FillStyle = 1    ' FillStyleを1-透明にします

End Sub

Private Sub Sikoro3()

    'サイコロ3の描写
    Form1.Line (Sx, Sy)-(Ex, Ey), White, BF
    Form1.Line (Sx, Sy)-(Ex, Ey), Black, B
    FillColor = Black  ' FillColorを赤に設定します
    FillStyle = 0    ' FillStyleを0-塗りつぶしにします
    Form1.Circle (Sx + SLong * (1 / 4), Sy + SLong * (1 / 4)), Radius, Black '円の描写
    Form1.Circle (Sx + SLong * (2 / 4), Sy + SLong * (2 / 4)), Radius, Black '円の描写
    Form1.Circle (Sx + SLong * (3 / 4), Sy + SLong * (3 / 4)), Radius, Black '円の描写
    FillStyle = 1    ' FillStyleを1-透明にします

End Sub

Private Sub Sikoro4()

    'サイコロ4の描写
    Form1.Line (Sx, Sy)-(Ex, Ey), White, BF
    Form1.Line (Sx, Sy)-(Ex, Ey), Black, B
    FillColor = Black  ' FillColorを赤に設定します
    FillStyle = 0    ' FillStyleを0-塗りつぶしにします
    For i = 1 To 3 Step 2
        For j = 1 To 3 Step 2
                Form1.Circle (Sx + SLong * (j / 4), Sy + SLong * (i / 4)), Radius, Black '円の描写(4つ)
        Next j
    Next i
    FillStyle = 1    ' FillStyleを1-透明にします

End Sub

Private Sub Sikoro5()

    'サイコロ5の描写
    Form1.Line (Sx, Sy)-(Ex, Ey), White, BF
    Form1.Line (Sx, Sy)-(Ex, Ey), Black, B
    FillColor = Black  ' FillColorを赤に設定します
    FillStyle = 0    ' FillStyleを0-塗りつぶしにします
    For i = 1 To 3 Step 2
        For j = 1 To 3 Step 2
                Form1.Circle (Sx + SLong * (j / 4), Sy + SLong * (i / 4)), Radius, Black '円の描写(4つ)
        Next j
    Next i
    Form1.Circle (Sx + SLong * (2 / 4), Sy + SLong * (2 / 4)), Radius, Black '円の描写
    FillStyle = 1    ' FillStyleを1-透明にします

End Sub

Private Sub Sikoro6()

    'サイコロ6の描写
    Form1.Line (Sx, Sy)-(Ex, Ey), White, BF
    Form1.Line (Sx, Sy)-(Ex, Ey), Black, B
    FillColor = Black  ' FillColorを赤に設定します
    FillStyle = 0    ' FillStyleを0-塗りつぶしにします
    For i = 1 To 3
        For j = 1 To 3 Step 2
                Form1.Circle (Sx + SLong * (j / 4), Sy + SLong * (i / 4)), Radius6, Black '円の描写(6つ)
        Next j
    Next i
    FillStyle = 1    ' FillStyleを1-透明にします

End Sub

'クリア
Private Sub Command3_Click()

    Text1.Text = ""
    Form1.Cls
    Text1.SetFocus
    
End Sub

'フォームロード時にどうするか
Private Sub Form_Load()

    Command1.Enabled = False

End Sub

'テキスト1Changeイベント
Private Sub Text1_Change()

    If Text1.Text = "" Then
        Command1.Enabled = False
    Else
        Command1.Enabled = True
    End If

End Sub

'アスキーコードの使用
'数字以外は入力できない
Private Sub Text1_KeyPress(KeyAscii As Integer)
    
     If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then
     Else
           KeyAscii = 0 'NULLということ!
     End If

End Sub