Java 自动细胞机
import javax.print.attribute.standard.NumberUp;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.lang.Thread;
class View extends JPanel
{
private static final long serialVersionUID=-313123123123L;
private static final int GRID_SIZE=16;
private Field theField;
public View(Field field)
{
theField=field;
}
@Override
public void paint(Graphics 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);
}
}
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);
}
}
}
class Field
{
private int height;
private int width;
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)
{
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 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.getHeight();col++)
{
field.place(row,col,new Cell());
}
}
for(int row=0;row<field.getHeight();row++)
{
for(int col=0;col<field.getHeight();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.EXIT_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();row++)
{
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.println();
}
}
System.out.println("UPDATA");
frame.repaint();
try
{
Thread.sleep(80);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}