细胞模拟程序

      这是我在论坛上一个朋友出的一道题目,然后我再用java来写的。总共3个类。
      
  1. public class mainapp {
  2.     /**
  3.      * 1. 如果一个细胞只有0或1个邻居,它将因为孤独而死; 
  4.      * 2. 如果一个细胞有4到8个邻居,它将因为拥挤而死; 
  5.      * 3. 如果一个细胞恰有2或者3个邻居,它将继续生存下去; 
  6.      * 4. 如果一个空格子恰有3个邻居,将“生”出一个新细胞; 
  7.      * 5. 其他的空格子继续维持原状。
  8.      */
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Cellsgroup test = new Cellsgroup();
  12.         test.setVisible(true);
  13.     }
  14. }

  1. /*
  2.  * 此类是一个窗体,用来装cell的容器
  3.  */
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.GridLayout;
  7. import java.awt.Toolkit;
  8. import javax.swing.JFrame;
  9. public class Cellsgroup extends JFrame {
  10.     private static final long serialVersionUID = 1L;
  11.     public int rows    = 10;                          //行
  12.     public int coloums = 10;                          //列数
  13.     Cell[]     Cellgroup = new Cell[rows*coloums];    //装载Cellgroup的类
  14.     
  15.     //构造方法
  16.     public Cellsgroup(){
  17.         super("cell");                                 //设置标题
  18.         Toolkit mykit = this.getToolkit();
  19.         Dimension wndsize = mykit.getScreenSize();
  20.         int size = wndsize.width/2;
  21.         this.setBounds((wndsize.width-size)/2,(wndsize.height-size)/2
  22.                        ,size,size);                           //设置大小
  23.         this.allcell();
  24.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);         //设置推出
  25.         this.setVisible(true);
  26.     }
  27.     
  28.     //添加元素
  29.     private void allcell() {
  30.         GridLayout layout = new GridLayout(rows,coloums);     //设置网格布局管理器
  31.         Container  content = this.getContentPane();            
  32.         content.setLayout(layout);            
  33.         for(int i = 0;i<Cellgroup.length;i++){                //初始化,放入cell    
  34.             Cellgroup[i] = new Cell(i,this);
  35.             content.add(Cellgroup[i]);
  36.         }
  37.         for(Cell cell:Cellgroup){
  38.             cell.initialfrinends(cell.getNo());
  39.         }
  40.     }
  41. }
  1. /*细胞,为了处理方便,这里把其做为一个Label表示。*/
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JLabel;
  5. import javax.swing.Timer;
  6. public class Cell extends JLabel {
  7.     private static final long serialVersionUID = 1L;
  8.     private boolean         status;                     //表示细胞存活的状态。
  9.     private Cell[]          cellfriend;                 //存储附近细胞的类 
  10.     private Cellsgroup      Cellsgroup;                 //载入窗体
  11.     private int             no;                         //窗体编号
  12.     public int getNo() {
  13.         return no;
  14.     }
  15.     Timer                   t ; //计时器
  16.     
  17.     //获取状态
  18.     public boolean isStatus() {                         
  19.         return status;
  20.     }
  21.     
  22.     //设定状态
  23.     public void setStatus(boolean status) {
  24.         if(status){
  25.             this.setText("Cell");                      //表示出现新的细胞
  26.         }else{
  27.             this.setText("");                     //细胞死亡
  28.         }
  29.         this.repaint();
  30.         this.status = status;
  31.     }
  32.     
  33.     //构造函数
  34.     public Cell(int no,Cellsgroup Cellsgroup){
  35.         this.Cellsgroup = Cellsgroup;
  36.         this.initialStatus();                           //初始化状态
  37.         this.no   = no;
  38.         t = new Timer((int) (200*Math.random()+900),new timelistener());
  39.         t.start();                                      //打开计时器
  40.         
  41.     }
  42.     
  43.     //初始化状态
  44.     private void initialStatus(){                       
  45.         if(Math.random()>0.5){                          //当随机数大于0.5时,表示细胞存活 
  46.             this.setStatus(true);                       //为存活
  47.         }else{
  48.             this.setStatus(false);                      //为死亡
  49.         }
  50.     }
  51.     
  52.     //设定朋友群
  53.     public void initialfrinends(int no){
  54.         cellfriend = new Cell[8];                          //表示8个方位
  55.         this.setOneFriend(1, no-Cellsgroup.coloums);       //正上方
  56.         if(((no-1)%Cellsgroup.coloums)!=0){                //左边一排没有左面
  57.             this.setOneFriend(3, no-1);                    //左面
  58.             this.setOneFriend(0, no-Cellsgroup.coloums-1); //左上角
  59.             this.setOneFriend(5, no+Cellsgroup.coloums-1); //左下角
  60.         }
  61.         if((no%Cellsgroup.coloums)!=0){                    //右边一排没有右面
  62.             this.setOneFriend(4, no+1);                    //右面
  63.             this.setOneFriend(2, no-Cellsgroup.coloums+1); //右上角
  64.             this.setOneFriend(7, no+Cellsgroup.coloums+1); //右下角
  65.         }
  66.         this.setOneFriend(6, no+Cellsgroup.coloums);       //正下方
  67.     }
  68.     
  69.     //设定一个朋友
  70.     //friendno是将要放入cellfrind的下标,cellsno是cellgroup对应的下标。
  71.     private void setOneFriend(int friendno,int cellsno){
  72.         //因为考虑到边的情况,所以要cellsno判断一下是否越界。
  73.         if((cellsno<Cellsgroup.Cellgroup.length)&&(cellsno>=0)){
  74.             cellfriend[friendno]= Cellsgroup.Cellgroup[cellsno];
  75.         }
  76.     }
  77.     
  78.     //获得存活的朋友数
  79.     public int getalivefriend(){
  80.         int alivefriend = 0;                             //计数器
  81.         for(Cell cell:cellfriend){                       //迭代整个数组
  82.             if(cell!=null&&cell.isStatus()){             //为存活
  83.                 alivefriend++;                           //计数器加一
  84.             }
  85.         }
  86.         return alivefriend;
  87.     }
  88.     
  89.     //监听器类
  90.     class timelistener implements ActionListener{
  91.         
  92.         public void actionPerformed(ActionEvent e) {
  93.             int alivefriend = getalivefriend();
  94.             if(alivefriend<2||alivefriend>3){         //细胞会死亡
  95.                  setStatus(false);
  96.             }else if(alivefriend==3){                 //生成新细胞
  97.                  setStatus(true);
  98.             }
  99.         }
  100.         
  101.     }
  102.     public String toString(){
  103.         return String.valueOf(this.no);
  104.     }
  105. }


      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值