我创造了一个简化的生命游戏。生命游戏 - 这个算法是什么
这是我的手机在实际的事情发生:
class Cell
{
public Cell(MainWindow correspondingMainWindow){
this.mainWindow = correspondingMainWindow;
}
public bool excluded;
public Boolean Occupied { get; set; }
public Control correspondingPanel;
private int[] coordinates;
private MainWindow mainWindow;
public int[] Coordinates
{
get { return this.coordinates; }
set {
if(value.Length != 2)
{
throw new ArgumentException();
}
else if(value[0] < 0 || value [1] < 0
|| value[0] > Settings.FIELDWIDTH
|| value[1] > Settings.FIELDHEIGHT)
{
throw new ArgumentException();
}
else{
correspondingPanel = mainWindow.FieldArea.Controls
.Find(String.Format("panel{0}_{1}", value[0], value[1]), true)
.FirstOrDefault();
this.coordinates = value;
}
}
}
//Surrounding Cells in the 3x3 around the current cell
//this is to speed up the updating as soon as the algorithm runs on many cells
public Cell Top { get; set; }
public Cell TopRight { get; set; }
public Cell Right { get; set; }
public Cell BotRight { get; set; }
public Cell Bot { get; set; }
public Cell BotLeft { get; set; }
public Cell Left { get; set; }
public Cell TopLeft { get; set; }
public void die()
{
this.Occupied = false;
this.correspondingPanel.BackColor = Color.Beige;
}
public void populate()
{
this.Occupied = true;
this.correspondingPanel.BackColor = Color.DarkRed;
}
}
这里是有问题的算法:
//should return true if there were any changes to any "living" state
bool Algorithm.runOver(Cell target)
{
if (target.Occupied && !target.excluded)
{
target.Right.populate();
target.Left.populate();
target.Top.populate();
target.Bot.populate();
target.Right.excluded = true;
target.Left.excluded = true;
target.Top.excluded = true;
target.Bot.excluded = true;
return true;
}
else
{
return false;
}
}
排除和已经运行的重置了假每次算法运行完毕所有细胞一次。这是为了防止在此步行中更新的Cell上的walkOver()的调用。
现在,当我把我的细胞放在我的领域的左上角(像简单的蛇场一样继续)时,标记它并运行算法,它在第一次运行后停止更改。
它实际上以设计的方式更新Cell,但随后停止。
我将我的单元格保存到程序中的一个静态列表(我知道我不应该,但它是最简单的方法,直到它正常工作)。细胞有一些面板匹配。这些面板位于名为FieldArea的GroupBox中。它们是根据一些静态常量设置(您在坐标集验证中看到的)动态生成的。
我确定单元格与面板正确匹配。这个问题似乎发生在从左上角(0,0)到右下角的FieldArea的对角线上。任何其他起点都可以正常工作。不知何故,当大量细胞从“顶部”进入时,细胞在该区域的顶部和左侧边缘形成边界。
现在的问题是:我做错了什么?为什么我的字段无法正常工作?
如果您的问题很严重,请您忘记我的意见。 –