完成了界面,实现双人对战就很简单了。
1、定义公共变量和结构变量
Public Structure FivePos
Dim x As Integer
Dim y As Integer
Dim color As Integer
End Structure
Public fpGoPos(225) As FivePos '记录下子顺序和位置
Public intGoType(224) As Integer '记录下子分布
Public intFirstGo As Integer '谁先手
Public bLimit As Boolean '是否禁手
Public intStep As Integer '已走步数
Dim intPlayer1, intPlayer2 As Integer '双人对战
Const conBlack = 1‘黑子
Const conWhite = 0’白子
Const conNull = 2‘空位
2、Form1_Load事件中设置
intPlayer1 = conBlack
intPlayer2 = conWhite
intFirstGo = conBlack
intStep = 0
bLimit = True
3、panFive_Paint添加检测是否胜利、禁手的代码,注意几点
a、intStep > 7时开始检测‘>8会漏掉连五可能
b、禁手时是对方胜利
c、双三禁手中必须都是活三,双四禁手中可以是冲四、活四
4、panFive_MouseDown
a、要检测落子范围是否在棋盘内
b、检查是否该位置已下子
c、落子颜色的变化,参考以下代码
intStep = intStep + 1
fpGoPos(intStep).x = intTempX
fpGoPos(intStep).y = intTempY
If intStep Mod 2 = 1 Then
fpGoPos(intStep).color = intFirstGo
Else
fpGoPos(intStep).color = (intFirstGo + 1) Mod 2 '转换成另一颜色,免于判断
End If
intGoType(intTempX * 15 + intTempY) = fpGoPos(intStep).color
'绘制此步棋子
Dim sdr = CType(sender, Panel)
panFive_Paint(sdr, New PaintEventArgs(sdr.CreateGraphics, sdr.DisplayRectangle))
'panFive.Refresh()’将清除所有棋子
5、完善一下提示功能、新游戏开始初始化功能等
这样就完成了双人对战的功能