java-生命游戏简易版-软工实验

生命游戏

Cell.java

package Game;
import java.util.Random;
public class Cell {
    private int status;//细胞存活状态,0为死,1为生
    private int livings;//细胞周围的活细胞个数
    private int x;
    private int y;
    Random ran=new Random();
    Cell(){
        this.status=0;
        this.livings=0;
    }
    Cell(int status,int livings){
        this.status=status;
        this.livings=livings;
    }
    public int getStatus() {
        return status;//获取状态
    }
    public void setStatus(int status) {
      this.status=status;//设置状态
    }
    public int getLiving() {
        return this.livings;
    }
    public void setLiving(int livings) {
        this.livings=livings;//设置当前周围活细胞数目
    }
    public void UpdateStatus(){
           if(this.livings==3){
               this.status=1;
           }
           else if(this.livings==2){
           }
           else{
               this.status=0;
           }
    }
}

Map.java

package Game;
import java.util.Random;
public class Map {
    final static int xlength=16;
    final static int ylength=16;
    //创建地图中各个细胞
    public static Cell[][] Create(){
        Cell[][] cell=new Cell[xlength][ylength];
        for(int i=0;i<xlength;i++){
            for(int j=0;j<ylength;j++){
            	cell[i][j]=new Cell();
                Random ran=new Random();
                cell[i][j].setStatus(ran.nextInt(2));
            }
        }
        return cell;
    }
    
    //获取每个细胞周围的细胞存活数
    public static void getLivings(Cell[][] cell){
    	for(int i=0;i<xlength;i++) {
    		for(int j=0;j<ylength;j++) {
    			int living=0;
    			if(i>0&&j>0) living+=cell[i-1][j-1].getStatus();
    			if(i>0) living+=cell[i-1][j].getStatus();
    			if(j>0) living+=cell[i][j-1].getStatus();
    			if(i<xlength-1&&j<xlength-1) living+=cell[i+1][j+1].getStatus();
    			if(i<xlength-1) living+=cell[i+1][j].getStatus();
    			if(j<xlength-1) living+=cell[i][j+1].getStatus();
    			if(i<xlength-1&&j>0) living+=cell[i+1][j-1].getStatus();
    			if(i>0&&j<xlength-1) living+=cell[i-1][j+1].getStatus();
    			cell[i][j].setLiving(living);
    		}
    	}
    }
    //更新细胞状态
    public static int Update(Cell[][]cell){
    	int count=0;
        for(int i=0;i<xlength;i++){
            for(int j=0;j<ylength;j++){
            	int status=cell[i][j].getStatus();
    			cell[i][j].UpdateStatus();
    			if(status==cell[i][j].getStatus()) count++;
            }
        }
        return count;
    }
    //测试输出当前各个细胞的状态
    public static void Print(Cell[][]cell){
        for(int i=0;i<xlength;i++){
            for(int j=0;j<ylength;j++){
                System.out.print(cell[i][j].getStatus()+" ");
                System.out.println();
            }
        }
    }
}

GUI.java

package Game;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI implements ActionListener{
	//创建主窗口
	JFrame jframe;
	//面板
	JPanel jpanel;
	JPanel jpanel2;
	//迭代次数
	int count=0;
	//绘图工具
	Graphics g;
	JButton button;
	JButton button2;
	//二维矩阵记录元胞变化
	Cell [][]cell;
	
	GUI() {
		//窗口初始化
		jframe=new JFrame();
    	jframe.setSize(480,460);
    	jpanel = new JPanel();
    	jpanel.setBounds(0, 0, 400, 400);
    	jpanel.setLayout(null);
    	//主窗口设置
    	jframe.setTitle("这是第"+count+"次演化");
    	jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	jframe.setLocationRelativeTo(null);
    	jframe.getContentPane().setLayout(null);
    	jframe.getContentPane().add(jpanel);
    	//主面板设置
    	jpanel2=new JPanel();
    	jpanel2.setBounds(400, 0, 60, 400);
    	jpanel2.setLayout(null);
    	
    	button = new JButton("开始");
    	button.setBounds(0, 0, 60, 60);
    	jpanel2.add(button);    	
    	
    	button2 = new JButton("生成");
    	button2.setBounds(0, 60, 60, 60);
    	jpanel2.add(button2);
    	
    	jframe.getContentPane().add(jpanel2);
    	jframe.setVisible(true);
    }
	//画出图像
	public void drawCell(Cell[][] cell) {
		jframe.setTitle("这是第"+count+"次演化");
		for(int i=0;i<16;i++) {
    		for(int j=0;j<16;j++) {
    			g.setColor(Color.WHITE);
    			g.fillRect(i*25, j*25, 25, 25);
    			g.setColor(Color.BLACK);
    			g.drawRect(i*25, j*25, 25, 25);
    		}
    	}
    	for(int i=0;i<16;i++) {
    		for(int j=0;j<16;j++) {
    			if(cell[i][j].getStatus()==1) {
    				g.setColor(Color.BLACK);
    				g.fillRect(i*25, j*25, 25, 25);
    			}
    		}
    	}
    	 count++;
	}
	//开始迭代
	public void StartPrint(Cell[][] cell) {
		long d1= System.currentTimeMillis();
    	while(true) {
    		long d2= System.currentTimeMillis();
    		if(d2-d1>100) {
    			int change;
    			d1=d2;
    			change=Map.Update(cell);
    			Map.getLivings(cell);
    			//画图
    			drawCell(cell);
    			if(change==16*16) break;
    			if(count>1000) break;
    	    }	    	
      }
    	JOptionPane.showMessageDialog(jframe, "在第"+count+"次演化达到平衡", "提示", JOptionPane.PLAIN_MESSAGE);
	}
	
	//开始运行程序
	public void start(){
		//初始化元胞
    	cell=Map.Create();
    	//获得细胞周围或细胞数
    	Map.getLivings(cell);
    	//网格初始化
    	g=jpanel.getGraphics();
    	//开始监听
    	button.addActionListener(this);
    	button2.addActionListener(this);
	}
	
	public static void main(String[] args) {
		GUI gui=new GUI();
		gui.start();
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==button)
			StartPrint(cell);
		else if(e.getSource()==button2){
			drawCell(cell);
		}
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值