Public Function DrawPiece(ByRef FormTar As Form, ByVal XHorizontal%, ByVal YVertical%, ByVal Flag As Byte)
'在画好棋盘的窗体上画棋子,参数Flag表示玩家的情况,数值与棋子的表示相同
If Flag = 1 Then
FormTar.FillColor = RGB(0, 0, 0)
FormTar.Circle (20 + (XHorizontal - 1) * 30, 20 + (YVertical - 1) * 30), 13, RGB(255, 255, 255)
ElseIf Flag = 2 Then
FormTar.FillColor = RGB(255, 255, 255)
FormTar.Circle (20 + (XHorizontal - 1) * 30, 20 + (YVertical - 1) * 30), 13, RGB(0, 0, 0)
End If
End Function
除了棋盘界面的画棋子函数,需要实现棋局数组在棋盘上的动态显示;可利用两个函数访问保存棋局信息的数组,并调用画棋盘与画棋子函数来动态更新棋盘之显示。以下即为实现动态变更棋子数组的落子函数:
Public Function LayPiece(ByRef Piece() As Integer, ByVal XInt%, ByVal YInt%, ByVal Flag As Byte) '落子
Piece(XInt, YInt) = Flag: X = XInt: Y = YInt: CountNum = CountNum + 1
PieceRec(SearchNode(PieceRec) + 1) = (YInt - 1) * 15 + XInt
'扩大记步数组变量范围以保存本步
If JudgeWinner(Piece, XInt, YInt, Flag) = True Then MsgBox IIf(Flag = 1, "黑棋", "白棋") & "获胜!", vbExclamation + vbOKOnly: GameOrNot = False '调用判断赢家函数判断胜负
If CountNum = 220 Then MsgBox "和棋。", vbOKOnly + vbExclamation
End Function
数据能够实时更新后,又有了画棋子与画棋盘函数的支持;以下即为在前面几个函数基础之上的棋盘数据实时更新函数:
Public Function PadRefresh(ByRef FormTar As Form, ByRef Piece() As Integer) '棋盘恢复函数
FormTar.Cls
Drawpad FormTar
Dim i%, j%
For i = 1 To 15
For j = 1 To 15
If Piece(i, j) <> 0 Then DrawPiece FormTar, i, j, Piece(i, j)
'如果该位置非空子,则画棋子
Next j
Next i
End Function
|