开发者

Help with my simple winforms game

开发者 https://www.devze.com 2023-02-28 16:14 出处:网络
So, I am trying to emulate the minesweeper game in winforms, just as an exercise. So far I have two classes, one called \"Cell\" which derives from regular button class, but has few properties of its

So, I am trying to emulate the minesweeper game in winforms, just as an exercise. So far I have two classes, one called "Cell" which derives from regular button class, but has few properties of its own and a class that handles the logic (basically I made a two-dimensional array filled with objects of type "Cell" and populate it with bombs). I ran into a problem - how do I attach my array of "Cell" button controls to the form? Without re-writing everything from scratch? Both classes are unfinished obviously, it's just that I wanted to check how it would look on a form and realized that I am stuck.

Here is my Cell class

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();

    }
}

And here is my array class

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

This issue can be solved similar to this one:

Using programatically created CheckBoxes in Windows Form [C#]

The idea is to use YourForm.Controls.Add(...). Don't forget to provide proper locations for your cells/buttons within the forms coordinate system.

Of course, I would like to mention that IMHO deriving Cell from Button is a horrible design decision. Better separate your data classes (like Cell) completely from the GUI classes (like Button) and choose a technique like the one Asher suggested in his first answer (add a cell to the Tag property of each Button) to create a connection between Cell objects and corresponding Button objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消