5fox
5fox

Reputation: 163

C# Form does not refresh

I have a problem with my program (Minesweeper game) this is my Board class (mines board)

class Board
{

    private Cell[,] MinesBoard;
    private int maxrow, maxcol;
    private int numOfMines;
    public bool isLose;
    private List<Point> MinesLocation;
    private Random rnd;
    private List<Point> randomPoint;



    public delegate void OnClickEventHandler(object sender, CellClickEventArgs e);
    public event OnClickEventHandler OnCellClick;

    public int NumOfMines
    {
        get { return numOfMines; }
    }

    public int Column
    {
        get { return maxcol; }
    }

    public int Row
    {
        get { return maxrow; }
    }

    private void GetRandomPoints(int maxr, int maxc)
    {
        Point t = new Point();
        t.X = rnd.Next(0, maxr);
        t.Y = rnd.Next(0, maxc);
        if (!randomPoint.Contains(t))
            randomPoint.Add(t);
    }

    public Board(int _row, int _col, int _mines)
    {
        isLose = false;
        maxrow = _row;
        maxcol = _col;
        numOfMines = _mines;
        rnd = new Random(DateTime.Now.Ticks.GetHashCode());
        randomPoint = new List<Point>();
        MinesBoard = new Cell[_row, _col];
        MinesLocation = new List<Point>();
        for (int i = 0; i < maxrow; ++i)
            for (int j = 0; j < maxcol; ++j)
            {
                MinesBoard[i, j] = new Cell();
                //MinesBoard[i, j].button = new Button();
                //MinesBoard[i, j].IsMine = false;
                //MinesBoard[i, j].IsOpen = false;
                //MinesBoard[i, j].MineAround = 0;
            }
        for (int i = 0; i < numOfMines; ++i)
            GetRandomPoints(maxrow, maxcol);

        foreach (Point p in randomPoint)
        {
            if (MinesBoard[p.X, p.Y].IsMine == false)
            {
                MinesBoard[p.X, p.Y].IsMine = true;
                MinesLocation.Add(p);
            }
        }


    }
public void DrawBoard(Form frm)
    {
        int id = 0;
        for (int i = 0; i < maxrow; ++i)
            for (int j = 0; j < maxcol; ++j)
            {
                frm.Controls.Remove(MinesBoard[i, j].button);
                MinesBoard[i, j].button.Name = "btnCell" + id.ToString();
                ++id;
                MinesBoard[i, j].button.Size = new Size(25, 25);
                MinesBoard[i, j].button.Location = new Point(j * 25, 60 + i * 25);
                //MinesBoard[i, j].button.FlatStyle = FlatStyle.Flat;
                //CellClickEventArgs args = new CellClickEventArgs(new Point(i, j));
                MinesBoard[i, j].button.Click += new System.EventHandler(CellClick);
                MinesBoard[i, j].button.MouseDown += new MouseEventHandler(CellMouseDown);

                //=========
                MinesBoard[i, j].button.Tag = new Point(i, j);
                //MinesBoard[i, j].button.Tag = MinesBoard[i, j].button;
                //=========

                if (MinesBoard[i, j].IsMine)
                    MinesBoard[i, j].button.Text = "z";
                else
                    MinesBoard[i, j].button.Text = "";

                frm.Controls.Add(MinesBoard[i, j].button);

            }
    }

when I change number of mines, it doesn't change correctly. But the rows and columns is OK this is my Form1 class

public partial class frmMain : Form
{
    public frmMain()
    {
        InitializeComponent();

    }
    private Board board;
    private void frmMain_Load(object sender, EventArgs e)
    {
        board = new Board(8, 10, 15);
        board.DrawBoard(this);

    }

    private void optionToolStripMenuItem_Click(object sender, EventArgs e)
    {

        frmOption option = new frmOption(board.Row, board.Column, board.NumOfMines);
        if (option.ShowDialog() == DialogResult.OK)
        {
            board = new Board(option.Rows, option.Columns, option.Mines);
            this.Size = new Size(board.Column * 25 + 5, board.Row * 25 + 88);
            board.DrawBoard(this);

        }

    }

here is the picture: 8x10 15 mines enter image description here

5x5 5 mines enter image description here

when I set number of mines is 5, but it shows only 3 (1 z letter is 1 mine) could you give me any solutions, thank you very much.!!!

Upvotes: 0

Views: 282

Answers (2)

Gene
Gene

Reputation: 4232

When you click on the tool strip menu item, you're creating a new board, which will cause the old buttons to stay on the form (resize the form to see all of the old buttons). You will notice, that the mines are placed on the same buttons as before. This is caused by the old buttons staying in front of the new ones. Therefore, you need to clear the old board before creating a new one. Example:

public void Clear(Form frm)
{
  for (int i = 0; i < maxrow; ++i)
  {
    for (int j = 0; j < maxcol; ++j)
    {
      frm.Controls.Remove(MinesBoard[i, j].button);
    }
  }
}

Call this directly before creating the new board:

private void optionToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (board != null)
  {
    board.Clear(this);
  }

  board = new Board(5, 5, 5);
  this.Size = new Size(board.Column * 25 + 5, board.Row * 25 + 88);
  board.DrawBoard(this);
}

Be sure to fix the bug mentioned by @AndersJH as well :)

Upvotes: 2

AndersJH
AndersJH

Reputation: 142

I hope I don't misunderstand the question here. But the method GetRandomPoints does not guarantee to add a point to the list of random points. If the point exists, it will not be added.

I would suggest something like this:

while(randomPoint.Count < numOfMines)
{ GetRandomPoints(maxrow, maxcol); }

Upvotes: 2

Related Questions