10*10 table,单元格的邻居数
10*10 table, cell's neighbor count
所以我得到了这个代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Table t = new Table();
}
}
class Cell
{
Point position;
const int SIZE = 20;
Cell[] neighbors;
public Cell(Point position, Random r)
{
this.position = position;
Visualisation(r);
}
void Visualisation(Random r)
{
Graphics paper= Form1.ActiveForm.CreateGraphics();
paper.DrawRectangle(new Pen(Color.Red), position.X, position.Y, SIZE, SIZE);
}
}
class Table
{
Cell[] table = new Cell[100];
public Table()
{
Random r = new Random();
for (int i = 0; i < 100; i++)
{
table[i] = new Cell(new Point(i % 10 * 20 + 40, i / 10 * 20 + 40), r);
}
}
我会将数字写入所有单元格,每个单元格有多少邻居。
我该怎么做?
Cella[] szomszedok;是我应该计算每个单元格有多少邻居的部分。
我在单元格中需要的目标:
3 5 5 5 5 5 5 5 5 3
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
3 5 5 5 5 5 5 5 5 3
对此有很多可能的方法。
一种天真的方法是创建一个 GetIndex(int x, int y)
方法来获取要用于 table[]
的索引。对于不在网格上的位置,让它 return -1
。然后创建一个 GetCell(int x, int y)
方法来调用 GetIndex()
和 return 给定的单元格,或者 null
用于不在网格上的位置。
现在,您可以通过引入一种查找邻居的方法来计算 [x, y]
处给定单元格的邻居数:
public List<Cell> GetNeighbors(int x, int y)
{
var neighbors = new List<Cell>();
neighbors.Add(GetCell(x - 1, y - 1));
neighbors.Add(GetCell(x + 0, y - 1));
neighbors.Add(GetCell(x + 1, y - 1));
// ...
neighbors.Add(GetCell(x + 1, y + 1));
return neighbors;
}
然后要计算一个单元格的邻居,只需计算 table.GetNeighbors(x, y).Count(n => n != null)
。
所以我得到了这个代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Table t = new Table();
}
}
class Cell
{
Point position;
const int SIZE = 20;
Cell[] neighbors;
public Cell(Point position, Random r)
{
this.position = position;
Visualisation(r);
}
void Visualisation(Random r)
{
Graphics paper= Form1.ActiveForm.CreateGraphics();
paper.DrawRectangle(new Pen(Color.Red), position.X, position.Y, SIZE, SIZE);
}
}
class Table
{
Cell[] table = new Cell[100];
public Table()
{
Random r = new Random();
for (int i = 0; i < 100; i++)
{
table[i] = new Cell(new Point(i % 10 * 20 + 40, i / 10 * 20 + 40), r);
}
}
我会将数字写入所有单元格,每个单元格有多少邻居。 我该怎么做? Cella[] szomszedok;是我应该计算每个单元格有多少邻居的部分。 我在单元格中需要的目标:
3 5 5 5 5 5 5 5 5 3
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
5 8 8 8 8 8 8 8 8 5
3 5 5 5 5 5 5 5 5 3
对此有很多可能的方法。
一种天真的方法是创建一个 GetIndex(int x, int y)
方法来获取要用于 table[]
的索引。对于不在网格上的位置,让它 return -1
。然后创建一个 GetCell(int x, int y)
方法来调用 GetIndex()
和 return 给定的单元格,或者 null
用于不在网格上的位置。
现在,您可以通过引入一种查找邻居的方法来计算 [x, y]
处给定单元格的邻居数:
public List<Cell> GetNeighbors(int x, int y)
{
var neighbors = new List<Cell>();
neighbors.Add(GetCell(x - 1, y - 1));
neighbors.Add(GetCell(x + 0, y - 1));
neighbors.Add(GetCell(x + 1, y - 1));
// ...
neighbors.Add(GetCell(x + 1, y + 1));
return neighbors;
}
然后要计算一个单元格的邻居,只需计算 table.GetNeighbors(x, y).Count(n => n != null)
。