|
楼主 |
发表于 2014-11-29 01:24
|
显示全部楼层
全部代码如下,一览无余,查看行数:
模块1:- Option Explicit
- Dim zb1&(), zb2&(), pd%, bz(), js&, jps%(1 To 3) '坐标、步骤、金片数
- Const hjj& = 10, ljj& = 50, jcx& = 60, jcy& = 150 '行间距、列间距、基础x值、基础y值
- Const w& = 30, h& = 10, dz& = 24 '金片宽、金片高、金片宽递增
- Public Sub YuBei(n%) '预备
- Dim i&, j&, shp As Shape, x&, y&
- ReDim zb1&(1 To n, 1 To 4), zb2&(1 To 3, 1 To 4)
- With aoe
- For Each shp In .Shapes
- If shp.Type <> msoOLEControlObject And shp.Type <> msoFormControl Then shp.Delete
- Next shp
- x = jcx: y = jcy + (h + dz / 2) * n
- Randomize
- For i = n To 1 Step -1
- j = n - i + 1
- zb1(j, 1) = x + (i - 1) * dz / 2: zb1(j, 2) = y - (i - 1) * (h + hjj)
- zb1(j, 3) = w + (n - i) * dz: zb1(j, 4) = h
- .Shapes.AddShape(msoShapeRectangle, zb1(j, 1), zb1(j, 2), zb1(j, 3), zb1(j, 4)).Name = "金片" & j
- .Shapes("金片" & j).Select
- With Selection.ShapeRange
- .Line.Weight = 1
- .Fill.ForeColor.RGB = RGB(Int(Rnd() * 256), Int(Rnd() * 256), Int(Rnd() * 256))
- End With
- Next i
- For i = 1 To 3
- zb2(i, 1) = zb1(n, 1) + zb1(n, 3) / 2 + (zb1(n, 3) + ljj) * (i - 1): zb2(i, 3) = zb2(i, 1)
- zb2(i, 2) = zb1(1, 2) - 60: zb2(i, 4) = zb1(n, 2) + h * 2
- '.Shapes.AddConnector(msoConnectorStraight, zb2(i, 1), zb2(i, 2), zb2(i, 3), zb2(i, 4)).Name = Chr(64 + i)'2010录制
- .Shapes.AddLine(zb2(i, 1), zb2(i, 2), zb2(i, 3), zb2(i, 4)).Name = Chr(64 + i) '2003录制
- .Shapes(Chr(64 + i)).Select
- With Selection.ShapeRange
- .Line.Weight = 4
- '.Line.ForeColor.ObjectThemeColor = msoThemeColorText1'2010录制
- .Line.ForeColor.SchemeColor = 64 '2003录制
- .ZOrder msoSendToBack
- End With
- Next i
- .Range("b2").Select
- End With
- pd = 1
- End Sub
- Sub Hanoi1() '汉诺塔
- Dim n%
- If pd = 1 Then
- n = aoe.Range("b1").Value: js = 0
- jps(1) = n: jps(2) = 0: jps(3) = 0
- ReDim bz(1 To 2 ^ n - 1)
- Call dg_Hanoi1(n, "A", "B", "C")
- Else
- MsgBox "请选择阶数,初始化汉诺塔游戏。", , "友情提示"
- End If
- pd = 0
- aoe.Range("b3").Select
- End Sub
- Sub dg_Hanoi1(n%, a, b, c) '递归过程(阶数、起始针、过渡针、目标针)
- If n = 1 Then
- js = js + 1
- bz(js) = "金片" & n & ":" & a & "→" & c
- aoe.Cells(js + 1, "p").Value = "第" & js & "步": aoe.Cells(js + 1, "q").Value = bz(js)
- Call DongHua("金片" & n, n, Asc(a) - 64, Asc(c) - 64)
- jps(Asc(a) - 64) = jps(Asc(a) - 64) - 1: jps(Asc(c) - 64) = jps(Asc(c) - 64) + 1
- Else
- Call dg_Hanoi1(n - 1, a, c, b)
- js = js + 1
- bz(js) = "金片" & n & ":" & a & "→" & c
- aoe.Cells(js + 1, "p").Value = "第" & js & "步": aoe.Cells(js + 1, "q").Value = bz(js)
- Call DongHua("金片" & n, n, Asc(a) - 64, Asc(c) - 64)
- jps(Asc(a) - 64) = jps(Asc(a) - 64) - 1: jps(Asc(c) - 64) = jps(Asc(c) - 64) + 1
- Call dg_Hanoi1(n - 1, b, a, c)
- End If
- End Sub
- Public Sub DongHua(jp$, n1%, m1%, m2%) '动画(金片、金片序号、开始针、到达针)
- Dim i&, zy%, tj2& '左右、调节
- tj2 = aoe.Range("b3").Value
- aoe.Shapes(jp).Select
- With Selection.ShapeRange
- For i = .Top To zb2(1, 2) - 30 Step -1 '金片上移
- .IncrementTop -1
- If i Mod tj2 = 0 Then Call YanShi
- Next i
- If zb2(m1, 1) < zb2(m2, 1) Then zy = 1 Else zy = -1
- For i = WorksheetFunction.Min(zb2(m1, 1), zb2(m2, 1)) To WorksheetFunction.Max(zb2(m1, 1), zb2(m2, 1)) - 1
- .IncrementLeft zy '金片左右平移(须注意植树问题在此的应用,否则效果会偏差,此问题极隐蔽,极不易测试察觉)
- If i Mod tj2 = 0 Then Call YanShi
- Next i
- For i = zb2(1, 2) - 30 To zb2(1, 4) - (hjj + h) * (jps(m2) + 1) '金片下移
- .IncrementTop 1
- If i Mod tj2 = 0 Then Call YanShi
- Next i
- End With
- End Sub
- Public Sub YanShi() '延时
- Dim j&, tj1&
- tj1 = aoe.Range("b2").Value
- For j = 1 To tj1
- DoEvents
- Next j
- End Sub
复制代码 工作表aoe:- Option Explicit
- Private Sub Worksheet_Change(ByVal Target As Range)
- If Target.Address(0, 0) = "B1" Then Range("p2:q" & Rows.Count).ClearContents: Call YuBei(Target.Value)
- End Sub
复制代码 |
|