我創造了一個簡化的生命遊戲。生命遊戲 - 這個算法是什麼
這是我的手機在實際的事情發生:
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的對角線上。任何其他起點都可以正常工作。不知何故,當大量細胞從「頂部」進入時,細胞在該區域的頂部和左側邊緣形成邊界。
現在的問題是:我做錯了什麼?爲什麼我的字段無法正常工作?
如果您的問題很嚴重,請您忘記我的意見。 –