📄 猜数字游戏.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "猜数字"
ClientHeight = 3885
ClientLeft = 60
ClientTop = 450
ClientWidth = 5910
LinkTopic = "Form1"
ScaleHeight = 3885
ScaleWidth = 5910
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton 确定
Caption = "确定"
Enabled = 0 'False
Height = 495
Left = 4440
TabIndex = 6
Top = 2160
Width = 1215
End
Begin VB.CommandButton 开始
Caption = "开始"
Height = 495
Left = 4440
TabIndex = 5
Top = 1320
Width = 1215
End
Begin VB.ListBox list
Height = 2580
Left = 120
TabIndex = 4
Top = 1080
Width = 3975
End
Begin VB.TextBox Text4
Height = 495
Left = 3240
MaxLength = 1
TabIndex = 3
Top = 360
Width = 615
End
Begin VB.TextBox Text3
Height = 495
Left = 2160
MaxLength = 1
TabIndex = 2
Top = 360
Width = 615
End
Begin VB.TextBox Text2
Height = 495
Left = 1200
MaxLength = 1
TabIndex = 1
Top = 360
Width = 615
End
Begin VB.TextBox Text1
Height = 495
Left = 240
MaxLength = 1
TabIndex = 0
Top = 360
Width = 615
End
Begin VB.Label lbl
Height = 375
Left = 4200
TabIndex = 7
Top = 480
Width = 1575
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim r1%, r2%, r3%, r4% '随机变量
Dim con1%, con2%, con3%, con4% '用户输入数据
Dim n% '只能猜10次的标识符
Dim n1% '位置不对数字对
Dim n2% '数字位置都对
Private Sub 开始_Click()
n = 0
lbl.Caption = "请输数字"
list.Clear
开始.Enabled = False
确定.Enabled = True
Text1.Locked = 0
Text2.Locked = 0
Text3.Locked = 0
Text4.Locked = 0
Call Randomize
'产生0-9的随机数
r1 = Int(10 * Rnd())
r2 = Int(10 * Rnd())
r3 = Int(10 * Rnd())
r4 = Int(10 * Rnd())
'使四个随机数不相等
Do Until r1 <> r2 And r1 <> r3 And r1 <> r4
r1 = Int(10 * Rnd())
Loop
Do Until r2 <> r1 And r2 <> r3 And r2 <> r4
r2 = Int(10 * Rnd())
Loop
Do Until r3 <> r2 And r3 <> r1 And r3 <> r4
r3 = Int(10 * Rnd())
Loop
Do Until r4 <> r2 And r4 <> r3 And r4 <> r1
r4 = Int(10 * Rnd())
Loop
End Sub
Private Sub 确定_Click()
Dim k As Integer '四个方格不能有相同的数字
n1 = 0
n2 = 0
k = 0
If n >= 10 Then '如果超过10次还没有猜对
Call nowin
Else
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Then
list.AddItem "请输入数字"
Else
con1 = Text1.Text
con2 = Text2.Text
con3 = Text3.Text
con4 = Text4.Text
'四个方格不能有相同的数字
If con1 = con2 Or con1 = con3 Or con1 = con4 Then
k = k + 1
End If
If con2 = con3 Or con2 = con4 Then
k = k + 1
End If
If con3 = con4 Then
k = k + 1
End If
If k <> 0 Then
list.AddItem "四个方格不能有相同的数字"
Else
n = n + 1
lbl.Caption = "你还可以猜" & 10 - n & "次"
Call contr
End If
End If
End If
End Sub
Private Sub Form_Load()
'开始不能交互
Text1.Locked = 1
Text2.Locked = 1
Text3.Locked = 1
Text4.Locked = 1
list.AddItem Space$(16) & "猜数字"
list.AddItem " 每次完成猜数后,会显示本次结果"
list.AddItem " 根据提示,在猜测!"
list.AddItem " 最多只能猜10次,是4个不同的数!"
End Sub
'只允许数字
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim Numbers As String
Numbers = "1234567890" + Chr(8) + Chr(46)
If InStr(Numbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
Dim Numbers As String
Numbers = "1234567890" + Chr(8) + Chr(46)
If InStr(Numbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
Dim Numbers As String
Numbers = "1234567890" + Chr(8) + Chr(46)
If InStr(Numbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer)
Dim Numbers As String
Numbers = "1234567890" + Chr(8) + Chr(46)
If InStr(Numbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub
Private Sub nowin() '输了
开始.Enabled = 1 '非0数为真
确定.Enabled = 0
list.Clear
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.Locked = 1
Text2.Locked = 1
Text3.Locked = 1
Text4.Locked = 1
list.AddItem "你以超过10次你输了"
list.AddItem "答案是:" & r1 & r2 & r3 & r4
lbl.Caption = ""
End Sub
Private Sub win() '赢了
开始.Enabled = 1
确定.Enabled = 0
list.Clear
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.Locked = 1
Text2.Locked = 1
Text3.Locked = 1
Text4.Locked = 1
list.AddItem "你赢了"
lbl.Caption = ""
End Sub
Private Sub contr() '是否猜对
Dim x1%, x2%, x3%, x4% '位置不对数字对
If con1 = r1 And con2 = r2 And con3 = r3 And con4 = r4 Then
Call win
Else
If con1 = r1 Or con1 = r2 Or con1 = r3 Or con1 = r4 Then
x1 = x1 + 1
If con1 = r1 Then
n2 = n2 + 1
x1 = 0
End If
End If
If con2 = r1 Or con2 = r2 Or con2 = r3 Or con2 = r4 Then
x2 = x2 + 1
If con2 = r2 Then
n2 = n2 + 1
x2 = 0
End If
End If
If con3 = r1 Or con3 = r2 Or con3 = r3 Or con3 = r4 Then
x3 = x3 + 1
If con3 = r3 Then
n2 = n2 + 1
x3 = 0
End If
End If
If con4 = r1 Or con4 = r2 Or con4 = r3 Or con4 = r4 Then
x4 = x4 + 1
If con4 = r4 Then
n2 = n2 + 1
x4 = 0
End If
End If
n1 = x1 + x2 + x3 + x4 '位置不对数字对有多少个
Call pr
End If
End Sub
Private Sub pr()
Dim s As String
s = con1 & con2 & con3 & con4
list.AddItem s & Space$(5) & _
"位置不对数字有" & n1 & Space$(2) & _
"位置数字都对" & n2
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -