|
代码如下,加入了一些功能,自我感觉代码应该比楼主的精简一些:
工作表代码:- Private Sub Worksheet_Change(ByVal Target As Range)
- If Target.Address(0, 0) = "D1" Then
- If IsNumeric(Target.Value) And Len(Trim(Target.Value)) = 4 Then
- BiDui Target.Value
- Else
- MsgBox "请输入四位不重复数字!", , "友情提示"
- Application.EnableEvents = False
- Target.Value = ""
- Application.EnableEvents = True
- End If
- End If
- End Sub
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Range("d1").Select
- End Sub
复制代码 模块代码:- Option Explicit
- Public sjs$, n%, kj As Boolean
- Public Sub RndShu() '随机数
- Dim arr$(0 To 9), i%, r%, gd$
- n = 0: Range("d1,b3:d12") = "": kj = False '次数归零、区域清空、重新开局
- For i = 0 To 9
- arr(i) = i
- Next i
- Randomize: sjs = ""
- For i = 0 To 9 '【香川经典数组洗牌法】
- r = Int(Rnd() * (9 - i)) + i
- gd = arr(i): arr(i) = arr(r): arr(r) = gd
- Next i
- For i = 0 To 3
- sjs = sjs & arr(i)
- Next i
- MsgBox "随机数已生成,可以开始。", , "友情提示"
- End Sub
- Public Sub BiDui(srs$) '比对(输入数)
- If sjs = "" Or kj Then MsgBox "请点击开始按钮获取随机数。", , "友情提示": Exit Sub
- Dim i%, j%, brr$(1 To 4), crr$(1 To 4), a%, b%
- For i = 1 To 4
- brr(i) = Mid(srs, i, 1): crr(i) = Mid(sjs, i, 1)
- Next i
- For i = 1 To 4
- For j = 1 To 4
- If i = j And brr(i) = crr(j) Then
- a = a + 1: Exit For
- ElseIf brr(i) = crr(j) Then
- b = b + 1: Exit For
- End If
- Next j
- Next i
- n = n + 1
- Range("b" & n + 2).Value = n
- Range("c" & n + 2).Value = srs
- Range("d" & n + 2).Value = a & "A" & b & "B"
- If a = 4 Then BangDan n, sjs Else If n = 10 Then MsgBox "失败!不要气馁,点击开始,再来一次!", , "友情提示": kj = True
- End Sub
- Public Sub BangDan(cs%, sz$) '榜单(次数、数字)
- Dim drr, wj$, i%, j%, h%, l%, gd, m%
- drr = Range("o2:q13").Value
- kj = True
- wj = Application.InputBox("胜出,您真厉害!" & Chr(10) & "请输入您的大名(最长10个字符),看看能否刷新榜单!" & Chr(10), "输入姓名", _
- Choose(Int(Rnd() * 4 + 1), "英雄", "无敌", "独孤", "智圣"), , , , , 2)
- m = WorksheetFunction.Max(10 - Len(wj), 0)
- wj = Mid(wj & WorksheetFunction.Rept(" ", m), 1, 10)
- drr(UBound(drr), 1) = wj: drr(UBound(drr), 2) = cs: drr(UBound(drr), 3) = sz '记录在最末尾
- h = UBound(drr): l = UBound(drr, 2)
- For i = 2 To h '特殊处理榜单中的空值,使其排序后不在最前面
- If drr(i, 2) = "" Then drr(i, 2) = 11
- Next i
- For i = 2 To h - 1 '排序
- For j = i + 1 To h
- If drr(i, 2) > drr(j, 2) Then
- gd = drr(i, 1): drr(i, 1) = drr(j, 1): drr(j, 1) = gd
- gd = drr(i, 2): drr(i, 2) = drr(j, 2): drr(j, 2) = gd
- gd = drr(i, 3): drr(i, 3) = drr(j, 3): drr(j, 3) = gd
- End If
- Next j
- Next i
- For i = 2 To h
- If drr(i, 2) = 11 Then drr(i, 2) = ""
- Next i
- Range("o2").Resize(h - 1, l).Value = drr
- End Sub
- Public Sub ShuoMing() '说明
- MsgBox "1.点击开始按钮获取随机数,通过最多10次的输入测试,推测该随机数,推测准确的获胜并进入榜单;" & Chr(10) & Chr(10) _
- & "2.在D1单元格输入0-9四位不重复的数字,敲击回车开始验证,在D3:D12区域记录验证结果为形如:xAyB;" & Chr(10) & Chr(10) _
- & "3.xA表示输入数字与随机数的数位相同且数字相同的有x个,yB表示数位不同但数字相同的有y个。" & Chr(10) & Chr(10) _
- & "4.系统每次随机产生一个不重复随机四位数,千位也可出现0,在每局游戏中,该随机数保持不变。", , "游戏说明"
- End Sub
- Public Sub ChaKan() '查看
- Dim err, i%, j%, xx$, yy$
- err = Range("o2:q12").Value
- For i = 1 To UBound(err)
- For j = 1 To UBound(err, 2)
- xx = xx & " " & err(i, j)
- Next j
- yy = yy & Mid(xx, 2) & Chr(10): xx = ""
- Next i
- MsgBox yy, , "榜单"
- End Sub
复制代码 |
|