这是我在论坛上一个朋友出的一道题目,然后我再用java来写的。总共3个类。
- public class mainapp {
- /**
- * 1. 如果一个细胞只有0或1个邻居,它将因为孤独而死;
- * 2. 如果一个细胞有4到8个邻居,它将因为拥挤而死;
- * 3. 如果一个细胞恰有2或者3个邻居,它将继续生存下去;
- * 4. 如果一个空格子恰有3个邻居,将“生”出一个新细胞;
- * 5. 其他的空格子继续维持原状。
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Cellsgroup test = new Cellsgroup();
- test.setVisible(true);
- }
- }
- /*
- * 此类是一个窗体,用来装cell的容器
- */
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- public class Cellsgroup extends JFrame {
- private static final long serialVersionUID = 1L;
- public int rows = 10; //行
- public int coloums = 10; //列数
- Cell[] Cellgroup = new Cell[rows*coloums]; //装载Cellgroup的类
- //构造方法
- public Cellsgroup(){
- super("cell"); //设置标题
- Toolkit mykit = this.getToolkit();
- Dimension wndsize = mykit.getScreenSize();
- int size = wndsize.width/2;
- this.setBounds((wndsize.width-size)/2,(wndsize.height-size)/2
- ,size,size); //设置大小
- this.allcell();
- this.setDefaultCloseOperation(EXIT_ON_CLOSE); //设置推出
- this.setVisible(true);
- }
- //添加元素
- private void allcell() {
- GridLayout layout = new GridLayout(rows,coloums); //设置网格布局管理器
- Container content = this.getContentPane();
- content.setLayout(layout);
- for(int i = 0;i<Cellgroup.length;i++){ //初始化,放入cell
- Cellgroup[i] = new Cell(i,this);
- content.add(Cellgroup[i]);
- }
- for(Cell cell:Cellgroup){
- cell.initialfrinends(cell.getNo());
- }
- }
- }
- /*细胞,为了处理方便,这里把其做为一个Label表示。*/
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JLabel;
- import javax.swing.Timer;
- public class Cell extends JLabel {
- private static final long serialVersionUID = 1L;
- private boolean status; //表示细胞存活的状态。
- private Cell[] cellfriend; //存储附近细胞的类
- private Cellsgroup Cellsgroup; //载入窗体
- private int no; //窗体编号
- public int getNo() {
- return no;
- }
- Timer t ; //计时器
- //获取状态
- public boolean isStatus() {
- return status;
- }
- //设定状态
- public void setStatus(boolean status) {
- if(status){
- this.setText("Cell"); //表示出现新的细胞
- }else{
- this.setText(""); //细胞死亡
- }
- this.repaint();
- this.status = status;
- }
- //构造函数
- public Cell(int no,Cellsgroup Cellsgroup){
- this.Cellsgroup = Cellsgroup;
- this.initialStatus(); //初始化状态
- this.no = no;
- t = new Timer((int) (200*Math.random()+900),new timelistener());
- t.start(); //打开计时器
- }
- //初始化状态
- private void initialStatus(){
- if(Math.random()>0.5){ //当随机数大于0.5时,表示细胞存活
- this.setStatus(true); //为存活
- }else{
- this.setStatus(false); //为死亡
- }
- }
- //设定朋友群
- public void initialfrinends(int no){
- cellfriend = new Cell[8]; //表示8个方位
- this.setOneFriend(1, no-Cellsgroup.coloums); //正上方
- if(((no-1)%Cellsgroup.coloums)!=0){ //左边一排没有左面
- this.setOneFriend(3, no-1); //左面
- this.setOneFriend(0, no-Cellsgroup.coloums-1); //左上角
- this.setOneFriend(5, no+Cellsgroup.coloums-1); //左下角
- }
- if((no%Cellsgroup.coloums)!=0){ //右边一排没有右面
- this.setOneFriend(4, no+1); //右面
- this.setOneFriend(2, no-Cellsgroup.coloums+1); //右上角
- this.setOneFriend(7, no+Cellsgroup.coloums+1); //右下角
- }
- this.setOneFriend(6, no+Cellsgroup.coloums); //正下方
- }
- //设定一个朋友
- //friendno是将要放入cellfrind的下标,cellsno是cellgroup对应的下标。
- private void setOneFriend(int friendno,int cellsno){
- //因为考虑到边的情况,所以要cellsno判断一下是否越界。
- if((cellsno<Cellsgroup.Cellgroup.length)&&(cellsno>=0)){
- cellfriend[friendno]= Cellsgroup.Cellgroup[cellsno];
- }
- }
- //获得存活的朋友数
- public int getalivefriend(){
- int alivefriend = 0; //计数器
- for(Cell cell:cellfriend){ //迭代整个数组
- if(cell!=null&&cell.isStatus()){ //为存活
- alivefriend++; //计数器加一
- }
- }
- return alivefriend;
- }
- //监听器类
- class timelistener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- int alivefriend = getalivefriend();
- if(alivefriend<2||alivefriend>3){ //细胞会死亡
- setStatus(false);
- }else if(alivefriend==3){ //生成新细胞
- setStatus(true);
- }
- }
- }
- public String toString(){
- return String.valueOf(this.no);
- }
- }