当前位置:编程学习 > VB >>

VB汉诺塔编制

想做一个汉诺德的游戏试试,给个源码学习一下
答案:给一个可以自动游戏的,呵呵

'//////////////////////pan是一个shape控件,用来表示盘子,指针是一个picture控件,用来在理面画一个指针,其他都基本上是默认控件
'延时的api
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim pn As Integer, lai As Integer, qu As Integer

Dim n As Integer
Dim Gp(1 To 3) As Integer
Dim Zw As Integer
Dim huo As Integer
Dim t As Integer, bu As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyLeft Then Zuo
    If KeyCode = vbKeyRight Then You
    If KeyCode = vbKeyUp Then Shang
    If KeyCode = vbKeyDown Then Xia
    If KeyCode = vbKeyZ Then
        If MsgBox("您选择了自动演示,由于本人水平有限,在自动演示时," & Chr(10) & "如果您进行其他操作,本程序将没有响应。" & Chr(10) & "是否继续?", _
        vbYesNo, "路过de流星提示您") = vbYes Then zz n
       
    End If

End Sub

 

Private Sub Form_Load()
    Do While (n < 1 Or n > 10)
        n = Int(Val(InputBox("请输入圆盘的个数!(1-10个)", "路过de流星提示您")))
    Loop
   
    Timer1.Enabled = True
   
    '窗口的大小
    Form1.Height = (6 + n) * 400
    Form1.Width = (200 + n * 300) * 3 + 200
    If Form1.Width < 5000 Then Form1.Width = 5000
   
    Form1.KeyPreview = True
   
   '三根柱子的位置
    Line1(1).X1 = (Form1.Width - 200) / 6 + 100
    Line1(1).X2 = (Form1.Width - 200) / 6 + 100
    Line1(2).X1 = 3 * (Form1.Width - 200) / 6 + 100
    Line1(2).X2 = 3 * (Form1.Width - 200) / 6 + 100
    Line1(3).X1 = 5 * (Form1.Width - 200) / 6 + 100
    Line1(3).X2 = 5 * (Form1.Width - 200) / 6 + 100
   
    Line1(1).Y1 = Form1.Height
    Line1(2).Y1 = Form1.Height
    Line1(3).Y1 = Form1.Height
   
    '遮罩的位置
    Shape1.Width = Form1.Width
    Shape1.Left = -10
    Shape1.Top = Form1.Height - 900
   
   
    '载入盘子
    For i = 1 To n
        Load pan(i)
        pan(i).Visible = True
        pan(i).Width = 200 + i * 300
        pan(i).Left = Line1(1).X1 - pan(i).Width / 2
        pan(i).Top = Form1.Height - 900 - (n - i + 1) * 400
    Next i
   
    Gp(1) = n
   
    '放置指针
    Zw = 1
    Zhiz Zw
   
   
    '说明标签的位置
    shijian.Left = Line1(1).X1 - shijian.Width / 2
    shijian.Top = Form1.Height - 850
    bushu.Left = Line1(2).X1 - bushu.Width / 2
    bushu.Top = Form1.Height - 850
    mingzi.Left = Line1(3).X1 - mingzi.Width / 2
    mingzi.Top = Form1.Height - 800
   
End Sub

'指针位置
Sub Zhiz(n As Integer)
    zhizhen.Left = Line1(n).X1 - 250
End Sub

'每根柱子上的盘字数
Sub Jishu()
    Gp(1) = 0: Gp(2) = 0: Gp(3) = 0
    For i = 1 To n
        If (pan(i).Left + pan(i).Width / 2) = Line1(1).X1 Then Gp(1) = Gp(1) + 1
        If (pan(i).Left + pan(i).Width / 2) = Line1(2).X1 Then Gp(2) = Gp(2) + 1
        If (pan(i).Left + pan(i).Width / 2) = Line1(3).X1 Then Gp(3) = Gp(3) + 1
    Next i
End Sub

'按左键
Sub Zuo()
    Zw = Zw - 1
    If Zw = 0 Then Zw = 3
    Zhiz Zw
    If huo Then
        pan(huo).Left = Line1(Zw).X1 - pan(huo).Width / 2
    End If
    Jishu
   
    bu = bu + 1
    bushu.Caption = "移动" & bu & "步"
   
End Sub


'按右键

Sub You()
    Zw = Zw + 1
    If Zw = 4 Then Zw = 1
    Zhiz Zw
    If huo Then
        pan(huo).Left = Line1(Zw).X1 - pan(huo).Width / 2
    End If
    Jishu

    bu = bu + 1
    bushu.Caption = "移动" & bu & "步"


End Sub

'按上键
Sub Shang()
    For i = 1 To n
        If (pan(i).Left + pan(i).Width / 2) = Line1(Zw).X1 Then
            huo = i
            pan(i).Top = 850
            Exit Sub
        End If
    Next i
   

   
End Sub

'按下键
Sub Xia()
    If huo Then
        If huo > 1 Then
            For i = huo - 1 To 1 Step -1
                If (pan(i).Left + pan(i).Width / 2) = pan(huo).Left + pan(huo).Width / 2 Then Exit Sub
            Next i
        End If
        pan(huo).Top = Form1.Height - 900 - Gp(Zw) * 400
        huo = 0
    End If
    Sheng


End Sub

 

'胜利的条件
Sub Sheng()
    Dim p As Integer
   
    If Zw = 3 Then
        p = 1
        For i = 1 To n
            If (pan(i).Left + pan(i).Width / 2) <> Line1(3).X1 Then p = 0
        Next i
    End If
    If p Then
        MsgBox "恭喜你,赢得胜利!", , "路过de流星提示您"
        Form1.KeyPreview = False
        Timer1.Enabled = False
    End If
End Sub


'自动演示
Sub Zidong(ge As Integer, a As Integer, b As Integer, c As Integer)
    Form1.KeyPreview = False
    If ge = 1 Then
        Yi a, c
    Else
        Zidong (n - 1), a, c, b
        Yi a, c
        Zidong (n - 1), b, a, c
    End If
End Sub

'移动
Sub Yi(x As Integer, y As Integer

上一个:英语不好能学好vb吗
下一个:计算机VB编程

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,