c# - 帮助我的简单 winforms 游戏

标签 c# winforms

所以,我尝试在winforms中模拟扫雷游戏,作为练习。到目前为止,我有两个类,一个叫做“Cell”,它派生自常规按钮类,但它自己的属性很少,还有一个处理逻辑的类(基本上我制作了一个二维数组,其中填充了“Cell”类型的对象并用炸弹填充它)。我遇到了一个问题——如何将我的“单元格”按钮控件数组附加到表单?无需从头开始重写所有内容?这两个类(class)显然都未完成,只是我想检查它在表格上的外观并意识到我被卡住了。

这是我的 Cell 类

class Cell : Button
{
    //private GraphicsPath path;

    const int width = 20;
    const int height = 20;

    public byte Value { get; set; }
    public bool IsBomb { get; set; }

    public Cell()
    {

    }

    public Cell(int x, int y)
    {
        this.Location = new Point(x, y);
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);

        this.Width = width;
        this.Height = height;

    }

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        this.Text = Value.ToString();

    }
}

这是我的数组类

class CellArray
{
    private int _rows = 0;
    private int _columns = 0;
    private int _bombAmount = 0;
    private Random rand;
    private Cell[,] cellMatrix;

    public CellArray(int rows, int columns, int bombAmount)
    {
        _rows = rows;
        _columns = columns;
        _bombAmount = bombAmount;
        populate();
        setBombs();
    }

    private void populate()
    {
        cellMatrix = new Cell[_rows, _columns];

        for (int i = 1; i < _rows; i++)
        {
            for (int j = 1; j < _columns; j++)
            {
                cellMatrix[i, j] = new Cell();
                cellMatrix[i, j].IsBomb = false;

            }
        }
    }

    private void setBombs()
    {
        //*****************************************QUESTIONABLE************************************
        rand = new Random();
        int k = 1;
        while (k < _bombAmount)
        {
        Flag:
            {
                int i = rand.Next(_rows);
                int j = rand.Next(_columns);
                if (cellMatrix[i, j].IsBomb == false)
                    cellMatrix[i, j].IsBomb = true;
                else
                    goto Flag;
            }
        }
        //*****************************************QUESTIONABLE************************************

        for (int i = 1; i < _rows; i++)
        {
            for (int j = 1; j < _columns; j++)
            {
                if (cellMatrix[i - 1, j - 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i - 1, j].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i, j - 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i - 1, j + 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i, j + 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i + 1, j + 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i + 1, j].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i + 1, j - 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
            }
        }

    }

}

最佳答案

how do I attach my array of "Cell" button controls to the form

这个问题可以像这个一样解决:

Using programatically created CheckBoxes in Windows Form [C#]

想法是使用 YourForm.Controls.Add(...)。不要忘记在表单坐标系中为您的单元格/按钮提供正确的位置。

当然,我想提一下,恕我直言,从 Button 派生 Cell 是一个糟糕的设计决策。最好将数据类(如 Cell)与 GUI 类(如 Button)完全分开,并选择 Asher 在他的第一个答案中建议的技术(添加一个单元格到每个 Button 的 Tag 属性)以在 Cell 对象和相应的 Button 对象之间创建连接。

关于c# - 帮助我的简单 winforms 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5772596/

相关文章:

c# - JSON WebMethods 的 SoapExtension 等效项是什么?

c# - 尝试引用用户控件时出现错误 "The name does not exist in the current context"

c# - 表单在 backgroundworker 中不显示为对话框?

C# 图像覆盖另一个图像

c# - 转换为字符串返回null

c# - 你如何使用 MySql 的 IN 子句

c# - 以编程方式更改 DataGridView 行上的只读模式

c# - 为什么对接不能正确适用于 winforms 中的现有组框

c# - 级联线程杀死

c# - RestSharp 下载数据与进度条同步