数据与表现分离:细胞自动机
field.place(row, col, new Cell()
public Cell place(int row, int col, Cell o) {
Cell ret = cells[row][col];
cells[row][col] = o;
return ret;
}
在field里创建好多个cell。
for ( int row = 0; row<field.getHeight(); row++ ) {
for ( int col = 0; col<field.getWidth(); col++ ) {
Cell cell = field.get(row, col);
if ( Math.random() < 0.2 ) {
cell.reborn();
}
}
}
为每一个cell设置了活着还是死了的属性。
return list.toArray(new Cell[list.size()]);
将容器 转换为 一个一维数组Cell 规定了大小是 list的大小。