我目前有一个扩展System.Windows.Forms.Label
类的类。我正在寻找扫雷程序来进行知识分配。它基本上为常规标签添加了一些功能,然后在form1.cs部分中实例化。
public partial class Cell : System.Windows.Forms.Label
{
private bool hasBomb;
private bool isRevealed;
private int neighbourBombCount;
static int BombAmount;
// Properties
public bool HasBomb
{
get { return hasBomb; }
set { hasBomb = value; }
}
public bool IsRevealed
{
get { return isRevealed; }
set { isRevealed = value; }
}
public int NeighbourBombCount
{
get { return neighbourBombCount; }
set { neighbourBombCount = value; }
}
// constructors
public Cell()
{
hasBomb = false;
isRevealed = false;
neighbourBombCount = 0;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Size = new System.Drawing.Size(50, 50);
}
然后是form1.cs:
public partial class Form1 : Form
{
// Public variable declarations
int chance;
Random rand1 = new Random();
static int bombAmount;
public Form1()
{
InitializeComponent();
this.AutoSize = false;
this.Width = 420;
this.Height = 420;
createGrid();
}
public void createGrid()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
grid[i][j] = new Cell();
grid[i][j].Name = "grid" + i.ToString() + j.ToString();
grid[i][j].Location = new System.Drawing.Point(i * 49, j * 49);
grid[i][j].Size = new System.Drawing.Size(50, 50);
grid[i][j].TabIndex = 0;
chance = rand1.Next(0, 6);
if (chance % 6 == 0 && bombAmount < 10)
{
grid[i][j].HasBomb = true;
bombAmount++;
}
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j <8; j++)
{
this.Controls.Add(grid[i][j]);
}
}
}
private Cell[][] grid = new Cell[8][];
其中有一些逻辑,现在不重要。问题是没有任何东西出现,我错过了什么?
答案 0 :(得分:1)
您没有初始化锯齿状阵列的第二部分。
尝试添加此内容:
public void createGrid() {
for (int i = 0; i < 8; i++) {
grid[i] = new Cell[8];