如何使用 Visual Basic 创建 grid/game 板(windows 形式)

How to create grid/game board using Visual Basic(windows form)

我目前正在进行一个项目,我正在创建一个名为 go 的游戏。这是一种领土棋盘游戏,尽管规则简单,但很难掌握。如果您想阅读更多相关信息,请单击此处:https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjbjOTh_tfXAhVjIcAKHQuxCEgQFggoMAA&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGo_(game)&usg=AOvVaw3jJDO24LghMzmB1WUxB2t_

我正在使用 Visual Basic 编写此游戏,更具体地说,使用 vb,而不是 C# 或任何其他语言,使用 windows 形式作为面向对象的游戏。

我在网上搜索了如何对这个板进行编程,但找不到任何对我来说简单易懂的东西(我的编程技能不是太高)。主要问题是我需要通过单击空白区域来记录用户在板上放置棋子或石头的位置,因此使用 2D 数组。然而,石头被放置在网格的交叉点,而不是在线之间的空间。

我找不到任何尝试与此类似的人的例子。只能想到creating/using一张棋盘的图片,导入到windows表格中,点击时叠加一个不可见的二维数组,点击的地方会出现一个black/white石头。这是我的另一个问题;如何在点击的表格中显示作品。

我很感激任何建议。

这是一些使用面板 (Panel1) 作为面板的起始代码。多次点击某个位置可切换该位置的状态:

Public Class Form1

    Private Const GridSize As Integer = 9 ' small = 9, medium = 13, large = 19
    Private Grid As New List(Of List(Of Boolean?))() ' nullable boolean (black:true, white:false, blank:nothing)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Panel1.BackColor = Color.Tan

        ' initialize the grid to all blanks
        For col As Integer = 1 To GridSize
            Dim column As New List(Of Boolean?)
            For row As Integer = 1 To GridSize
                column.Add(Nothing)
            Next
            Grid.Add(column)
        Next
    End Sub

    Private Sub Panel1_SizeChanged(sender As Object, e As EventArgs) Handles Panel1.SizeChanged
        Dim pnl As Panel = DirectCast(sender, Panel)
        pnl.Invalidate() ' redraw the board whenever it gets resized
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
        Dim pnl As Panel = DirectCast(sender, Panel)

        Dim p As Decimal
        Dim x, y As Integer
        Dim margin As Integer

        ' draw the vertical lines:
        margin = (1 / (GridSize + 1)) * pnl.Size.Height
        For col As Integer = 1 To GridSize
            p = col / (GridSize + 1)
            x = p * pnl.Size.Width
            e.Graphics.DrawLine(Pens.Black, x, margin, x, pnl.Size.Height - margin)
        Next

        ' draw the horizontal lines:
        margin = (1 / (GridSize + 1)) * pnl.Size.Width
        For row As Integer = 1 To GridSize
            p = row / (GridSize + 1)
            y = p * pnl.Size.Height
            e.Graphics.DrawLine(Pens.Black, margin, y, pnl.Size.Width - margin, y)
        Next

        ' draw the pieces:
        For x = 0 To GridSize - 1
            Dim column As List(Of Boolean?) = Grid(x)
            For y = 0 To GridSize - 1
                If Grid(x)(y).HasValue Then
                    Dim clr As Color = If(Grid(x)(y), Color.Black, Color.White)
                    Dim pt As New Point((x + 1) / (GridSize + 1) * pnl.Size.Width, (y + 1) / (GridSize + 1) * pnl.Size.Height)
                    Dim rc As New Rectangle(pt, New Size(1, 1))
                    rc.Inflate((1 / (GridSize + 1)) * pnl.Size.Width / 2, (1 / (GridSize + 1)) * pnl.Size.Height / 2)
                    Using brsh As New SolidBrush(clr)
                        e.Graphics.FillEllipse(brsh, rc)
                    End Using
                End If
            Next
        Next
    End Sub

    Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
        Dim pnl As Panel = DirectCast(sender, Panel)

        ' figure out where the user clicked: min = 0, max = (gridsize -1)
        Dim pt As Point = pnl.PointToClient(Cursor.Position)
        Dim colWidth As Integer = (1 / (GridSize + 1)) * pnl.Size.Width
        Dim rowHeight As Integer = (1 / (GridSize + 1)) * pnl.Size.Height
        Dim gridPosition As New Point(Math.Min(Math.Max((pt.X / colWidth) - 1, 0), GridSize - 1), Math.Min(Math.Max((pt.Y / rowHeight) - 1, 0), GridSize - 1))

        ' now do something with gridPosition: (here we just toggle between black:true, white:false and blank:nothing)
        If Not Grid(gridPosition.X)(gridPosition.Y).HasValue Then
            Grid(gridPosition.X)(gridPosition.Y) = True
        ElseIf Grid(gridPosition.X)(gridPosition.Y) = True Then
            Grid(gridPosition.X)(gridPosition.Y) = False
        Else
            Grid(gridPosition.X)(gridPosition.Y) = Nothing
        End If
        pnl.Invalidate() ' force the board to redraw itself
    End Sub

End Class

示例截图: