利用VB解决华容道问题的源代码

本文介绍了一种使用Visual Basic(VB)编程语言解决华容道问题的方法,通过详细的源代码阐述了解题思路和过程,包括关键函数的实现和全局变量的定义。
摘要由CSDN通过智能技术生成

全局变量定义


       
       
        
        Type HRDState            '华容道的棋局表示
       
       
state(1 To 12) As Long   '棋盘上的12个棋子的当前位置
Superid As Long        '上一步棋盘的位置编号,0代表无上一步
Level  As Long         '这一不棋局的级别,0代表是开始状态
End Type
Public G_Next As CHRDNext
Public G_Save As CHRDSave
Public G_State As HRDState



应用程序启动


        
        
         
         Sub Main()
        
        
frmHRDMAIN.Show     '显示主窗口
End Sub
<B>CHRDNext封装计算下一步算法的类</b>
Dim bs(1 To 12) As Long '棋子的开始状态,接收输入值
Dim ES(1 To 12) As Long '棋子的计算结束状态,生成输出值,中间变量
Dim hnum As Long        '横放的将军的数量,输入值
Public iEndNum As Long  '计算结束的下一步的数量,输出值
Dim SaveEnd(1 To 240) As Long '最后生成的存放结果数组,输出值
Public Function getid(id As Long) As Long
getid = SaveEnd(id)
End Function
Public Sub GetNext(BEGINSTATE() As Long, BEGINHNUM As Long)
Dim i As Long
Dim MoveType As Long   '移动方向
Dim iend As Long       '记录移动结果
For i = 1 To 12
 bs(i) = BEGINSTATE(i) '初始状态
Next i
hnum = BEGINHNUM          '横放的将军数量
iEndNum = 0               '初始化结果数量为0
If MoveCaoCao() = 0 Then AddEnd
For i = 2 To hnum + 1      '移动横放的将军
    For MoveType = 1 To 4
        If MoveHtiger(MoveType, i) = 0 Then AddEnd
    Next MoveType
Next i
For i = hnum + 2 To 6       '移动竖放的将军
    For MoveType = 1 To 4
       If MoveVtiger(MoveType, i) = 0 Then AddEnd
    Next MoveType
Next i
For i = 7 To 10             '移动小卒
    For MoveType = 1 To 4
        If MoveFighter(MoveType, i) = 0 Then AddEnd
    Next MoveType
Next i
End Sub
Private Sub AddEnd()
'将End数组中的数据添加到SaveEnd中去,最后将iendnum的值加1
Dim i As Long
    For i = 1 To 12
       SaveEnd(iEndNum * 12 + i) = ES(i)
    Next i
    iEndNum = iEndNum + 1
End Sub
Private Sub SortEnd(BeginId As Long, EndId As Long)
'将输出结果进行排序,保证小者在前,大者在后
Dim i As Long
Dim j As Long
Dim Swap As Long
i = BeginId
Do While i <= EndId - 1
    j = i + 1
    Do While j <= EndId
        If ES(i) > ES(j) Then
           Swap = ES(i): ES(i) = ES(j): ES(j) = Swap
        End If
        j = j + 1
    Loop
    i = i + 1
Loop
End Sub
Private Function MoveFighter(move_type As Long, id As Long)
As Long
'初始化下一步的数据
Dim i As Long
For i = 1 To 12
    ES(i) = bs(i)
Next i
MoveFighter = -1 '初始化返回值
Select Case move_type
    Case 1 'up</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值