当前位置:文档之家› 猜数字游戏的VB代码

猜数字游戏的VB代码

猜数字游戏的VB代码本猜数字游戏的方法及规则:系统自动生成一个四位数(四个数字没有重复),玩者需要猜中这个数字。

玩者在四个文本框内输入四个数字,按“ENTER”后,如果猜对,则文本框变绿,本轮结束,按“CLEAR”进入下一轮。

如果不对,则会在下面显示“xAxB”,其中“xA”表示有四个数字中有多少个数字是数字和所在位置都猜对的,“xB”表示有多少个数字猜对但是所在位置不对。

比如,答案是“1234”,玩者猜“5432”,则显示“1A2B”,因为“3”猜对且位置也对,“4”猜对但位置不对,“2”猜对但位置不对。

如果玩者猜“1324”,则显示“2A2B”;如果玩者猜“4567”,则显示“0A1B”;如果玩者猜“4256”,则显示“1A1B”,原因自己推。

(四位数也可能是零开头。

)最多猜错十次,如十次没有猜对,则游戏失败,本轮结束,按“CLEAR”进入下一轮。

按“QUIT”可查看答案,同时本轮结束。

各控件在界面上的布局如下:Option ExplicitPrivate Sub Command1_Click() '此为“确认”按钮。

If Form1.ForeColor <> vbMagenta ThenIf Command2.Enabled = False ThenDim keydig(1 To 4) As Integer, ansdig(1 To 4) As IntegerDim n, m, cnt_A, cnt_B As Integer, isappliable As BooleanStatic count, keynum As Integer 'command按钮事件执行终,必须使变量count仍然残留。

DoIf count = 0 Then '“count”变量计算Command1_Click执行的次数,每执行一次增加1。

Randomizekeynum = Int(Rnd * (9876 - 123 + 1)) + 123 '生成一个四位随机整数。

End If '3rdFor n = 1 To 4 Step 1keydig(n) = keynum \ 10 ^ (4 - n) Mod 10 '“keydig(n)”数组有四个元素,存放四位随机数的四个数字。

Nextisappliable = TrueFor m = 1 To 3 Step 1 '生成的四位整数不能有任何两位相同,否则重新生成。

For n = m + 1 T o 4 Step 1If keydig(m) = keydig(n) Then isappliable = FalseNextNextLoop Until isappliable = True '如果有相同数字就返回去重算。

ansdig(1) = Val(Text1.Text): ansdig(2) = Val(Text2.Text): ansdig(3) = Val(Text3.Text): ansdig(4) = Val(Text4.Text)For m = 1 To 4 Step 1For n = 1 To 4 Step 1If keydig(m) = ansdig(n) ThenIf m = n Then cnt_A = cnt_A + 1 Else cnt_B = cnt_B + 1End IfNextNextIf cnt_A = 4 Or count = 9 ThenText5.Text = Str(keynum): Text5.FontSize = "15"Command2.Enabled = True: Command1.Enabled = FalseIf cnt_A = 4 Then '5thText1.BackColor = vbGreen: Text2.BackColor = vbGreen: Text3.BackColor = vbGreen: Text4.BackColor = vbGreen: Text5.BackColor = vbGreen: Label2.Caption = "BINGO!": Label2.ForeColor = vbBlue: Text5.ForeColor = vbBlackElseWith Form1.ForeColor = vbRed: .Font = "Times New Roman": .FontSize = "20"End WithText5.ForeColor = vbRed: Text1.Text = "": Text2.Text = "": Text3.Text = "": T ext4.Text = ""Print Tab(65); "YOU LOSE!"End If '5thElseForm1.FontSize = "13": Text1.Text = "": Text2.Text = "": Text3.Text = "": Text4.Text = ""Print Tab(41); Str(count + 1)Print Tab(47); Str(ansdig(1)) + Str(ansdig(2)) + Str(ansdig(3)) + Str(ansdig(4)) + " " + Str(cnt_A) + "A" + Str(cnt_B) + "B"End If '4thcount = count + 1Elsecount = 0End If '2ndElseText5.Text = Str(keynum)End If '1stEnd SubPrivate Sub Command2_Click()Dim n As IntegerIf Form1.ForeColor = vbRed Or Label2.Caption = "BINGO!" Or Form1.ForeColor = vbMagenta ThenClsCall FormInitializationFor n = 1 T o 15 Step 1Print ""NextCall Command1_ClickElseCommand1.Enabled = True: Command3.Enabled = True: Command3.Caption = "QUIT": Command2.Enabled = FalseEnd IfEnd SubPrivate Sub Command3_Click()If Form1.ForeColor <> vbMagenta ThenCommand2.Enabled = True: Command1.Enabled = FalseForm1.ForeColor = vbMagenta: Form1.Font = "Times New Roman": Form1.FontSize = "40"Command3.Caption = "See the key"Print Tab(40); "QUIT"Text1.Text = "": Text2.Text = "": Text3.Text = "": Text4.Text = ""ElseCall Command1_ClickText5.ForeColor = vbRed: Text5.FontSize = "15"Command3.Enabled = FalseEnd IfEnd SubPrivate Sub Form_Load()Dim n As IntegerCall FormInitializationFor n = 1 T o 15 Step 1Print ""NextCommand2.Enabled = FalseEnd SubPrivate Function FormInitialization() As IntegerWith Form1.ForeColor = vbBlack: .Font = "宋体": .FontSize = "9"End WithWith Command1.Font = "Times New Roman": .FontSize = "15": .Caption = "ENTER" End WithWith Command2.Caption = "CLEAR": .Font = "Times New Roman": .FontSize = "12" End WithWith Label2.Font = "Times New Roman": .FontSize = "20": .Caption = "'Bingo' Game": .ForeColor = vbBlackEnd WithWith Text1.Text = "": .FontSize = "35": .BackColor = vbWhiteEnd WithWith Text2.Text = "": .FontSize = "35": .BackColor = vbWhiteEnd WithWith Text3.Text = "": .FontSize = "35": .BackColor = vbWhiteEnd WithWith Text4.Text = "": .FontSize = "35": .BackColor = vbWhiteEnd WithWith Text5.ForeColor = vbBlack: .Text = "": .BackColor = vbWhiteEnd WithLabel1.Caption = "正确答案:"Command3.Caption = "QUIT": Label3.Caption = "系统生成一个四位随机整数(四个数字没有重复),玩者要想方设法猜中这个四位数。

相关主题