65.9K
CodeProject 正在变化。 阅读更多。
Home

Dot 狂热(游戏教程)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (5投票s)

2017年8月16日

CPOL

6分钟阅读

viewsIcon

8191

downloadIcon

470

简单的游戏教程。和家人朋友一起玩 Dots。

引言

Dots 是我们将要创建的另一个著名游戏。规则很简单,你需要围住对手的点。我高中时经常玩 Dots,主要是在宗教课上 :P。我对这个游戏有很深的感情,所以当我学会编程时,我真的想做这个游戏。经过几次不成功的尝试,我有点放弃了,但又回到了这个项目并完成了它。这个教程旨在介绍 Dots 游戏的核心元素——围住点。

我还制作了一个可下载的游戏,你可以在局域网与朋友一起玩,如果有人可以进行端口转发,可以通过互联网玩,或者在电脑前与朋友一起玩,或者与电脑玩(创建的电脑 AI 是简单级别)。下面展示的游戏并非我的广告。下面展示的游戏说明了如何使用代码,本教程仅展示游戏引擎,游戏的精髓。如果你有任何问题,例如“为什么我的面板发光而你的游戏面板不发光”或“你是怎么做到的……”,请尽管问,我会回答你。当我准备教程时,我总是想为你创造一个测试技能的机会,给你一个挑战。尝试基于这段代码创建你自己的游戏,你就会看到你的技能会提升。:  

使用代码

本教程中将演示的窗体相当简单

 

    

元素类型元素名称属性
表单Form1

名称:Form1

文本:Dots mania

尺寸:840; 663

PanelMain_board

名称:Main_board

尺寸:800; 600

位置:12;12

背景颜色:白色

锚点:顶部、底部、左侧、右侧

在本教程中,游戏将在窗体加载时开始,我们不会构建不必要的按钮和装饰,以免模糊游戏的核心引擎。我们的游戏将看起来像下面的 gif。

任何理解围住点方法的人,都能够添加他想要的额外元素。我们创建窗体加载事件。

Public Class Form1
   'Main board size
    Dim bHeight As Integer = 15
    Dim bWidth As Integer = 20
    Dim PLocationList As New List(Of List(Of Panel)) 'all panels on the board list


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = -2 To bWidth + 1
            Dim PanList As New List(Of Panel)
            For j As Integer = -2 To bHeight + 1
                Dim MainPanel As New Panel
                With MainPanel
                    .Location = New Point(i * 40 + 30, j * 40 + 30)
                    .Size = New Size(20, 20)
                    .BackColor = Color.Transparent
                    .Name = "panL_" + i.ToString + "^" + j.ToString + "^"
                    .Cursor = Cursors.Hand
                    'AddHandler .Click, AddressOf pan_Click
                    'Cross out the board, explain on the tutorial
                    If i = -2 Or i = -1 Or j = -1 Or j = -2 Or i = bWidth Or j = bHeight Or i = bWidth + 1 Or j = bHeight + 1 Then
                        .Enabled = False
                    End If
                End With
                Main_board.Controls.Add(MainPanel) ' Add items to the panel
                PanList.Add(MainPanel)
            Next
            'The variable stores all board panels
            PLocationList.Add(PanList)
        Next
        'generate board grid
        Dim bit As Bitmap = New Bitmap(Main_board.Width, Main_board.Height)
        Dim g As Graphics = Graphics.FromImage(bit)

        Dim myPen As Pen = New Pen(Color.FromArgb(191, 218, 229), 1)
        For i As Integer = 40 To Main_board.Height Step 40
            g.DrawLine(myPen, 0, i, Main_board.Width, i)
        Next
        For i As Integer = 40 To Main_board.Width Step 40
            g.DrawLine(myPen, i, 0, i, Main_board.Height)
        Next
        Main_board.BackgroundImage = bit
    End Sub
End Class

启动程序后,我们应该看到一个准备好的 Dots 页面,你可能会想把它改成更像笔记本页面的样子。我们应该注意的一个重要元素是将我们的面板放置在纸张区域之外。因为在程序运行期间,元素将以逻辑方式处理,这个过程需要检查点上方、下方、左侧和右侧。添加用户一开始无法访问的额外面板,比设置限制更方便。
如果我们给这些区域着色,它们看起来会像图片所示。

这些区域将被视为空白区域,并阻止它们,以确保它们保持空白。

现在是时候解锁了

AddHandler .Click, AddressOf pan_Click

在此处添加代码

    'Current player
    Dim Player As Boolean = True
    'dots list
    Dim Player1DotsL As New List(Of Panel)
    Dim Player2DotsL As New List(Of Panel)
    Dim bit As Bitmap
    Dim g As Graphics
    'Colors
    Dim pGreen As New System.Drawing.SolidBrush(System.Drawing.Color.Green)
    Dim pRed As New System.Drawing.SolidBrush(Color.Red)

    Public Sub pan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Player Then 'green
            bit = New Bitmap(20, 20)
            g = Graphics.FromImage(bit)
            'Added a checked panel to the list
            Player1DotsL.Add(DirectCast(sender, Panel))
            g.FillEllipse(pGreen, New Rectangle(5, 5, 10, 10))
            DirectCast(sender, Panel).BackgroundImage = bit
            DirectCast(sender, Panel).Enabled = False
            'LapTest()
        Else 'red
            bit = New Bitmap(20, 20)
            g = Graphics.FromImage(bit)
            'Added a checked panel to the list
            Player2DotsL.Add(DirectCast(sender, Panel))
            g.FillEllipse(pRed, New Rectangle(5, 5, 10, 10))
            DirectCast(sender, Panel).BackgroundImage = bit
            DirectCast(sender, Panel).Enabled = False
            'LapTest()
        End If

    End Sub
    End Sub

现在我们可以放置点。一个颜色,因为玩家不会将状态改为 false,而会在另一个循环中这样做。因为游戏规则要求如此。当玩家围住对手时,他可以放置一个额外的点。

至于简单的事情,就到此为止了。现在你需要理解我的意思,解释一些事情是如何运作的。游戏中最难的部分是确定点是否被围住。当只有一个点时,情况很明显。检查点的所有侧面是否被对手的点包围。不幸的是,这是我们必须考虑的多种情况之一。我将尝试解释是什么以及如何做,如果有人不明白,他可以给我发邮件或留言。

解锁代码

LapTest()

此元素的代码与第一个函数一起看起来像这样。

  Private Sub LapTest()
        Dim EdgeList As New List(Of Panel)
        'Adds elements adjacent to dots to the list
        If Not Player1DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player1DotsL))
        End If
        If Not Player2DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player2DotsL))
        End If

    End Sub

     'The function returns elements adjacent to dots
    Private Function Borders(ByRef PlayerList As List(Of Panel)) As List(Of Panel)
        Dim BorderList As New List(Of Panel) 'hold adjacent items
        For k As Integer = 0 To PlayerList.Count - 1

            Dim positionX As Integer = 0
            Dim positionY As Integer = 0
            'Get actual panel position
            For i As Integer = 0 To PLocationList.Count - 1
                For j As Integer = 0 To PLocationList(i).Count - 1
                    If PLocationList(i)(j).Name = PlayerList(k).Name Then
                        positionX = i
                        positionY = j
                    End If
                Next
            Next
            'Check that there are empty elements from left, right, top and bottom
            If Not BorderList.Contains(PLocationList(positionX - 1)(positionY)) And
                Not Player1DotsL.Contains(PLocationList(positionX - 1)(positionY)) And
                Not Player2DotsL.Contains(PLocationList(positionX - 1)(positionY)) Then
                BorderList.Add(PLocationList(positionX - 1)(positionY))
            End If
            If Not BorderList.Contains(PLocationList(positionX + 1)(positionY)) And
                Not Player1DotsL.Contains(PLocationList(positionX + 1)(positionY)) And
                Not Player2DotsL.Contains(PLocationList(positionX + 1)(positionY)) Then
                BorderList.Add(PLocationList(positionX + 1)(positionY))
            End If
            If Not BorderList.Contains(PLocationList(positionX)(positionY + 1)) And
                Not Player1DotsL.Contains(PLocationList(positionX)(positionY + 1)) And
                Not Player2DotsL.Contains(PLocationList(positionX)(positionY + 1)) Then
                BorderList.Add(PLocationList(positionX)(positionY + 1))
            End If
            If Not BorderList.Contains(PLocationList(positionX)(positionY - 1)) And
                Not Player1DotsL.Contains(PLocationList(positionX)(positionY - 1)) And
                Not Player2DotsL.Contains(PLocationList(positionX)(positionY - 1)) Then
                BorderList.Add(PLocationList(positionX)(positionY - 1))
            End If
        Next
        Return BorderList
    End Function

上述函数中包含的方法返回与点相邻的元素列表。

           

在图片中,我们看到了着色的相邻元素。下一步是创建特定元素的列表。我称这个列表为“freedomPoints”。在围住检查过程中,任何元素或组与此列表的碰撞都意味着点未被围住。

    Private Sub LapTest()
        Dim EdgeList As New List(Of Panel)
        'Adds elements adjacent to dots to the list
        If Not Player1DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player1DotsL))
        End If
        If Not Player2DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player2DotsL))
        End If


        Dim FreedomList As New List(Of List(Of Panel))
        FreedomList = freedomPoints(EdgeList)

    End Sub

      'Points of freedom
    Private Function freedomPoints(ByRef adjacent As List(Of Panel)) As List(Of List(Of Panel))
        Dim PanelsList As New List(Of List(Of Panel))

        Dim ListLength As Integer = PLocationList(0).Count
        Dim ListSize As Integer = PLocationList.Count
        'Adding elements from bottom
        For i As Integer = ListSize - 1 To 0 Step -1
            Dim lista As New List(Of Panel)
            For j As Integer = ListLength - 1 To 0 Step -1
                If Not adjacent.Contains(PLocationList(i)(j)) Then
                    lista.Add(PLocationList(i)(j))
                Else
                    Exit For
                End If

            Next
            PanelsList.Add(lista)
        Next
        'Adding elements from above
        For i As Integer = 0 To ListSize - 1
            Dim lista As New List(Of Panel)
            For j As Integer = 0 To ListLength - 1
                If Not adjacent.Contains(PLocationList(i)(j)) Then
                    lista.Add(PLocationList(i)(j))
                Else
                    Exit For
                End If

            Next
            PanelsList.Add(lista)
        Next
        'Adding elements from right side
        For i As Integer = ListLength - 1 To 0 Step -1
            Dim lista As New List(Of Panel)
            For j As Integer = ListSize - 1 To 0 Step -1
                If Not adjacent.Contains(PLocationList(j)(i)) Then
                    lista.Add(PLocationList(j)(i))
                Else
                    Exit For
                End If
            Next
            PanelsList.Add(lista)
            If i = ListSize Then
                Exit For
            End If
        Next
        'Adding elements from left side
        For i As Integer = 0 To ListLength - 1
            Dim lista As New List(Of Panel)
            For j As Integer = 0 To ListSize - 1
                If Not adjacent.Contains(PLocationList(j)(i)) Then
                    lista.Add(PLocationList(j)(i))
                Else
                    Exit For
                End If
            Next
            PanelsList.Add(lista)
            If i = ListSize Then
                Exit For
            End If
        Next
        Return PanelsList
    End Function

此函数返回一个包含从所有侧面检查过的元素的列表。

  

正如你在图片中看到的,与点相邻的元素未添加到列表中,因为它们存储在另一个列表(BorderList)中。当我们有更多项目时,此函数如何工作?嗯,就像下面的图片一样。

紫色和蓝色元素是“freedomPoints”函数的结果,红色是“Borders”的效果。看看中间被绿色点包围的元素,它们没有颜色,它们对我们摆脱围困非常重要。我们现在将开始一个非常重要的章节,我们将对我们的点进行分组。在这里,玩家将扮演一个重要角色,我们必须知道哪个玩家刚刚放置了一个点。这样的知识来自变量“Player”。当放置了一个绿点时,我们不考虑绿点,我们考虑红点。由于所有红点玩家的点都分组在一起,我们检查每个组是否会突破到我们的“FreedomList”,如果没有突破,则意味着它被围住了。预分组相当容易。我们创建相邻元素的组,将它们添加到列表中,然后合并包含相同元素的列表。

(生成的面板列表 - Generated panel list)

 让我们看看方法代码看起来是什么样的。

    Private Sub LapTest()
        Dim EdgeList As New List(Of Panel)
        'Adds elements adjacent to dots to the list
        If Not Player1DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player1DotsL))
        End If
        If Not Player2DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player2DotsL))
        End If


        Dim FreedomList As New List(Of List(Of Panel))
        FreedomList = freedomPoints(EdgeList)

        Dim GroupList As New List(Of List(Of Panel))

        If Player Then 'Green
            GroupList = Group(Player2DotsL)
        Else 'Red
            GroupList = Group(Player1DotsL)
        End If

    End Sub

     'This function creates an initial group of dots
    Private Function Group(ByRef PlayerList As List(Of Panel)) As List(Of List(Of Panel))
        Dim Groups As New List(Of List(Of Panel))
        'Enumerates all opponent dots
        For k As Integer = 0 To PlayerList.Count - 1
            Dim _continue As Boolean = True 'Block, does not add dots if already in some group
            For i As Integer = 0 To Groups.Count - 1
                If Groups(i).Contains(PlayerList(k)) Then
                    _continue = False
                    Exit For
                End If
            Next
            If _continue = True Then
                Dim PointsList As New List(Of Panel)
                PointsList.Add(PlayerList(k))
                'Get dot position
                Dim positionX As Integer = 0
                Dim positionY As Integer = 0
                For i As Integer = 0 To PLocationList.Count - 1
                    For j As Integer = 0 To PLocationList(i).Count - 1
                        If PLocationList(i)(j).Name = PlayerList(k).Name Then
                            positionX = i
                            positionY = j
                        End If
                    Next
                Next
                'Connects adjacent points to a group
                If Not PointsList.Contains(PLocationList(positionX - 1)(positionY)) And
                    PlayerList.Contains(PLocationList(positionX - 1)(positionY)) Then
                    PointsList.Add(PLocationList(positionX - 1)(positionY))
                End If
                If Not PointsList.Contains(PLocationList(positionX + 1)(positionY)) And
                    PlayerList.Contains(PLocationList(positionX + 1)(positionY)) Then
                    PointsList.Add(PLocationList(positionX + 1)(positionY))
                End If
                If Not PointsList.Contains(PLocationList(positionX)(positionY + 1)) And
                    PlayerList.Contains(PLocationList(positionX)(positionY + 1)) Then
                    PointsList.Add(PLocationList(positionX)(positionY + 1))
                End If
                If Not PointsList.Contains(PLocationList(positionX)(positionY - 1)) And
                    PlayerList.Contains(PLocationList(positionX)(positionY - 1)) Then
                    PointsList.Add(PLocationList(positionX)(positionY - 1))
                End If
                'Adds points to our list
                Groups.Add(PointsList)
            End If
        Next

        For i As Integer = 0 To Groups.Count - 1
            For j As Integer = 0 To Groups(i).Count - 1
                Dim PFGroup As Panel = Groups(i)(j)
                For r As Integer = 0 To Groups.Count - 1
                    If Not r = i And Groups(r).Contains(PFGroup) Then
                        'Merge lists
                        Groups(i).AddRange(Groups(r))
                        Groups(r).Clear()
                    End If
                Next
            Next
        Next
        'Removes empty lists
        For i As Integer = Groups.Count - 1 To 0 Step -1
            If Groups(i).Count = 0 Then
                Groups.RemoveAt(i)
            End If
        Next
        Return Groups
    End Function

最简单的例子是四个点的例子。

暂时,我们不必担心这些列表中的重复元素。这种分类,当我们添加颜色时,看起来是这样的。

(组 - Group)

正如我们所见,B 组暂时不计算其组的空白字段,因此我们不知道我们的 B 组是否确定被围住了。最后一步是将空白或边界字段添加到我们的组,并从列表中删除与我们的自由列表合并的组。

    Private Sub LapTest()
        Dim EdgeList As New List(Of Panel)
        'Adds elements adjacent to dots to the list
        If Not Player1DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player1DotsL))
        End If
        If Not Player2DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player2DotsL))
        End If


        Dim FreedomList As New List(Of List(Of Panel))
        FreedomList = freedomPoints(EdgeList)

        Dim GroupList As New List(Of List(Of Panel))

        If Player Then 'Green
            GroupList = Group(Player2DotsL)
            GroupList = GroupEdition(Player1DotsL, GroupList, EdgeList, FreedomList)
        Else 'Red
            GroupList = Group(Player1DotsL)
            GroupList = GroupEdition(Player2DotsL, GroupList, EdgeList, FreedomList)
        End If

    End Sub

    Private Function GroupEdition(ByRef EnemyList As List(Of Panel), ByRef Groups As List(Of List(Of Panel)),
                                  ByRef EdgeList As List(Of Panel), ByRef ZoneList As List(Of List(Of Panel))) As _
                                  List(Of List(Of Panel))
        'The loop checks each item and adds them to the groups
        Do
            Dim StartLoop As Boolean = False
            For k As Integer = Groups.Count - 1 To 0 Step -1
                For l As Integer = 0 To Groups(k).Count - 1
                    Dim positionX As Integer = 0
                    Dim positionY As Integer = 0
                    'Get the current position of our panel
                    For i As Integer = 0 To PLocationList.Count - 1
                        For j As Integer = 0 To PLocationList(i).Count - 1
                            If PLocationList(i)(j).Name = Groups(k)(l).Name Then
                                positionX = i
                                positionY = j
                            End If
                        Next
                    Next
                    Dim _continue As Boolean = True
                    For i As Integer = 0 To ZoneList.Count - 1
                        'The Group element touches the list of freedom items, which indicates that it is not encircled
                        If ZoneList(i).Contains(PLocationList(positionX - 1)(positionY)) Or
                            ZoneList(i).Contains(PLocationList(positionX + 1)(positionY)) Or
                            ZoneList(i).Contains(PLocationList(positionX)(positionY - 1)) Or
                            ZoneList(i).Contains(PLocationList(positionX)(positionY + 1)) Then
                            _continue = False
                            'Remove list from Groups
                            Groups.RemoveAt(k)
                            Exit For
                        End If
                    Next

                    If _continue = True Then
                        'Adds border lists
                        If EdgeList.Contains(PLocationList(positionX - 1)(positionY)) And
                            Not Groups(k).Contains(PLocationList(positionX - 1)(positionY)) Then
                            Groups(k).Add(PLocationList(positionX - 1)(positionY))
                            StartLoop = True
                            'It adds its own elements and does not add opponent elements
                        ElseIf Not EnemyList.Contains(PLocationList(positionX - 1)(positionY)) And
                            Not Groups(k).Contains(PLocationList(positionX - 1)(positionY)) Then
                            Groups(k).Add(PLocationList(positionX - 1)(positionY))
                            StartLoop = True
                        End If

                        If EdgeList.Contains(PLocationList(positionX + 1)(positionY)) And
                            Not Groups(k).Contains(PLocationList(positionX + 1)(positionY)) Then
                            Groups(k).Add(PLocationList(positionX + 1)(positionY))
                            StartLoop = True
                        ElseIf Not EnemyList.Contains(PLocationList(positionX + 1)(positionY)) And
                            Not Groups(k).Contains(PLocationList(positionX + 1)(positionY)) Then
                            Groups(k).Add(PLocationList(positionX + 1)(positionY))
                            StartLoop = True
                        End If

                        If EdgeList.Contains(PLocationList(positionX)(positionY + 1)) And
                            Not Groups(k).Contains(PLocationList(positionX)(positionY + 1)) Then
                            Groups(k).Add(PLocationList(positionX)(positionY + 1))
                            StartLoop = True
                        ElseIf Not EnemyList.Contains(PLocationList(positionX)(positionY + 1)) And
                            Not Groups(k).Contains(PLocationList(positionX)(positionY + 1)) Then
                            Groups(k).Add(PLocationList(positionX)(positionY + 1))
                            StartLoop = True
                        End If

                        If EdgeList.Contains(PLocationList(positionX)(positionY - 1)) And
                            Not Groups(k).Contains(PLocationList(positionX)(positionY - 1)) Then
                            Groups(k).Add(PLocationList(positionX)(positionY - 1))
                            StartLoop = True
                        ElseIf Not EnemyList.Contains(PLocationList(positionX)(positionY - 1)) And
                            Not Groups(k).Contains(PLocationList(positionX)(positionY - 1)) Then
                            Groups(k).Add(PLocationList(positionX)(positionY - 1))
                            StartLoop = True
                        End If
                    Else
                        Exit For
                    End If
                Next
            Next
            'The loop works until all elements are checked or no longer in the group
            If Groups.Count = 0 Then
                Exit Do
            ElseIf StartLoop = False Then
                Exit Do
            End If
        Loop
        Return Groups
    End Function

此函数将允许我们消除开放组并保留封闭组,或者在所有组都开放时保留空组。

(组被围住 - Group is encircled)

 当我们知道哪个组被围住时,它应该被圆圈标记并从游戏中移除,以便对手无法再使用它们。最后一件事我们可以很容易地通过将被围住的元素添加到“Player1CheckedList”或“Player2CheckedList”来实现,这样对于一个玩家来说,点将被消除,而对于另一个玩家来说,由于它们被围住(空白字段)而无用。基于这些列表,我们还可以告诉你哪个玩家赢得了比赛,如果我们谈论的是面积占有方面的胜利。让我们继续进行绘线元素,看到上面的图片,很容易猜测我们是如何做到的。我们将检查每个被圆圈标记的元素,或者它是否靠近对手的点,并创建一个包含这些点的列表。然后,从列表中,我们将删除重复的元素,使它们成为单个元素,我们将查看与它们相邻的元素,并将创建一个元组列表来存储线条坐标。

    Dim Player1CheckedList As New List(Of Panel)
    Dim Player2CheckedList As New List(Of Panel)

    Private Sub LapTest()
        Dim EdgeList As New List(Of Panel)
        'Adds elements adjacent to dots to the list
        If Not Player1DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player1DotsL))
        End If
        If Not Player2DotsL.Count = 0 Then
            EdgeList.AddRange(Borders(Player2DotsL))
        End If


        Dim FreedomList As New List(Of List(Of Panel))
        FreedomList = freedomPoints(EdgeList)

        Dim GroupList As New List(Of List(Of Panel))

        If Player Then 'Green
            GroupList = Group(Player2DotsL)
            GroupList = GroupEdition(Player1DotsL, GroupList, EdgeList, FreedomList)
            'List all elements in groups that remain
            For i As Integer = 0 To GroupList.Count - 1
                'Draws lines and creates a list of tuples
                DrawLines(GroupList(i), Player1DotsL, Player1CheckedList)
                For j As Integer = 0 To GroupList(i).Count - 1
                    'Elements can repeat, so the loop checks if they are no longer listed
                    If Not Player1CheckedList.Contains(GroupList(i)(j)) Then
                        Player1CheckedList.Add(GroupList(i)(j)) 'dodaje element
                        GroupList(i)(j).Enabled = False
                        'The condition removes an item from the opponents list so it can't longer be used
                        If Not Player2DotsL.IndexOf(GroupList(i)(j)) = -1 Then
                            Player2DotsL.RemoveAt(Player2DotsL.IndexOf(GroupList(i)(j)))
                            Player1DotsL.Add(GroupList(i)(j)) 'Adds current player to the list
                        End If
                    End If
                Next
            Next
            'Change player if nothing is encircled
            If Not GroupList.Count > 0 Then
                Player = False
            End If
        Else 'Red
            GroupList = Group(Player1DotsL)
            GroupList = GroupEdition(Player2DotsL, GroupList, EdgeList, FreedomList)
            'List all elements in groups that remain
            For i As Integer = 0 To GroupList.Count - 1
                'Draws lines and creates a list of tuples
                DrawLines(GroupList(i), Player2DotsL, Player2CheckedList)
                For j As Integer = 0 To GroupList(i).Count - 1
                    'Elements can repeat, so the loop checks if they are no longer listed
                    If Not Player2CheckedList.Contains(GroupList(i)(j)) Then
                        Player2CheckedList.Add(GroupList(i)(j)) 'dodaje element
                        GroupList(i)(j).Enabled = False
                        'The condition removes an item from the opponents list so it can't longer be used
                        If Not Player1DotsL.IndexOf(GroupList(i)(j)) = -1 Then
                            Player1DotsL.RemoveAt(Player1DotsL.IndexOf(GroupList(i)(j))) 'remove
                            Player2DotsL.Add(GroupList(i)(j)) 'Adds current player to the list
                        End If
                    End If
                Next
            Next
            'Change player if nothing is encircled
            If Not GroupList.Count > 0 Then
                Player = True
            End If
        End If
    End Sub

这个修改过的 LapTest 将验证你需要的内容,将其添加到你的代码中。现在我们将添加“DrawLines”的最后一个元素,它将绘制我们的点。它看起来是这样的。

    Private Sub DrawLines(ByRef pane As List(Of Panel), ByRef PlayerList As List(Of Panel), ByRef cList As List(Of Panel))
        Dim AllLinesList As New List(Of Tuple(Of Panel, Panel))
        Dim d As Graphics
        d = Main_board.CreateGraphics()

        Dim OrderedPanelsList As New List(Of Panel)

        'Creates a list of all group panels without repetition
        For i As Integer = 0 To pane.Count - 1
            If Not OrderedPanelsList.Contains(pane(i)) Then
                OrderedPanelsList.Add(pane(i))
            End If
        Next

        Dim adjacentPanList As New List(Of Panel) 'List of adjacent panels
        For k As Integer = 0 To OrderedPanelsList.Count - 1

            Dim positionX As Integer = 0
            Dim positionY As Integer = 0
            For i As Integer = 0 To PLocationList.Count - 1
                For j As Integer = 0 To PLocationList(i).Count - 1
                    If PLocationList(i)(j).Name = OrderedPanelsList(k).Name Then
                        positionX = i
                        positionY = j
                    End If
                Next
            Next
            If PlayerList.Contains(PLocationList(positionX - 1)(positionY)) Then
                adjacentPanList.Add(PLocationList(positionX - 1)(positionY))
            End If
            If PlayerList.Contains(PLocationList(positionX + 1)(positionY)) Then
                adjacentPanList.Add(PLocationList(positionX + 1)(positionY))
            End If
            If PlayerList.Contains(PLocationList(positionX)(positionY + 1)) Then
                adjacentPanList.Add(PLocationList(positionX)(positionY + 1))
            End If
            If PlayerList.Contains(PLocationList(positionX)(positionY - 1)) Then
                adjacentPanList.Add(PLocationList(positionX)(positionY - 1))
            End If
        Next

        For k As Integer = 0 To adjacentPanList.Count - 1
            If Not cList.Contains(adjacentPanList(k)) Then
                Dim positionX As Integer = 0
                Dim positionY As Integer = 0
                For i As Integer = 0 To PLocationList.Count - 1
                    For j As Integer = 0 To PLocationList(i).Count - 1
                        If PLocationList(i)(j).Name = adjacentPanList(k).Name Then
                            positionX = i
                            positionY = j
                        End If
                    Next
                Next
                If adjacentPanList.Contains(PLocationList(positionX - 1)(positionY)) And
                    Not cList.Contains(PLocationList(positionX - 1)(positionY)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX - 1)(positionY)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX + 1)(positionY)) And
                    Not cList.Contains(PLocationList(positionX + 1)(positionY)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX + 1)(positionY)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX)(positionY + 1)) And
                    Not cList.Contains(PLocationList(positionX)(positionY + 1)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX)(positionY + 1)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX)(positionY - 1)) And
                    Not cList.Contains(PLocationList(positionX)(positionY - 1)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX)(positionY - 1)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX - 1)(positionY - 1)) And
                    Not cList.Contains(PLocationList(positionX - 1)(positionY - 1)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX - 1)(positionY - 1)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX + 1)(positionY + 1)) And
                    Not cList.Contains(PLocationList(positionX + 1)(positionY + 1)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX + 1)(positionY + 1)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX - 1)(positionY + 1)) And
                    Not cList.Contains(PLocationList(positionX - 1)(positionY + 1)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX - 1)(positionY + 1)))
                End If
                If adjacentPanList.Contains(PLocationList(positionX + 1)(positionY - 1)) And
                    Not cList.Contains(PLocationList(positionX + 1)(positionY - 1)) Then
                    AllLinesList.Add(Tuple.Create(adjacentPanList(k), PLocationList(positionX + 1)(positionY - 1)))
                End If
            End If
        Next

        'We draw circles and lines
        For i As Integer = 0 To AllLinesList.Count - 1
            AllLinesList(i).Item1.Visible = False
            AllLinesList(i).Item2.Visible = False
            Application.DoEvents()
            If Player Then
                Dim myPen As Pen = New Pen(pGreen, 2)
                d.DrawLine(myPen, AllLinesList(i).Item1.Location.X + 10, AllLinesList(i).Item1.Location.Y + 10,
                           AllLinesList(i).Item2.Location.X + 10, AllLinesList(i).Item2.Location.Y + 10)
                d.FillEllipse(pGreen, New Rectangle(AllLinesList(i).Item1.Location.X + 5,
                                                    AllLinesList(i).Item1.Location.Y + 5, 10, 10))
                d.FillEllipse(pGreen, New Rectangle(AllLinesList(i).Item2.Location.X + 5,
                                                    AllLinesList(i).Item2.Location.Y + 5, 10, 10))
            Else
                Dim myPen As Pen = New Pen(pRed, 2)
                d.DrawLine(myPen, AllLinesList(i).Item1.Location.X + 10, AllLinesList(i).Item1.Location.Y + 10,
                           AllLinesList(i).Item2.Location.X + 10, AllLinesList(i).Item2.Location.Y + 10)
                d.FillEllipse(pRed, New Rectangle(AllLinesList(i).Item1.Location.X + 5,
                                                  AllLinesList(i).Item1.Location.Y + 5, 10, 10))
                d.FillEllipse(pRed, New Rectangle(AllLinesList(i).Item2.Location.X + 5,
                                                  AllLinesList(i).Item2.Location.Y + 5, 10, 10))
            End If
        Next

    End Sub

这个循环可能还需要一些工作,但它的假设应该很清楚。

 

© . All rights reserved.