我的程序有问题(扫雷游戏)这是我的板课(地雷板)
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 个地雷)你能给我任何解决方案吗,非常感谢。!!!