猜数字游戏的VB程序代码
冬至打雷-吻别歌词
猜数字游戏的VB程序代码(改进版)
本猜数字游戏的方法及规则:
系统自
动生成一个四位数(四个数字没有重复),玩者需要猜
中这个数字。玩者在四个文本框内输入四个数字,
按“ENTER”后,
如果猜对,则文本框变绿,本轮结束,按“CLEAR”进入下一轮。如
果不对,则会在下面显示“xAxB”,其中“xA”表示有四个数字中有
多少个数字是数字和所在位置
都猜对的,“xB”表示有多少个数字
猜对但是所在位置不对。比如,答案是“1234”,玩者猜“5
432”,
则显示“1A2B”,
因为“3”猜对且位置也对,“4”猜对但位置不
对,
“2”猜对但位置不对。如果玩者猜“1324”,则显示“2A2B”;如果玩
者猜“4
567”
则显示“0A1B”;如果玩者猜“4256”,则显示“1A1B”,
原
因自己推。(四位数也可能是零开头。)最多猜错十次,如十次
没有猜对,则游戏失败,本轮结束,按“
CLEAR”进入下一轮。按
“QUIT”可查看答案,同时本轮结束。各控件在界面上的布局如下:
程序代码:
Option Explicit
Dim
key_num, count_enter As Integer
Dim key_dig(1
To 4) As Integer
Private Sub Command1_Click()
Dim i, j, count_A, count_B As Integer
Dim ans_dig(1 To 4) As Integer
count_enter =
count_enter + 1
ans_dig(1) = Val():
ans_dig(2) = Val():
ans_dig(3) = Val():
ans_dig(4) = Val()
For i = 1 To 4 Step 1
For j = 1 To 4 Step 1
If ans_dig(i) =
key_dig(j) Then
If i = j Then count_A =
count_A + 1 Else count_B = count_B
+ 1
'最主要的步骤
End If
Next
Next
If
count_A < 4 And count_enter < 10 Then
For i
= 1 To 25 Step 1
Print
Next
Print Str(count_enter) +
For i = 1 To 4 Step
1
Print Str(ans_dig(i));
Next
Print
Else
For i = 1 To 25 Step 1
Print
Next
If count_A = 4 Then
lor = vbGreen: lor = vbGreen:
lor = vbGreen:
lor = vbGreen
lor = vbBlack: lor = vbGreen
If key_num 1000 = 0 Then =
Else = Str(key_num)
ze = lor =
vbGreen: Print
d = True: d = False
count_enter = 0
d = False
Else
ze = 你输
了!
lor = vbRed
If key_num
1000 = 0 Then =
Else = Str(key_num)
d
= True: d = False
count_enter = 0
d
= False
End If
End If
End Sub
Private Sub Command2_Click()
Call
FormInitialization
d = False
d =
True: d = True
End Sub
Private Sub
Command3_Click()
Dim i As Integer
For i
= 1 To 25 Step 1
Print
Next
ze =
lor = vbRed:
lor = vbRed: = Str(key_num)
d = True
count_enter = 0
d = False
d = False
End Sub
Private Sub
Form_Load()
Call FormInitialization
End
Sub
Private Function FormInitialization()
Print
Cls
With Form1
.ForeColor = vbBlack: .Font = 黑体
End With
Dim n, i, j As Integer, is_appliable As Boolean
For n = 1 To 7 Step 1
Print
Next
Do
is_appliable = True
Randomize
key_num = Int(Rnd * (9876 - 123 + 1)) + 123
For i = 1 To 4 Step 1
key_dig(i) =
key_num 10 ^ (4 - i) Mod 10
Next
For
i = 1 To 3 Step 1
For j = i + 1 To 4 Step 1
If key_dig(i) = key_dig(j) Then
is_appliable = False
Next
Next
Loop Until is_appliable
With Command1
.Font = New ze = .Caption =
End With
With Command2
.Font =
Round
End With
With Command3
.Font =
End With
With Label1
.Font
= 宋体.FontSize = .Caption = 正确答案:
End With
With Label2
.Font = 宋体= =
“bingo”游戏
End With
With Label3
.Font = 宋体
.Caption = 系统自动生成一个四位数(四个数字没有重复),<
br>玩者需要猜中这个数字。玩者在四个文本框内输入四个数字,按
“ENTER”后
,如果猜对,则文本框变绿,本轮结束,按“CLEAR”进
入下一轮。如果不对,则会在下面显示“x
AxB”,其中“xA”表示有
四个数字中有多少个数字是数字和所在位置都猜对的,“xB”表示有多少个数字猜对但是所在位置不对。比如,答案是“1234”,玩
者猜“5432”,则显示“
1A2B”,
因为“3”猜对且位置也对,“4”猜对
但位置不对,“2”猜对但位
置不对。如果玩者猜“1324”,则显示
“2A2B”;如果玩者猜“4567”
则显示“0A1B”;如果玩者猜“4256”,
则显示“1A1B”,原因自己推。(四位数也可能是
零开头。)最多猜
错十次,如十次没有猜对,则游戏失败,本轮结束,按“CLEAR”进
入下
一轮。按“QUIT”可查看答案,同时本轮结束。各控件在界面
上的布局如下:
End
With
With Text1
.Font = 黑体
End
With
With Text2
.Font = 黑体
End
With
With Text3
.Font =
黑体
End With
With Text4
.Font = 黑体
End With
With Text5
.Font = 黑体.ForeColor =
vbWhite: .FontSize =
End With
End Function
vbBlack:
.BackColor =