数据和表现
为了更好的减小代码的复杂程度,降低系统的耦合,数据的管理和可视化的表现,在代码设计要求中很重要,以便在未来让代码有更好的可扩展性,并且能够更好的去维护代码,需要去不断地规范在开始编程之前去设计好框架和接口。
细胞自动机
细胞自动机(在这个表格区域中,黑点表示活着alive的细胞,白点表示死亡die的细胞)
程序每一轮做的事情主要是判断细胞的新生和更迭
死亡:如果活着的邻居的数量<2或>3,则死亡
新生:如果正好有3个邻居活着,则新生
如图:
数据和数据接口
细胞cell
package cell;
import java.awt.Graphics;
public class Cell {
private boolean alive = false; //定义活着的细胞
public void die() {
alive = false; } //定义死亡的细胞
public void reborn() {
alive = true; }
public boolean isAlive() {
return alive; } //判断是否活着
//
public void draw(Graphics g, int x, int y, int size) {
g.drawRect(x, y, size, size);
if ( alive ) {
g.fillRect(x, y, size, size);
}
}
}
数据管理者Field
import java.util.ArrayList;
import cell.Cell;
public class Field {
//定义表格区域的变量高和宽
private int width;
private int height;
private Cell[][] field; //定义一个二维数组
//构造函数
public Field(int width, int height) {
this.width = width;
this.height = height;
field = new Cell[height][width];
}
public int getWidth() {
return width; }
public int getHeight() {
return height; }
public Cell place(int row, int col, Cell o) {
Cell ret = field[row][col];
field[row][col] = o;
return ret;
}
public Cell get(int row, int col) {
r