在单线棋形判断函数基础之上的玩家胜负判断函数:
Public Function JudgeWinner(ByRef Piece() As Integer, ByVal XInt%, ByVal YInt%, ByVal Flag As Byte) As Boolean '棋局胜负判断函数
X = XInt: Y = YInt
If JudgeNum(Piece, 1, 0, Flag) >= 5 Or JudgeNum(Piece, 1, 1, Flag) >= 5 Or JudgeNum(Piece, 0, 1, Flag) >= 5 Or JudgeNum(Piece, -1, 1, Flag) >= 5 Then _
JudgeWinner = True: Exit Function '判断标记为Flag的玩家是否已胜利
JudgeWinner = False
End Function
本程序为了实现动态判断是否有棋可悔,悔棋存储数组逻辑实质为堆栈结构;VB中未提供现成的Stack类,需要以下函数来完成对数组栈顶的搜索工作。
Public Function SearchNode(ByRef PieceRec() As Integer) As Integer
'寻找棋步记录数组中的最大定义值
Dim i%
For i = 225 To 1 Step -1
If PieceRec(i) <> 0 Then Exit For
Next i
SearchNode = i
End Function
Public Function GetNode(ByRef PieceTar() As Integer, ByRef XTar%, ByRef YTar%, ByVal Xt%, ByVal Yt%, ByVal XInt%, ByVal YInt%, ByVal Flag As Byte)
'本函数用来返回一条直线上的结点,承载变量为XTar,YTar;XInt和YInt为方向判断变量;注:本函数的待测坐标若为非空值则返回待测点坐标
Dim i%, j%
i = Xt: j = Yt
Do
i = i + XInt: j = j + YInt
If PieceTar(i, j) <> Flag Then Exit Do
Loop Until i < 1 Or i > 15 Or j < 1 Or j > 15
XTar = i - XInt: YTar = j - YInt
End Function
为了判断玩家的获胜情况,需要一个执行效率较高的函数来判断单线棋形最大棋子数。以下函数实现了上述功能。第二和第三参数两两决定了四种棋盘方向。
Public Function JudgeNum(ByRef Piece() As Integer, ByVal XFluctuate%, ByVal YFluctuate%, ByVal Flag As Byte) As Integer
'判断最多有多少棋子共线(连续),这是执行效率较高的一种算法
Dim i%, j%, Value%: i = X: j = Y: Value = 0
Do
If Piece(i, j) <> Piece(X, Y) Then Exit Do
i = i + XFluctuate: j = j + YFluctuate '分别在两个方向上加上改变量,下同
Value = Value + 1
Loop Until i < 1 Or i > 15 Or j < 1 Or j > 15
i = X: j = Y
Do
If Piece(i, j) <> Piece(X, Y) Then Exit Do
i = i - XFluctuate: j = j - YFluctuate
Value = Value + 1
Loop Until i < 1 Or i > 15 Or j < 1 Or j > 15
JudgeNum = Value - 1 '减去落子点本身所代表的重复计算过一次的1
End Function
|