[QQ游戏五子棋里什么叫有禁手?无禁手]有禁手是指黑棋不能在落一子的情况下形成两个活三,两个可能成五的四以及六子连珠。一旦黑棋走禁手,则立即判负。这些禁手被称为三三禁手、四四禁手、长连禁手。无禁手就是黑棋...+阅读
'模块1 Module Module1 Dim i As Integer Dim j As Integer Dim k As Integer Dim PTab(13, 13, 479) As Boolean '定义玩家的获胜组合数组 Dim CTab(13, 13, 479) As Boolean '定义电脑的获胜组合数组 Dim GBoard(13, 13) As Boolean '定义棋盘盘面数组 Dim GWin(1, 479) As Integer '定义游戏胜负数组(第一维0表示玩家的,1表示电脑的) Public IsOver As Boolean '定义是否结束游戏标记(True表示结束,False表示没有结束) Dim Count As Integer '定义获胜组合数目 Dim ChessNum
(1) As Integer '定义玩家和电脑所用棋子数数组( ChessNum(0)表示玩家, ChessNum
(1)表示电脑) '初始化游戏 Public Sub GameInit() '初始化获胜组合 Count = 0 '横向获胜组合 For i = 0 To 13 For j = 0 To 9 For k = 0 To 4 PTab(i, j + k, Count) = True CTab(i, j + k, Count) = True Next k Count = Count + 1 Next j Next i '竖向获胜组合 For i = 0 To 9 For j = 0 To 13 For k = 0 To 4 PTab(i + k, j, Count) = True CTab(i + k, j, Count) = True Next k Count = Count + 1 Next j Next i '正斜方向(135/315度)的获胜组合 For i = 0 To 9 For j = 0 To 9 For k = 0 To 4 PTab(i + k, j + k, Count) = True CTab(i + k, j + k, Count) = True Next k Count = Count + 1 Next j Next i '反斜方向(45/225度)的获胜组合 For i = 0 To 9 For j = 4 To 13 For k = 0 To 4 PTab(i + k, j - k, Count) = True CTab(i + k, j - k, Count) = True Next k Count = Count + 1 Next j Next i '初始化棋盘的是否可下(True为可以下) For i = 0 To 13 For j = 0 To 13 GBoard(i, j) = True Next j Next i '初始化判断胜负数据 For i = 0 To 479 GWin(0, i) = 0 GWin(1, i) = 0 Next i IsOver = False End Sub '玩家下棋(参数x,y表示玩家点击的鼠标位置) Public Sub PersonPlay(ByVal x As Single, ByVal y As Single) Dim XBoard As Integer '对应棋盘上的横坐标 Dim YBoard As Integer '对应棋盘上的纵坐标 XBoard = Int(x / 30) '30像素为每个棋盘方格的宽度与高度 YBoard = Int(y / 30) If GBoard(XBoard, YBoard) = True Then '如果(XBoard, YBoard)位置可以下棋,那么玩家所用棋子数加1,并且在该位置绘制出一个黑色实心圆 ChessNum(0) = ChessNum(0) + 1 Dim mycolor As Color Dim g As System.Drawing.Graphics g = Form1.PictureBox1.CreateGraphics mycolor = Color.Black Dim brush1 As System.Drawing.Brush = New SolidBrush(mycolor) g.FillEllipse(brush1, XBoard * 30, YBoard * 30, 30, 30) GBoard(XBoard, YBoard) = False '标记该位置不可以下棋子(即已下了棋子在该位置) Else '否则不可在此处下棋,给出提示,退出玩家该次下棋 Form1.StatusBar1.Panels(0).Text = "警告:此处不可以下棋!" Exit Sub End If '判断玩家是否获胜(在所有获胜组合里查询) For i = 0 To 479 If PTab(XBoard, YBoard, i) = True Then '如果第i获胜组合的(XBoard, YBoard)位置被玩家占据,那么电脑第i获胜组合的(XBoard, YBoard)位置就置为Flase,即想通过该位置的第i获胜组合获胜已不可能 CTab(XBoard, YBoard, i) = False GWin(0, i) = GWin(0, i) + 1 '玩家第i获胜组合值加1 GWin(1, i) = 6 '电脑通过第i获胜组合获胜已不可能,在此把GWin(1, i)置6 End If If GWin(0, i) = 5 Then '玩家某获胜组合值为5,也即该获胜组合的5个位置都下了棋子,那么玩家胜利 MsgBox("恭喜,玩家胜利!") IsOver = True '标志结束该盘棋 End End If Next i IsOver = False '该盘棋还没结束 Form1.StatusBar1.Panels(0).Text = "电脑思考中,请稍后......" Call ComputerPlay() '调用电脑下棋 End Sub '电脑AI判断下棋 Public Sub ComputerPlay() Dim Score(1, 13, 13) As Long Dim Max As Long Dim l As Integer Dim m As Integer Max = 0 For i = 0 To 13 For j = 0 To 13 Score(0, i, j) = 0 Score(1, i, j) = 0 If GBoard(i, j) = True Then ' 如果(i,j)位置可以下棋 For k = 0 To 479 '计算玩家在此空格上的分数 If PTab(i, j, k) = True And GWin(0, k)6 Then Select Case GWin(0, k) Case 0 Score(0, i, j) = Score(0, i, j) + 1 Case 1 Score(0, i, j) = Score(0, i, j) + 200 Case 2 Score(0, i, j) = Score(0, i, j) + 400 Case 3 Score(0, i, j) = Score(0, i, j) + 2000 Case 4 Score(0, i, j) = Score(0, i, j) + 10000 End Select End If '计算电脑在此空格上的分数 If CTab(i, j, k) = True And GWin(1, k)6 Then Select Case GWin(1, k) Case 0 Score(1, i, j) = Score(1, i, j) + 1 Case 1 Score(1, i, j) = Score(1, i, j) + 200 Case 2 Score(1, i, j) = Score(1, i, j) + 400 Case 3 Score(1, i, j) = Score(1, i, j) + 2000 Case 4 Score(1, i, j) = Score(1, i, j) + 10000 End Select End If '比较分数大小,确定电脑下棋位置 If Score(0, i, j) >Max Then Max = Score(0, i, j) l = i m = j ElseIf Score(0, i, j) = Max Then If Score(1, i, j) >Score(1, l, m) Then l = i m = j End If End If If Score(1, i, j) >Max Then l = i m = j ElseIf Score(1, i, j) = Max Then If Score(0, i, j) >Score(0, l, m) Then l = i m = j End If End If Next k End If Next j Next i '执行电脑下棋程序 GBoard(l, m) = False '电脑在(l, m)位置下棋,该位置就不可下棋了 ChessNum
(1) = ChessNum
(1) + 1 '电脑下棋...