VB.Net五子棋代码

Public Class 五子棋
Dim g As Graphics ‘/定义Graphics对象
Dim BlackPen As New Pen(Color.Black, 2) ‘/定义一个黑色画笔
Dim WhitePen As New Pen(Color.White, 2) ‘/定义一个黑色画笔

Dim BlackPointlst As New List(Of Point)    '/声明集合记录黑子点坐标
Dim WhitPointlst As New List(Of Point)    '/声明集合记录白子点坐标

Dim AllPointlst As New List(Of Point)      '/所有点的集合

Dim intChessCount As Integer = 0   '/棋子的数量

Dim BlackBrush As New SolidBrush(Color.Black)  '/声明黑色刷子
Dim WhiteBrush As New SolidBrush(Color.White)  '/声明白色刷子

''' <summary>
''' 开始按钮事件
''' </summary>
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    '初始化集合
    BlackPointlst.Clear()
    WhitPointlst.Clear()
    AllPointlst.Clear()
    intChessCount = 0

    '定义坐标点
    Dim Drawpoint(1) As Point

    '画棋盘,并以背景色清除
    g = Me.CreateGraphics()
    g.Clear(BackColor)

    '画X方向的直线
    For i = 0 To 450 Step 30
        g.DrawLine(Pens.Black, 50, 50 + i, 500, 50 + i)

        '添加点到AllPointlst集合
        Drawpoint(0).X = 50 : Drawpoint(0).Y = 50 + i
        Drawpoint(1).X = 500 : Drawpoint(1).Y = 50 + i
        AllPointlst.AddRange(Drawpoint)
    Next

    '画Y方向的直线
    For i = 0 To 450 Step 30
        g.DrawLine(Pens.Black, 50 + i, 50, 50 + i, 500)

        '添加点到AllPointlst集合
        Drawpoint(0).X = 50 + i : Drawpoint(0).Y = 50
        Drawpoint(1).X = 50 + i : Drawpoint(1).Y = 500
        AllPointlst.AddRange(Drawpoint)
    Next
End Sub

''' <summary>
''' 释放鼠标按钮事件
''' </summary>
Private Sub Form1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    Dim MouseLoction As Point   '/记录鼠标坐标
    Dim GridLoction As Point    '/记录网格坐标

    Dim blnlogx As Boolean = False  '/记录X状态
    Dim blnlogy As Boolean = False  '/记录Y状态

    Dim FunSuccess As Boolean '/判断是否成功
    Dim FunExist As Boolean   '/判断点是否已经存在

    '记录鼠标坐标
    MouseLoction = e.Location

    '将鼠标坐标转化为网格坐标
    For i = 0 To AllPointlst.Count - 1
        If AllPointlst(i).X - 10 < MouseLoction.X And MouseLoction.X < AllPointlst(i).X + 10 Then
            GridLoction.X = AllPointlst(i).X
            blnlogx = True
        End If

        If AllPointlst(i).Y - 10 < MouseLoction.Y And MouseLoction.Y < AllPointlst(i).Y + 10 Then
            GridLoction.Y = AllPointlst(i).Y
            blnlogy = True
        End If
    Next

    '判断点是否已经存在
    FunExist = CheckPoint(GridLoction, WhitPointlst, BlackPointlst)

    '判断是否下棋
    If blnlogx = True And blnlogy = True And FunExist = False Then  '/条件成立表示下棋条件满足
        intChessCount = intChessCount + 1

        If intChessCount Mod 2 = 1 Then '/条件成立表示下黑子

            '将黑子的点添加到黑子集合里面去
            BlackPointlst.Add(GridLoction)

            '填充黑子
            If FunSuccess = False Then
                g.FillEllipse(BlackBrush, GridLoction.X - 10, GridLoction.Y - 10, 20, 20)
                FunSuccess = Success(GridLoction, BlackPointlst, False)
            End If

        ElseIf intChessCount Mod 2 = 0 And intChessCount <> 0 Then  '/条件成立下白子

            '白子的点添加到白子集合里面去
            WhitPointlst.Add(GridLoction)

            '填充白子
            If FunSuccess = False Then
                g.FillEllipse(WhiteBrush, GridLoction.X - 10, GridLoction.Y - 10, 20, 20)
                FunSuccess = Success(GridLoction, WhitPointlst, True)
            End If

        End If
    End If

End Sub

''' <summary>
''' 判断是否成功
''' </summary>
''' <param name="GridLoction">最后一个点的坐标</param>
''' <param name="Pointlst">点的集合</param>
''' <param name="WhiteOrBlack">true,表示白棋子;false,表示黑棋子</param>
''' <returns>false,表示没有成功;true,表示成功</returns>
Private Function Success(ByVal GridLoction As Point, ByVal Pointlst As List(Of Point), ByVal WhiteOrBlack As Boolean) As Boolean

    Dim RetFun As Boolean = False  '/定义函数返回值
    Dim intmark(4) As Integer '/定义为编辑标记
    Dim PointArr(4) As Point '/定义为五个点的坐标数组
    Dim intmarkCount As Integer '/定义标记数组的集合
    Dim directionarr() As Integer = {0, 1, 2, 3}

    If Pointlst.Count >= 5 Then '/当点的数量大于5等于5的时候才开始判断

        For m = 0 To UBound(directionarr) '/表示方向的:0,表示西北到东南方向;'1表示西南到东北方向;'2表示西到东方向;3表示自北到南方向
            For j = 0 To 9
                ReDim intmark(4)
                If m = 0 Then '/0表示西北到东南方向
                    For i = 0 To UBound(PointArr)
                        PointArr(i).X = (j * 30 + GridLoction.X) - 30 * i
                        PointArr(i).Y = (j * 30 + GridLoction.Y) - 30 * i
                    Next
                ElseIf m = 1 Then '/1表示西南到东北方向
                    For i = 0 To UBound(PointArr)
                        PointArr(i).X = (j * 30 + GridLoction.X) - 30 * i
                        PointArr(i).Y = (-j * 30 + GridLoction.Y) + 30 * i
                    Next
                ElseIf m = 2 Then '/2表示西到东方向
                    For i = 0 To UBound(PointArr)
                        PointArr(i).X = (j * 30 + GridLoction.X) - 30 * i
                        PointArr(i).Y = GridLoction.Y
                    Next
                ElseIf m = 3 Then '/3表示自北到南方向
                    For i = 0 To UBound(PointArr)
                        PointArr(i).X = GridLoction.X
                        PointArr(i).Y = (j * 30 + GridLoction.Y) - 30 * i
                    Next
                End If

                '判断list列表里面包含点的个数
                For i = 0 To UBound(PointArr)
                    If Pointlst.Contains(PointArr(i)) Then
                        intmark(i) = 1
                    Else
                        intmark(i) = 0
                    End If
                Next
                intmarkCount = intmark(0) + intmark(1) + intmark(2) + intmark(3) + intmark(4)

                '当满足条件是决出胜负
                If intmarkCount = 5 And WhiteOrBlack = True Then '/表示白棋子获胜
                    MsgBox("白棋子获胜")
                    g.DrawLine(BlackPen, PointArr(0), PointArr(4))
                    Label1.Text = "白棋子获胜"
                    RetFun = True
                    Return RetFun
                ElseIf intmarkCount = 5 And WhiteOrBlack = False Then '/表示黑棋子获胜
                    MsgBox("黑棋子获胜")
                    g.DrawLine(WhitePen, PointArr(0), PointArr(4))
                    Label1.Text = "黑棋子获胜"
                    RetFun = True
                    Return RetFun
                End If
            Next
        Next
    End If

    Return RetFun
End Function

''' <summary>
''' 判断是否已经存在该点
''' </summary>
Private Function CheckPoint(ByVal GridPoint As Point, ByVal WhitPointlst As List(Of Point), ByVal BlackPointlst As List(Of Point)) As Boolean

    '包含黑色和白色的棋子的总和
    Dim Pointlst As New List(Of Point)

    '将黑色和白色的点添加到 Pointlst里面
    Pointlst.AddRange(WhitPointlst)
    Pointlst.AddRange(BlackPointlst)

    '点在已经存在,则返回true, 否则返回False
    For i = 0 To Pointlst.Count - 1
        If Pointlst.Contains(GridPoint) Then
            Return True
        Else
            Return False
        End If
    Next

    Return False
End Function

End Class

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值