跪求一个VC++最简单的五子棋源代码
发布网友
发布时间:2024-02-20 12:09
我来回答
共1个回答
热心网友
时间:2024-03-13 23:47
'模块1
Mole Mole1
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 '电脑下棋数加1
'绘制电脑下的棋子
Dim mycolor As Color
Dim g As System.Drawing.Graphics
g = Form1.PictureBox1.CreateGraphics
mycolor = Color.Red
Dim brush1 As System.Drawing.Brush = New SolidBrush(mycolor)
g.FillEllipse(brush1, l * 30, m * 30, 30, 30)
'下面判断胜负
For k = 0 To 479
If ChessNum(0) = 98 And ChessNum(1) = 98 Then '如果玩家已下了50个棋子即为和棋
MsgBox("和棋!各有千秋.....")
IsOver = True '游戏结束
End
ElseIf CTab(l, m, k) = True Then '否则如果电脑在第k获胜组合中已下了个棋子,那么电脑第k获胜组合值加1
GWin(1, k) = GWin(1, k) + 1
PTab(l, m, k) = False
GWin(0, k) = 6 '玩家第k获胜组合不可能获胜
If GWin(1, k) = 5 Then '电脑第k获胜组合值达到了5,那么电脑胜利
Form1.StatusBar1.Panels(0).Text = "下次努力哦!"
MsgBox("电脑赢了!")
IsOver = True '游戏结束
End
End If
End If
Next k
Form1.StatusBar1.Panels(0).Text = "该你下了,玩家棋数:" & ChessNum(0)
IsOver = False '游戏没有结束
End Sub
End Mole
-----------------------------------------------------------------华丽的分割线-------------------------------------------------
主窗体
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.Enabled = False
'游戏数据初始化
Call GameInit()
End Sub
Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
Dim a As Single
Dim b As Single
'获取鼠标单击位置值
a = e.X
b = e.Y
If IsOver = False Then
Call PersonPlay(a, b)
End If
End Sub
'绘制棋盘网格
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint '在重绘控件时发生
Dim i As Integer
Dim g As Graphics = e.Graphics '定义一个图形类对象
Dim mypen As System.Drawing.Pen = New System.Drawing.Pen(Color.Blue, 2)
'绘制棋盘
For i = 0 To 14
g.DrawLine(mypen, 30 * i, 0, 30 * i, 420)
g.DrawLine(mypen, 0, 30 * i, 420, 30 * i)
Next
End Sub
Private Sub TSMenuItemStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenuItemStart.Click
PictureBox1.Enabled = True
Me.StatusBar1.Panels(0).Text = "玩家先下"
End Sub
Private Sub 退出EToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出EToolStripMenuItem.Click
Me.Close()
End Sub
End Class
不是原创