t = DoGen(PieceTar, i, j, Flag)
If t = TTDL Then GetVitalStep = i + (j - 1) * 15: ScoreTar = TTDL: Exit Function
End If
Next j
Next i
For i = 1 To 15 '扫描对方双活三棋形
For j = 1 To 15
If PieceTar(i, j) = 0 Then
t = DoGen(PieceTar, i, j, FlagX)
If t = TTDL Then GetVitalStep = i + (j - 1) * 15: ScoreTar = TTDL: Exit Function
End If
Next j
Next i
Dim iMax%, jMax%, max%: max = 0 '若前面的棋形均无则自动取得分函数计算最大值点作为落子点
For i = 1 To 15
For j = 1 To 15
If PieceTar(i, j) = 0 Then
t = DoGen(PieceTar, i, j, Flag)
r = DoGen(PieceTar, i, j, FlagX)
If t + r > max Then
max = t + r: iMax = i: jMax = j
End If
PieceTar(i, j) = 0
End If
Next j
Next i
ScoreTar = max: GetVitalStep = iMax + (jMax - 1) * 15
End Function
Public Function Swap(ByRef Val1 As Variant, ByRef Val2 As Variant)
’传址变量交换函数,在排序中会用到
Dim t As Variant
t = Val1: Val1 = Val2: Val2 = t
End Function
到此为止,电脑AI的设计就告一段落。程序还应具备基本的写出棋谱的能力,并应尽可能的模块化。写棋谱函数的返回值为布尔型,写出为真;反之为假。文件结构由于其有序性而采用顺序文件结构。棋谱文件中每一项记录的组成:[横坐标][纵坐标][玩家标记]。棋盘的长宽是大于十的,因而我们采用十六进制数来表示棋盘坐标。在悔棋记录数组的基础之上,可利用其方便地实现棋谱的文件化输出。实现代码如下:
Public Function WriteChessManual(ByVal FileName As String, ByRef PieceTar() As Integer) As Boolean '写出文件成功返回真,取消或失败返回假
Dim FileNum%, i%, j%: Dim tmpx$, tmpy$
|