Java项目——细胞自动机

细胞机概念

死亡:如果活着的邻居的数量小于2或者大于3,则死亡
新生:如果正好有三个邻居活着,则新生
其他情况保持原状

代码

CellMachine.java

package callmachine;

import javax.swing.JFrame;

public class CellMachine {

	public static void main(String[] args){
		Field field = new Field(30,30);
		for(int row = 0; row<field.getHeight();row++){
			for(int col = 0; col<field.getWidth();col++){
				field.place(row,col,new 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();
				}
			}
		}
		View view = new View(field);
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.NXIT_ON_CLOSE);		//	关闭程序窗口
		frame.setResizable(false);
		frame.setTitle("Cells");
		frame.add(view);
		frame.pack();
		frame.setVisible(true);

		for(int i = 0;i<1000;i++){
			for(int row = 0;row<field.getHeight();raw++){
				for(int col = 0;col<field.getWidth();col++){
					Cell cell = field.get(row,col);
					Cell[] neighbour = field.getNeighbour(row,col);
					int numOfLive = 0;
					for(Cell c:neighbour){
						if(c.isAlive()){
							numOfLive++;
						}
					}
					System.out.print("["+row+"]["+col+"]:");
					System.out.print(cell.isAlive()?"live","dead");
					System.out.print(":"+numOfLive+"-->");
					if(cell.isAlive()){
						if(numOfLive < 2 || numOfLive > 3){
							cell.die();
							System.out.print("die");
						}
					}else if(numOfLive == 3){
						cell.reborn();
						System,out,print("reborn");
					}
					System.out.print();
				}
			}
			System.out.print("UPDATE");
			frame.repaint();
			try{
				Thread.sleep(200);
			}catch(InterruptedException e){
				e.printStackTrace();
			}			
		}
 	}
}

View.java

package field;

import java.awt.Dimension;

public class View extends JPanel{
	private static final long serialVersionUID = -525899567621660595L;
	private static final int GRID_SIZE = 16;
	private Field theField;

	public View(Field field){
		theField = field;
	}
	
	@Override
	public void paint(Graphicd g){
		super.paint(g);
		for(int row = 0;row< theField.getHeight();row++){
			for(int col = 0;col< theField.getWidth();col++){
				Cell cell = theField.get(row,col);
				if(cell != null){
					cell.draw(g,col*GRID_SIZE,row*GRID_SIZE,GRID_SIZE);
				}
			}
		}
	}

	@Override
	public Dimension getPreferredSize(){
		return new Dimension(theField.getWidth()*GRID_SIZE+1,theField.getHeight()*GRID_SIZE+1);
	}

	public Dimension getPreferredSize(){
		Field field = new Field(10,10);
		for(int row = 0; row<field.getHeight();row++){
			for(int col = 0;col<field.getWidth();col++){
				field.place(row,col,new Cell());
				//未完待续
			}
		}
	}
	
}

Field.java

package cell;

import java.util.ArrayList;

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] = 0;
		return ret;
	} 

	public Cell get(int row,int col){
		return field[row][col];
	}

	public Cell[] getNeighbour(int row,int col){
		ArrayList<Cell> list = new ArrayList<Cell>();
		for(int i =1;i<2;i++){
			for(int j=1;j<2;j++){
				int r = row+i;
				int c = col+j;
				if(r>-1 && r<height && c>-1 && c<width && !(r== row && c == col)){
					list.add(field[r][c]);
				}
			}
		}
		return list.toArray(new Cell[list.size()]);
	}

	public void clear(){
		
	}
	//未完待续
}

Cell.java

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);
		}
	}
} 

一、数据和表现分离

1. 程序的业务逻辑和表现无关;
2. 表现可以是图形的也可以是文本的;
3. 表现可以是本地的也可以是远程的;

二、责任驱动的设计

· 将程序要实现的功能分配到合适的类/对象中去是设计中非常重要的一环

三、网格化

1.图形界面本身有更高的解析度;
2. 将画面网格化以后,数据就更容易处理了;
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值