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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章