0

我的程序有问题(扫雷游戏)这是我的板课(地雷板)

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

            }
    }

当我更改地雷数量时,它不会正确更改。但是行和列都可以,这是我的 Form1 类

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

        }

    }

这是图片:8x10 15 地雷 在此处输入图像描述

5x5 5 地雷 在此处输入图像描述

当我将地雷数设置为 5 时,它只显示 3(1 个 z 字母是 1 个地雷)你能给我任何解决方案吗,非常感谢。!!!

4

2 回答 2

2

我希望我不会误解这里的问题。但是 GetRandomPoints 方法不保证将一个点添加到随机点列表中。如果该点存在,则不会添加。

我会建议这样的事情:

while(randomPoint.Count < numOfMines)
{ GetRandomPoints(maxrow, maxcol); }
于 2013-10-24T11:15:21.230 回答
2

当您单击工具条菜单项时,您正在创建一个新板,这将导致旧按钮保留在表单上(调整表单大小以查看所有旧按钮)。您会注意到,地雷放置在与以前相同的按钮上。这是由于旧按钮位于新按钮之前。因此,您需要在创建新板之前清除旧板。例子:

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

在创建新板之前直接调用它:

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

一定要修复@AndersJH提到的错误:)

于 2013-10-24T12:28:14.193 回答