vb.net五子棋,用vb编写五子棋
算术运算导致溢出 vb.net
第一问题好理解, 可能 key 是integer类型缘故吧,把它改为Byte。
创新互联长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为叠彩企业提供专业的成都做网站、成都网站建设,叠彩网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
第二个没读懂,你换个方式试试,如:
'Dim bts() As Byte = My.Computer.FileSystem.ReadAllBytes("")
Dim bst As System.IO.BinaryReader = New System.IO.BinaryReader(System.IO.File.OpenRead("D:\Program Files(Green)\人机对战五子棋-绿色免安装单机版v1.0.exe"))
Dim bs() As Byte
Const aLength As Integer = 50000
Do
bs = bst.ReadBytes(aLength)
Console.WriteLine(bs.Length)
Loop Until bs.Length aLength
bst.Close()
文件读到最后一块总会小于指定长度的,你的代码先定义数组长度,然后再读取,这样最后读取的一块二进制数组必然有一部分无效的。
五子棋游戏程序设计(VB)
五子棋的AI构想 有句话叫“当局者迷,旁观者清。”,但这句话在由AI所控制的计算机玩家上是不成立的,因为计算机必须知道有那些获胜方式,并计算出每下一步棋到棋盘上任一格子的获胜几率,也就是说,一个完整的五子棋的AI构想必须:
1、能够知道所有的获胜组合; 2、建立和使用获胜表; 3、设定获胜的分数; 4、使电脑具有攻击和防守的能力; 一、求五子棋的获胜组合 在一场五子棋的游戏中,计算机必须要知道有那些的获胜组合,因此我们必须求得获胜组合的总数。我们假定当前的棋盘为10*10。 (1)计算水平方向的获胜组合数,每一列的获胜组合是:6,共10列,所以水平方向的获胜组合数为:6*10=60 (2)计算垂直方向的获胜组合总数,每一行的获胜组合是:6,共10行,则垂直方向的获胜组合数为:6*10=60 (3)计算正对角线方向的获胜组合总数,正对角线上的获胜组合总数为6+(5+4+3+2+1)*2=36 (4)计算反对角线方向的获胜组合总数,反对角线上的获胜组合总数为6+(5+4+3+2+1)*2=36 ,这样所有的获胜组合数为:60+60+36+36=192 二、建立和使用获胜表 我们已经计算出了一个10*10的五子棋盘会有192种获胜方式,这样我们可以利用数组建立获胜表,获胜表的主要作用是:1,判断当前的获胜方式是否有效;2,判断当前的获胜方式中到底有多少子落入该获胜组合中。详细的使用您将在后面的程序中可以看出。 三,分数的设定 在游戏中为了让计算机能够决定下一步最佳的走法,必须先计算出计算机下到棋盘上任一空格的分数,而其中最高分数便是计算机下一步的最佳走法。 原理:我们判定当前讨论的空格与当前讨论的点有几种获胜的方式,有几种该空格就加几分。这种原理初听起来似乎是无法入手,没关系,当您了解我们后面的程序后您就会明白这种决策原理了。 这种决策有一些缺陷,因为如果只根据这个模型设计,就有可能出现电脑或玩家有三个子连成一线的时候,计算机却判断不出,它认为其他某些空格是当前的获胜的最佳位置而不去攻击或防守。没关系我们完全可以通过一个加强算法来改变当前的分值情况,也就是说当电脑或玩家有三个子或四个子连成一线时,我们通过加强算法将当前与三个子或四个子有关的空格的分值提高,从而可以弥补这一缺憾。 四、攻击与防守 以上的方式,事实上计算机只是计算出了最佳的攻击位置,为了防守我们还应计算当前玩家的最佳的攻击位置。这样有什么用呢?道理很简单,如果玩家最佳攻击位置的分数大于计算机最佳攻击位置上的分数,那么计算机就将下一步的棋子摆在玩家的最佳攻击位上以阻止玩家的进攻,否则计算机便将棋子下在自己的最佳攻击位置上进行攻击。 事实上,这个AI构想是很强大的如果你不是很厉害的五子棋高手的话,可能很快会被计算机打败。我在联众上可是中级棋手啊,跟这种构想打的时候胜率也不是很高。 使用vb.net编写五子棋 一、编写前的准备: 1、用计算机的思想描述整个下棋的过程 考虑步骤: (1)为了简便我们可以先让电脑先走第一步棋,电脑每走一步就会封掉许多玩家的获胜可能情况。 (2)当玩家走棋的时候我们首先应该考虑玩家走棋的合法性。 (3)如果合法,那么玩家也会封掉许多电脑的获胜的可能情况。 (4)电脑的思考路径:首先判断当前玩家和电脑的所有获胜组合是否需要进行加强赋值,
是进行加强赋值,否则进行普通的赋值。 (5)比较当前玩家和电脑谁的分值最大。将分值最大的点作为电脑的下一步走法。 2、利用vb.net窗体和图形工具建立五子棋的棋盘界面 (1)添加一个picturebox控件 作用:使用picturebox控件绘制棋子和棋盘 (2)添加一个label控件 作用:显示当前的获胜标志,也就是当某一方获胜或和棋时显示此标签。 (3)添加一个mainmenu控件 作用:控制游戏的开始或结束 (4)添加一个mediaplay组件 作用:使程序可以播放音乐。 3、设置整体框价 我们采取10*10的棋盘,为主要的平台。利用数组定义整个棋盘桌面,利用数组定义获胜组合以及获胜标志等。 二,声明全局数组和变量 定义虚拟桌面: Dim table(9, 9) As Integer 定义当前玩家桌面空格的分数: Dim pscore(9, 9) As Integer 定义当前电脑桌面空格的分数: Dim cscore(9, 9) As Integer 定义玩家的获胜组合: Dim pwin(9, 9, 191) As Boolean 定义电脑的获胜组合: Dim cwin(9, 9, 191) As Boolean 定义玩家的获胜组合标志: Dim pflag(191) As Boolean 定义电脑的获胜组合标志:
Dim cflag(191) As Boolean 定义游戏有效标志: Dim theplayflag As Boolean 三、初始化游戏 '*****************************************************************************
'** 模块名称: initplayenvironment
'**
'** 描述: 此函数主要功能如下:
'** 1. 设置背景音乐。
'** 2. 设置游戏状态有效。
'** 3. 初始化游戏状态标签。
'** 4. 直接指定电脑的第一步走法。
'** 5. 初始化基本得分桌面。
'** 6. 电脑和玩家获胜标志初始化。
'** 7. 初始化所有获胜组合。
'** 8. 重新设定玩家的获胜标志。
'**
'*****************************************************************************
Sub initplayenvironment()
player.FileName = ".\music\zhyu01.mid"
player.Play()
theplayflag = True
'游戏有效
Label1.Visible = False
'游戏状态标签不显示
PictureBox1.Refresh()
'清空picturebox1的内容
yuandian(130, 130)
'调用绘图函数绘制当前电脑先走的位置
Dim i, j, m, n As Integer
For i = 0 To 9
For j = 0 To 9
table(i, j) = 0
Next
Next
'桌面初始化
For i = 0 To 191
pflag(i) = True
cflag(i) = True
Next
'获胜标志初始化
table(4, 4) = 1
'由于我们设定电脑先手,并下了4,4位所以将其值设为1
''' ******** 初始化获胜组合 ********
n = 0
For i = 0 To 9
For j = 0 To 5
For m = 0 To 4
pwin(j + m, i, n) = True
cwin(j + m, i, n) = True
Next
n = n + 1
Next
Next
For i = 0 To 9
For j = 0 To 5
For m = 0 To 4
pwin(i, j + m, n) = True
cwin(i, j + m, n) = True
Next
n = n + 1
Next
Next
For i = 0 To 5
For j = 0 To 5
For m = 0 To 4
pwin(j + m, i + m, n) = True
cwin(j + m, i + m, n) = True
Next
n = n + 1
Next
Next
For i = 0 To 5
For j = 9 To 4 Step -1
For m = 0 To 4
pwin(j - m, i + m, n) = True
cwin(j - m, i + m, n) = True
Next
n = n + 1
Next
Next
''' ******** 初始化获胜组合结束 ********
For i = 0 To 191
If pwin(4, 4, i) = True Then
pflag(i) = False
End If
Next
'由于电脑已下了4,4位所以我们需要重新设定玩家的获胜标志
End Sub
四,处理鼠标事件 '*****************************************************************************
'** 模块名称: themousedown
'**
'** 描述: 此函数主要实行以下功能:
'** 1. 判定当前游戏标志是否有效。
'** 2. 将实际坐标转化成虚拟坐标。
'** 3. 绘制玩家的棋子。
'** 4. 执行检查获胜函数。
'** 5. 执行电脑算法函数。
'**
'*****************************************************************************
Sub themousedown(ByVal x As Integer, ByVal y As Integer)
If theplayflag = False Then
Exit Sub
End If
'检查游戏状态是否有效
Dim i, j As Integer
Dim zhx, zhy As Integer
zhx = Int((x - 10) / 30)
zhy = Int((y - 10) / 30)
For i = 0 To 9
For j = 0 To 9
If table(zhx, zhy) 0 Then
Exit Sub
End If
Next
Next
'检查当前鼠标点击的格子是否有效
Dim mycolor As Color
Dim g As System.Drawing.Graphics
g = PictureBox1.CreateGraphics
mycolor = Color.White
Dim brush1 As System.Drawing.Brush = New SolidBrush(mycolor)
g.FillEllipse(brush1, zhx * 30 + 10, zhy * 30 + 10, 30, 30)
'绘制玩家的棋子
table(zhx, zhy) = 2
For i = 0 To 191
If cwin(zhx, zhy, i) = True Then
cflag(i) = False
End If
Next
'重设电脑的获胜标志
checkwin()
'检查当前玩家是否获胜
diannao()
'调用电脑算法
End Sub 五、获胜检查算法。 '*****************************************************************************
'** 模块名称: checkwin
'**
'** 描述: 此模块执行以下功能:
'** 1. 检查是否和棋。
'** 2. 检查电脑是否获胜。
'** 3. 检查玩家是否获胜。
'**
'*****************************************************************************
Sub checkwin()
Dim i, j, k, m, n As Integer
Dim ca As Integer
Dim pa As Integer
Dim cnormal As Integer = 0
For i = 0 To 191
If cflag(i) = False Then
cnormal = cnormal + 1
End If
Next
If cnormal = 190 Then
Label1.Visible = True
Label1.Text = "和棋,请重新开始!"
PictureBox1.Refresh()
theplayflag = False
Exit Sub
End If
'设定和棋规则
For i = 0 To 191
If cflag(i) = True Then
ca = 0
For j = 0 To 9
For k = 0 To 9
If table(j, k) = 1 Then
If cwin(j, k, i) = True Then
ca = ca + 1
End If
End If
Next
Next
If ca = 5 Then
Label1.Visible = True
Label1.Text = "电脑获胜,请重新开始"
PictureBox1.Refresh()
theplayflag = False
Exit Sub
End If
End If
Next
'检查电脑是否获胜
For i = 0 To 191
If pflag(i) = True Then
pa = 0
For j = 0 To 9
For k = 0 To 9
If table(j, k) = 2 Then
If pwin(j, k, i) = True Then
pa = pa + 1
End If
End If
Next
Next
If pa = 5 Then
Label1.Visible = True
Label1.Text = "玩家获胜,请重新开始"
PictureBox1.Refresh()
theplayflag = False
Exit Sub
End If
End If
Next
'检查玩家是否获胜
End Sub 六、电脑算法 '*****************************************************************************
'** 模块名称: diannao
'**
'** 描述: 此程序主要执行以下功能:
'** 1. 初始化赋值系统。
'** 2. 赋值加强算法。
'** 3. 计算电脑和玩家的最佳攻击位。
'** 4. 比较电脑和玩家的最佳攻击位并决定电脑的最佳策略。
'** 5. 执行检查获胜函数。
'**
'***************************************************************************** Sub diannao()
Dim i, j, k, m, n As Integer
Dim dc As Integer
Dim cab As Integer
Dim pab As Integer
For i = 0 To 9
For j = 0 To 9
pscore(i, j) = 0
cscore(i, j) = 0
Next
Next
'初始化赋值数组
''' ******** 电脑加强算法 ********
For i = 0 To 191
If cflag(i) = True Then
cab = 0
For j = 0 To 9
For k = 0 To 9
If table(j, k) = 1 Then
If cwin(j, k, i) = True Then
cab = cab + 1
End If
End If
Next
Next
Select Case cab
Case 3
For m = 0 To 9
For n = 0 To 9
If table(m, n) = 0 Then
If cwin(m, n, i) = True Then
cscore(m, n) = cscore(m, n) + 5
End If
End If
Next
Next
Case 4
For m = 0 To 9
For n = 0 To 9
If table(m, n) = 0 Then
If cwin(m, n, i) = True Then
yuandian(m * 30 + 10, n * 30 + 10)
table(m, n) = 1
For dc = 0 To 191
If pwin(m, n, dc) = True Then
pflag(dc) = False
checkwin()
Exit Sub
End If
Next
End If
End If
Next
Next
End Select
End If
Next
For i = 0 To 191
If pflag(i) = True Then
pab = 0
For j = 0 To 9
For k = 0 To 9
If table(j, k) = 2 Then
If pwin(j, k, i) = True Then
pab = pab + 1
End If
End If
Next
Next
Select Case pab
Case 3
For m = 0 To 9
For n = 0 To 9
If table(m, n) = 0 Then
If pwin(m, n, i) = True Then
pscore(m, n) = pscore(m, n) + 30
End If
End If
Next
Next
Case 4
For m = 0 To 9
For n = 0 To 9
If table(m, n) = 0 Then
If pwin(m, n, i) = True Then
yuandian(m * 30 + 10, n * 30 + 10)
table(m, n) = 1
For dc = 0 To 191
If pwin(m, n, dc) = True Then
pflag(dc) = False
checkwin()
Exit Sub
End If
Next
End If
End If
Next
Next
End Select
End If
Next
''' ******** 电脑加强算法结束 ******** ' ******** 赋值系统 ********
For i = 0 To 191
If cflag(i) = True Then
For j = 0 To 9
For k = 0 To 9
If table(j, k) = 0 Then
If cwin(j, k, i) = True Then
For m = 0 To 9
For n = 0 To 9
If table(m, n) = 1 Then
If cwin(m, n, i) = True Then
cscore(j, k) = cscore(j, k) + 1
End If
End If
Next
Next
End If
End If
Next
Next
End If
Next
For i = 0 To 191
If pflag(i) = True Then
For j = 0 To 9
For k = 0 To 9
If table(j, k) = 0 Then
If pwin(j, k, i) = True Then
For m = 0 To 9
For n = 0 To 9
If table(m, n) = 2 Then
If pwin(m, n, i) = True Then
pscore(j, k) = pscore(j, k) + 1
End If
End If
Next
Next
End If
End If
Next
Next
End If
Next
''' ******** 赋值系统结束 ********
''' ******** 分值比较算法 ********
Dim a, b, c, d As Integer
Dim cs As Integer = 0
Dim ps As Integer = 0
For i = 0 To 9
For j = 0 To 9
If cscore(i, j) cs Then
cs = cscore(i, j)
a = i
b = j
End If
Next
Next
For i = 0 To 9
For j = 0 To 9
If pscore(i, j) ps Then
ps = pscore(i, j)
c = i
d = j
End If
Next
Next
If cs ps Then
yuandian(a * 30 + 10, b * 30 + 10)
table(a, b) = 1
For i = 0 To 191
If pwin(a, b, i) = True Then
pflag(i) = False
End If
Next
Else
yuandian(c * 30 + 10, d * 30 + 10)
table(c, d) = 1
For i = 0 To 191
If pwin(c, d, i) = True Then
pflag(i) = False
End If
Next
End If
''' ******** 分值比较算法结束 ********
checkwin()
End Sub 七、绘制棋子 '*****************************************************************************
'** 模块名称: yuandian
'**
'** 描述: 此函数主要进行电脑棋子的绘制。
'**
'***************************************************************************** Sub yuandian(ByVal x As Integer, ByVal y As Integer)
Dim mycolor As Color
Dim g As System.Drawing.Graphics
g = PictureBox1.CreateGraphics
Dim zhx, zhy As Integer
zhx = Int((x - 10) / 30)
zhy = Int((y - 10) / 30)
mycolor = Color.Black
Dim brush1 As System.Drawing.Brush = New SolidBrush(mycolor)
g.FillEllipse(brush1, zhx * 30 + 10, zhy * 30 + 10, 30, 30)
End Sub
急求vb双人对战五子棋源代码
Option Explicit
'五子棋程序 人机对战版本
'需要2个Label控件 2个CommandButton控件
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
'Dim PlayStep() As String '记录棋谱的数组
'Dim Label2Cap As String
Private Const BoxL As Single = 50, BoxT As Single = 50, BoxW As Single = 25, BoxN As Integer = 18
Dim Table() As Long '棋盘(0-BoxN,0-BoxN) 0-空 1-黑子 2-白子
Dim PsCore() As Long '定义当前玩家桌面空格的分数
Dim CsCore() As Long '定义当前电脑桌面空格的分数
Dim pWin() As Boolean '定义玩家的获胜组合
Dim cWin() As Boolean '定义电脑的获胜组合
Dim pFlag() As Boolean '定义玩家的获胜组合标志
Dim cFlag() As Boolean '定义电脑的获胜组合标志
Dim ThePlayFlag As Boolean '定义游戏有效标志
Private Sub Command1_Click()
If Not ThePlayFlag Then Call InitPlayEnvironment: Exit Sub
If MsgBox("本局还没有下完,是否重新开始?(Y/N)", vbYesNo) = vbNo Then Exit Sub
Call InitPlayEnvironment
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Dim i As Long, lw As Long, lh As Long
'Label2Cap = "000 黑方 行 00 列 00"
Me.Width = 10815: Me.Height = 8040
' Me.Caption = "五子棋 - 人机对战": Me.Show
lw = Me.Width \ Screen.TwipsPerPixelX: lh = Me.Height \ Screen.TwipsPerPixelY
SetWindowRgn Me.hWnd, CreateRoundRectRgn(0, 0, lw, lh, 60, 60), True
With Label1
.Alignment = vbCenter: .FontSize = 12: .FontBold = True
.ForeColor = vbRed: .BackStyle = 0: .AutoSize = True: .Move 8910, 510
End With
Label2.AutoSize = True: Label2.WordWrap = True
Label2.BackStyle = 0: Label2.Move 8040, 1050, 2280
Command1.Move 8025, 7035, 1020, 435: Command1.Caption = "再来一局"
Command2.Move 9300, 7035, 1020, 435: Command2.Caption = "不玩了"
Call DrawChessBoard: Me.FillStyle = 0: Call InitPlayEnvironment
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
End
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iRow As Long, iCol As Long, i As Long, k As Long, t As String
If Not ThePlayFlag Then Exit Sub
If Button = vbLeftButton Then '左键下棋
iRow = -1: iCol = -1
For i = 0 To BoxN '鼠标必须落在交叉点 半径10以内 若是则给出行列号
If (Y + 10) (BoxT + i * BoxW) And (Y - 10) = (BoxT + i * BoxW) Then iRow = i
If (X + 10) (BoxL + i * BoxW) And (X - 10) = (BoxL + i * BoxW) Then iCol = i
Next
If (iRow = -1) Or (iCol = -1) Then Beep: Exit Sub
If Table(iCol, iRow) 0 Then Exit Sub
Table(iCol, iRow) = 2: Label1.Caption = "下一步 黑方"
Me.FillColor = vbWhite: Me.Circle (iCol * BoxW + BoxT, iRow * BoxW + BoxL), 8
For i = 0 To UBound(cWin, 3)
If cWin(iCol, iRow, i) = True Then cFlag(i) = False
Next
Call CheckWin: Call DianNao '检查当前玩家是否获胜 调用电脑算法
End If
End Sub
Public Sub InitPlayEnvironment()
'*****************************************************************************
' 模块名称: InitPlayEnvironment [初始化过程]
'
' 描述: 1. 设置背景音乐。 2. 设置游戏状态有效。
' 3. 初始化游戏状态标签。 4. 直接指定电脑的第一步走法。
' 5. 初始化基本得分桌面。 6. 电脑和玩家获胜标志初始化。
' 7. 初始化所有获胜组合。 8. 重新设定玩家的获胜标志。
'*****************************************************************************
Dim i As Long, j As Long, m As Long, n As Long
ThePlayFlag = True: Label1.Caption = "下一步 白方": Label2.Caption = ""
Me.FillColor = vbBlack: Me.FillStyle = 0: Me.AutoRedraw = True
Me.Cls: Me.Circle (9 * BoxW + BoxL, 9 * BoxW + BoxT), 8
ReDim Table(0 To BoxN, 0 To BoxN) As Long
ReDim pFlag(NumsWin(BoxN + 1) - 1) As Boolean
ReDim cFlag(UBound(pFlag)) As Boolean
ReDim PsCore(BoxN, BoxN) As Long, CsCore(BoxN, BoxN) As Long
ReDim pWin(BoxN, BoxN, UBound(pFlag)) As Boolean
ReDim cWin(BoxN, BoxN, UBound(pFlag)) As Boolean
For i = 0 To UBound(pFlag): pFlag(i) = True: cFlag(i) = True: Next
Table(9, 9) = 1 '假定电脑先手 并下了(9, 9)位 将其值设为1
'******** 初始化获胜组合 ****************************************
For i = 0 To BoxN: For j = 0 To BoxN - 4
For m = 0 To 4
pWin(j + m, i, n) = True: cWin(j + m, i, n) = True
Next
n = n + 1
Next: Next
For i = 0 To BoxN: For j = 0 To BoxN - 4
For m = 0 To 4
pWin(i, j + m, n) = True: cWin(i, j + m, n) = True
Next
n = n + 1
Next: Next
For i = 0 To BoxN - 4: For j = 0 To BoxN - 4
For m = 0 To 4
pWin(j + m, i + m, n) = True: cWin(j + m, i + m, n) = True
Next
n = n + 1
Next: Next
For i = 0 To BoxN - 4: For j = BoxN To 4 Step -1
For m = 0 To 4
pWin(j - m, i + m, n) = True: cWin(j - m, i + m, n) = True
Next
n = n + 1
Next: Next
'******** 初始化获胜组合结束 *************************************
For i = 0 To UBound(pWin, 3) '由于电脑已下了(9, 9)位 所以需要重新设定玩家的获胜标志
If pWin(9, 9, i) = True Then pFlag(i) = False
Next
End Sub
Public Function DrawChessBoard() As Long
'容器的(BoxL, BoxT)为左上角坐标画一个 BoxN*BoxN, 每格边长为 BoxW 象素的棋盘
Dim i As Long, j As Long, cx As Long, cy As Long
Me.ScaleMode = 3: Me.FillStyle = 1: Me.AutoRedraw = True: Me.Cls
For i = 0 To BoxN '画棋盘
Me.Line (BoxL + i * BoxW, BoxT)-(BoxL + i * BoxW, BoxT + BoxN * BoxW)
Me.Line (BoxL, BoxT + i * BoxW)-(BoxL + BoxN * BoxW, BoxT + i * BoxW)
Me.CurrentX = BoxL + i * BoxW - IIf(i 9, 6, 2)
Me.CurrentY = BoxT - 20: Me.Print Format(i)
Me.CurrentX = BoxL - IIf(i 9, 23, 20)
Me.CurrentY = BoxT + i * BoxW - 6: Me.Print Format(i)
Next
For i = 3 To 16 Step 6: For j = 3 To 16 Step 6 '画小标志
cx = BoxL + j * BoxW - 3: cy = BoxT + i * BoxW - 3
Me.Line (cx, cy)-(cx + 6, cy + 6), , B
Next: Next
Me.AutoRedraw = False: Set Me.Picture = Me.Image
End Function
Public Sub CheckWin()
'*****************************************************************************
' 模块名称: CheckWin [获胜检查算法]
'
' 描述: 1. 检查是否和棋。 2. 检查电脑是否获胜。 3. 检查玩家是否获胜。
'*****************************************************************************
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim cA As Long, pA As Long, cN As Long
For i = 0 To UBound(cFlag): cN = IIf(cFlag(i) = False, cN + 1, cN): Next
If cN = UBound(cFlag) - 1 Then '设定和棋规则
Label1.Caption = "双方和棋!": ThePlayFlag = False: Exit Sub
End If
For i = 0 To UBound(cFlag) '检查电脑是否获胜
If cFlag(i) = True Then
cA = 0: For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 1 And cWin(j, k, i) = True Then cA = cA + 1
Next: Next
If cA = 5 Then Label1.Caption = "电脑获胜!": ThePlayFlag = False: Exit Sub
End If
Next
For i = 0 To UBound(pFlag) '检查玩家是否获胜
If pFlag(i) = True Then
pA = 0: For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 2 And pWin(j, k, i) = True Then pA = pA + 1
Next: Next
If pA = 5 Then Label1.Caption = "玩家获胜!": ThePlayFlag = False: Exit Sub
End If
Next
End Sub
Public Sub DianNao()
'*****************************************************************************
' 模块名称: DianNao [电脑算法]
' 描述: 1. 初始化赋值系统。 2. 赋值加强算法。 3. 计算电脑和玩家的最佳攻击位。
' 4. 比较电脑和玩家的最佳攻击位并决定电脑的最佳策略。 5. 执行检查获胜函数。
'*****************************************************************************
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim Dc As Long, cAb As Long, pAb As Long
ReDim PsCore(BoxN, BoxN) As Long, CsCore(BoxN, BoxN) As Long '初始化赋值数组
'******** 电脑加强算法 ********
For i = 0 To UBound(cFlag)
If cFlag(i) = True Then
cAb = 0
For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 1 And cWin(j, k, i) = True Then cAb = cAb + 1
Next: Next
Select Case cAb
Case 3
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And cWin(m, n, i) = True Then CsCore(m, n) = CsCore(m, n) + 5
Next: Next
Case 4
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And cWin(m, n, i) = True Then
Table(m, n) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (m * BoxW + BoxL, n * BoxW + BoxT), 8
For Dc = 0 To UBound(pWin, 3)
If pWin(m, n, Dc) = True Then pFlag(Dc) = False: Call CheckWin: Exit Sub
Next
End If
Next: Next
End Select
End If
Next
For i = 0 To UBound(pFlag)
If pFlag(i) = True Then
pAb = 0
For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 2 And pWin(j, k, i) = True Then pAb = pAb + 1
Next: Next
Select Case pAb
Case 3
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And pWin(m, n, i) = True Then PsCore(m, n) = PsCore(m, n) + 30
Next: Next
Case 4
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And pWin(m, n, i) = True Then
Table(m, n) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (m * BoxW + BoxL, n * BoxW + BoxT), 8
For Dc = 0 To UBound(pWin, 3)
If pWin(m, n, Dc) = True Then pFlag(Dc) = False: Call CheckWin: Exit Sub
Next
End If
Next: Next
End Select
End If
Next
'******** 电脑加强算法结束 ********
'******** 赋值系统 ****************
For i = 0 To UBound(cFlag)
If cFlag(i) = True Then
For j = 0 To BoxN: For k = 0 To BoxN
If (Table(j, k) = 0) And cWin(j, k, i) Then
For m = 0 To BoxN: For n = 0 To BoxN
If (Table(m, n) = 1) And cWin(m, n, i) Then CsCore(j, k) = CsCore(j, k) + 1
Next: Next
End If
Next: Next
End If
Next
For i = 0 To UBound(pFlag)
If pFlag(i) = True Then
For j = 0 To BoxN: For k = 0 To BoxN
If (Table(j, k) = 0) And pWin(j, k, i) Then
For m = 0 To BoxN: For n = 0 To BoxN
If (Table(m, n) = 2) And pWin(m, n, i) Then PsCore(j, k) = PsCore(j, k) + 1
Next: Next
End If
Next: Next
End If
Next
'******** 赋值系统结束 ************
'******** 分值比较算法 ************
Dim a As Long, b As Long, c As Long, d As Long
Dim cS As Long, pS As Long
For i = 0 To BoxN: For j = 0 To BoxN
If CsCore(i, j) cS Then cS = CsCore(i, j): a = i: b = j
Next: Next
For i = 0 To BoxN: For j = 0 To BoxN
If PsCore(i, j) pS Then pS = PsCore(i, j): c = i: d = j
Next: Next
If cS pS Then
Table(a, b) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (a * BoxW + BoxL, b * BoxW + BoxT), 8
For i = 0 To UBound(pWin, 3)
If pWin(a, b, i) = True Then pFlag(i) = False
Next
Else
Table(c, d) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (c * BoxW + BoxL, d * BoxW + BoxL), 8
For i = 0 To UBound(pWin, 3)
If pWin(c, d, i) = True Then pFlag(i) = False
Next
End If
'******** 分值比较算法结束 ********
Call CheckWin
End Sub
Public Function NumsWin(ByVal n As Long) As Long
'根据输入的棋盘布局 n*n 计算总共有多少种获胜组合
'假定棋盘为 10 * 10 相应的棋盘数组就是 Table(9, 9)
'水平方向 每一列获胜组合是6 共10列 6*10=60
'垂直方向 每一行获胜组合是6 共10行 8*10=60
'正对角线方向 6 + (5 + 4 + 3 + 2 + 1) * 2 = 36
'反对角线方向 6 + (5 + 4 + 3 + 2 + 1) * 2 = 36
'总的获胜组合数为 60 + 60 + 36 + 36 = 192
Dim i As Long, t As Long
For i = n - 5 To 1 Step -1: t = t + i: Next
NumsWin = 2 * (2 * t + n - 4) + 2 * n * (n - 4)
End Function
C#写五子棋,如何实现,落子
软糖有一个五子棋源码,不过是VB.net的。
棋盘是用int[,]二维数组做的,值为0就是空,1是白方,2是黑方。
落子判断值是否为0,然后根据当前玩家的赋值为1或2。
''' summary
''' 返回玩家能否在棋盘(X,Y)的位置下棋
''' /summary
''' param name="X"横向坐标/param
''' param name="Y"纵向坐标/param
''' returns如果已经有棋子或不在棋盘上,则返回False/returns
''' remarks本函数会调用检查五子并对检查结果进行判断/remarks
Public Function 下棋(ByVal X As Integer, ByVal Y As Integer) As Boolean
If 棋盘(X, Y) 0 Then Return False
棋盘(X, Y) = M当前玩家
Dim 检查结果 As Integer
检查结果 = 检查五子(X, Y)
If 检查结果 0 Then 玩家获胜(检查结果) Else 交换玩家()
Return True
End Function
网站栏目:vb.net五子棋,用vb编写五子棋
网页链接:http://abwzjs.com/article/hcoddh.html