Public Class Form1
    Private Enum Chess
        none = 0
        Black
        White
    End Enum
    Private Box As Chess(,) = New Chess(14, 14) {}
    Private mplayer As Chess = Chess.Black        '假设持黑棋
    Private r As Integer
    '绘制棋盘的方法DrawBoard()：
    Private Sub DrawBoard()
        Dim i As Integer
        '获取对将用于绘图的图形对象的引用创建图形图像。 
        Dim g As Graphics = Me.pictureBox1.CreateGraphics()
        Dim myPen As New Pen(Color.Red)
        myPen.Width = 1
        r = pictureBox1.Width / 30
        pictureBox1.Height = pictureBox1.Width
        For i = 0 To 14
            '竖线
            If i = 0 OrElse i = 14 Then
                myPen.Width = 2
            Else
                myPen.Width = 1
            End If
            g.DrawLine(myPen, r + i * 2 * r, r, r + i * 2 * r, r * 2 * 15 - r - 1)
        Next
        For i = 0 To 14
            '横线
            If i = 0 OrElse i = 14 Then
                myPen.Width = 2
            Else
                myPen.Width = 1
            End If
            g.DrawLine(myPen, r, r + i * 2 * r, r * 2 * 15 - r - 1, r + i * 2 * r)
        Next
        Dim myBrush As New SolidBrush(Color.Yellow)
        '画4个天星
        g.FillEllipse(myBrush, r + 3 * r * 2 - 4, r + 3 * r * 2 - 4, 8, 8)
        g.FillEllipse(myBrush, r + 3 * r * 2 - 4, r + 11 * r * 2 - 4, 8, 8)
        g.FillEllipse(myBrush, r + 11 * r * 2 - 4, r + 11 * r * 2 - 4, 8, 8)
        g.FillEllipse(myBrush, r + 11 * r * 2 - 4, r + 3 * r * 2 - 4, 8, 8)
    End Sub
    '窗体上鼠标按下的事件中根据鼠标在pictureBox1内的像素坐标(e.X，e.Y)，转换成棋盘坐标p，调用Draw(g, p, mplayer)方法在p坐标点上绘制指定mplayer颜色的棋子。最后调用isWin()判断落子后是否赢了此局。
    Private Sub pictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        '(e.X，e.Y)为鼠标在pictureBox1内的像素坐标
        Dim g As Graphics = Me.PictureBox1.CreateGraphics()
        Dim p As New Point((e.X - r / 2 + 1) / (2 * r), (e.Y - r / 2 + 1) / (2 * r))
        If p.X < 0 OrElse p.Y < 0 OrElse p.X > 15 OrElse p.Y > 15 Then
            MessageBox.Show("超边界了")
            Return
        End If
        Label1.Text = p.X.ToString() & "|" & p.Y.ToString() & "|" & e.X.ToString() & "|" & e.Y.ToString()
        If Box(p.X, p.Y) <> Chess.none Then
            MessageBox.Show("已有棋子了")
            Return
        End If
        Draw(g, p, mplayer)
        Box(p.X, p.Y) = mplayer
        If isWin() = True Then      '判断输赢否
            MessageBox.Show(mplayer.ToString() & "赢了此局！")
            Button1.Enabled = True
            Return
        End If
        reverseRole()   '转换角色
    End Sub
    'Draw(g As Graphics, p2 As Point, mplayer As Chess)方法在p2坐标点上绘制指定的棋子mplayer。
    Private Sub Draw(ByVal g As Graphics, ByVal p2 As Point, ByVal mplayer As Chess)
        Dim myBrush As SolidBrush
        If mplayer = Chess.Black Then
            myBrush = New SolidBrush(Color.Black)
        Else
            myBrush = New SolidBrush(Color.White)
        End If
        g.FillEllipse(myBrush, p2.X * 2 * r, p2.Y * 2 * r, 2 * r, 2 * r)
    End Sub
    'reverseRole()转换用户的角色，主要是提示该那方走棋。
    Private Sub reverseRole()
        If mplayer = Chess.Black Then
            mplayer = Chess.White
            label2.Text = "你是白方,请走棋"
        Else
            mplayer = Chess.Black
            label2.Text = "你是黑方，请走棋"
        End If
    End Sub
    '窗体上“重新开始”命令按钮控件单击事件代码：
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        PictureBox1.Refresh()
        DrawBoard()                          '绘制棋盘
        For i As Integer = 0 To 14      '清空棋子信息
            For j As Integer = 0 To 14
                Box(i, j) = Chess.none
            Next
        Next
        mplayer = Chess.Black           '假设持黑棋
        label2.Text = "你是黑方，请走棋"
    End Sub
    'isWin()扫描整个棋盘，判断是否形成五子连珠。
    Private Function isWin() As Boolean
        Dim a As Chess = mplayer
        Dim i As Integer, j As Integer
        For i = 0 To 10     '判断X= Y 轴上是否形成五子连珠
            For j = 0 To 10
                If Box(i, j) = a AndAlso Box(i + 1, j + 1) = a AndAlso Box(i + 2, j + 2) = a AndAlso Box(i + 3, j + 3) = a AndAlso Box(i + 4, j + 4) = a Then
                    Return True
                End If
            Next
        Next
        For i = 4 To 14     '判断X= -Y 轴上是否形成五子连珠
            For j = 0 To 10
                If Box(i, j) = a AndAlso Box(i - 1, j + 1) = a AndAlso Box(i - 2, j + 2) = a AndAlso Box(i - 3, j + 3) = a AndAlso Box(i - 4, j + 4) = a Then
                    Return True
                End If
            Next
        Next
        For i = 0 To 14     '判断Y轴上是否形成五子连珠
            For j = 4 To 14
                If Box(i, j) = a AndAlso Box(i, j - 1) = a AndAlso Box(i, j - 2) = a AndAlso Box(i, j - 3) = a AndAlso Box(i, j - 4) = a Then
                    Return True
                End If
            Next
        Next
        For i = 0 To 10     '判断X轴上是否形成五子连珠
            For j = 0 To 14
                If Box(i, j) = a AndAlso Box(i + 1, j) = a AndAlso Box(i + 2, j) = a AndAlso Box(i + 3, j) = a AndAlso Box(i + 4, j) = a Then
                    Return True
                End If
            Next
        Next
        Return False
    End Function

End Class
