/**Conway's Life game.
*/
/**
* This software is provided "AS IS," without a warranty of any kind.
* anyone can use it for free,emails are welcomed concerning bugs or
* suggestions.
*/
/**
*This version uses the java 5.0 generic type linkedlist container
*
*/
/**
* Life.java.
*
* @version 1.1 04/18/2007
* @author Wen Yu, yuwen_66@yahoo.com
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Life extends Applet
implements Runnable, ActionListener{
private myCanvas canvas;
private Button start;
private Panel controlPanel;
private Panel canvasPanel;
private BorderLayout layout;
private Image grid;//memory image to draw on
private Graphics offScrGraphics;//for double buffering
private int cell[][],cellNeighbours[][];
private int cellSize,cellCount,speed;
private int xmin,xmax,ymin,ymax;
private SinglyLinkedList<CellCoordinate> live,die,nextLive,nextDie;
private Thread t;
private boolean firstTime;
public void init(){
// get initial cell parameters from html file
try {
cellSize=Integer.parseInt(getParameter("cellSize"));
cellCount=Integer.parseInt(getParameter("cellCount"));
speed=Integer.parseInt(getParameter("speed"));
}catch(NumberFormatException e){
cellSize=20;
cellCount=30;
speed=50;
}
if (cellCount==0)
cellCount=30;
if (cellSize==0)
cellSize=20;
if (speed==0)
speed=50;
// construct a square cell grid
cell=new int[cellCount][cellCount];
// record the neighbours of a certain cell
cellNeighbours=new int[cellCount][cellCount];
live=new SinglyLinkedList<CellCoordinate>();
die=new SinglyLinkedList<CellCoordinate>();
nextLive=new SinglyLinkedList<CellCoordinate>();
nextDie=new SinglyLinkedList<CellCoordinate>();
initCells();
setComponents();
firstTime=true;
System.gc();
}
public void initCells(){
grid=createImage(cellSize*cellCount,cellSize*cellCount);
offScrGraphics=grid.getGraphics();
offScrGraphics.setColor(Color.black);
offScrGraphics.fillRect(0,0,cellSize*cellCount,cellSize*cellCount);
offScrGraphics.setColor(Color.green);
for (int i=0;i<cellCount;i++){
offScrGraphics.drawLine(i*cellSize,0,i*cellSize,cellSize*cellCount);
offScrGraphics.drawLine(0,i*cellSize,cellSize*cellCount,i*cellSize);
}
}
public void setComponents(){
setLayout(new BorderLayout());
controlPanel=new Panel();
start=new Button("START");
start.addActionListener(this);
controlPanel.add(start);
add(BorderLayout.SOUTH,controlPanel);
canvas=new myCanvas(500,500,grid,this);
canvasPanel=new Panel();
canvasPanel.add(canvas);
add(BorderLayout.CENTER,canvasPanel);
}
public synchronized void nextGeneration(){
int x,y;
offScrGraphics.setColor(Color.red);
CellCoordinate cellCoordinate;
while(nextLive.hasNodes())
{
cellCoordinate = nextLive.removeFromHead();
x=cellCoordinate.x;
y=cellCoordinate.y;
if(cell[x][y]==0&&cellNeighbours[x][y]==3)
{
cell[x][y]=1;
offScrGraphics.fillOval(x*cellSize+1,y*cellSize+1,cellSize-2,cellSize-2);
live.addToTail(new CellCoordinate(x,y));
}
cellCoordinate=null;
}
offScrGraphics.setColor(Color.black);
while(nextDie.hasNodes())
{
cellCoordinate = nextDie.removeFromHead();
x=cellCoordinate.x;
y=cellCoordinate.y;
if((cell[x][y]==1)&&(cellNeighbours[x][y]!=2)&&(cellNeighbours[x][y]!=3))
{
cell[x][y]=0;
offScrGraphics.fillRect(x*cellSize+1,y*cellSize+1,cellSize-2,cellSize-2);
die.addToTail(new CellCoordinate(x,y));
}
cellCoordinate=null;
}
canvas.repaint();
notifyAll();
}
public void placeCell(int x, int y){
if (t!=null) return;
x=x/cellSize;
y=y/cellSize;
if (cell[x][y]==0){
cell[x][y]=1;
offScrGraphics.setColor(Color.red);
offScrGraphics.fillOval(x*cellSize+1,y*cellSize+1,cellSize-2,cellSize-2);
}else{
cell[x][y]=0;
offScrGraphics.setColor(Color.black);
offScrGraphics.fillRect(x*cellSize+1,y*cellSize+1,cellSize-2,cellSize-2);
}
canvas.repaint();
}
public void clearLists(){
live.clear();
die.clear();
nextLive.clear();
nextDie.clear();
}
public void initLists(){
clearLists();
int x,y;
for (int i=0;i<cellCount*cellCount;i++){
x=i%cellCount;
y=i/cellCount;
cellNeighbours[x][y]=0;
if(cell[x][y]==1)
{
live.addToTail(new CellCoordinate(x,y));
}
}
addNeighbours();
for (int i=0;i<cellCount*cellCount;i++){
x=i%cellCount;
y=i/cellCount;
if((cellNeighbours[x][y]<2)&&(cell[x][y]==1))
{
nextDie.addToTail(new CellCoordinate(x,y));
}
}
}
public void addNeighbours(){
int x,y;
CellCoordinate cellCoordinate;
while(live.hasNodes())
{
cellCoordinate = live.removeFromHead();
x=cellCoordinate.x;
y=cellCoordinate.y;
checkBoundary(x,y);
for (int j=xmin;j<=xmax;j++){
for (int k=ymin;k<=ymax;k++){
if (j!=x || k!=y)
{
cellNeighbours[j][k]++;
switch(cellNeighbours[j][k])
{
case 3:
{
if(cell[j][k]==0)
nextLive.addToTail(new CellCoordinate(j,k));
break;
}
case 4:
{
if(cell[j][k]==1)
nextDie.addToTail(new CellCoordinate(j,k));
break;
}
default:break;
}
}
}
}
cellCoordinate=null;
}
}
public void subNeighbours(){
int x,y;
CellCoordinate cellCoordinate;
while(die.hasNodes())
{
cellCoordinate = die.removeFromHead();
x=cellCoordinate.x;
y=cellCoordinate.y;
checkBoundary(x,y);
for (int j=xmin;j<=xmax;j++){
for (int k=ymin;k<=ymax;k++){
if (j!=x || k!=y)
{
cellNeighbours[j][k]--;
switch(cellNeighbours[j][k])
{
case 1:
{
if(cell[j][k]==1)
nextDie.addToTail(new CellCoordinate(j,k));
break;
}
case 3:
{
if(cell[j][k]==0)
nextLive.addToTail(new CellCoordinate(j,k));
break;
}
default:break;
}
}
}
}
cellCoordinate=null;
}
}
public void checkBoundary(int x,int y){
if(x==0)
xmin=0;
else
xmin=x-1;
if(x==cellCount-1)
xmax=cellCount-1;
else
xmax=x+1;
if(y==0)
ymin=0;
else
ymin=y-1;
if(y==cellCount-1)
ymax=cellCount-1;
else
ymax=y+1;
}
public void run(){
initLists();
while (true) {
nextGeneration();
addNeighbours();
subNeighbours();
try{
t.sleep(speed);
}catch(InterruptedException e){}
}
}
public void start(){
if (firstTime) return;
if (t == null) {
t=new Thread(this);
t.start();
}
}
public synchronized void stop(){
if (t != null) {
try{
wait();
}
catch (InterruptedException ex)
{
}
t.stop();
t=null;
}
// nextGeneration();
}
public void actionPerformed(ActionEvent e){
firstTime=false;
if (e.getSource().equals(start)){
if (t != null){
stop();
start.setLabel("START");
}else{
start();
start.setLabel("STOP");
}
}
}
private class CellCoordinate
{
private int x,y;
public CellCoordinate(int x,int y)
{
this.x = x;
this.y = y;
}
}
}
class myCanvas extends Canvas {
Life life;
Image img;
public myCanvas(int w, int h, Image img, Life life){
setSize(w,h);
this.life=life;
this.img=img;
addMouseListener(new myMouseAdapter());
}
class myMouseAdapter extends MouseAdapter{
public void mouseClicked(MouseEvent e){
life.placeCell(e.getX(),e.getY(
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
life.rar (10个子文件)
Life.java 8KB
SinglyLinkedListNode.java 936B
SinglyLinkedListNode.class 1KB
myCanvas$myMouseAdapter.class 581B
myCanvas.class 791B
SinglyLinkedList.class 2KB
SinglyLinkedList.java 3KB
Life.class 6KB
Life$CellCoordinate.class 519B
Life.htm 182B
共 10 条
- 1
资源评论
- zhanghao95472013-02-15很有趣的生命游戏
- jiangmintuo2013-10-11有没有c#编写的life game啊?
dragon_66
- 粉丝: 1
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Cesium 高程为0的地形数据
- 国产化神通数据库-数据库开发手册
- Oracle数据库备份与恢复教程.zip
- 苹果闹铃音乐苹果闹铃音乐
- Unity导入Excel文件自动创建class文件和ScriptObject文件,并转化成ScriptObject的插件pro版
- UE4仅k位移产生完美行走动作-UE4项目
- 基于 Python 的校园导航系统
- tensorflow-2.6.3-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-2.6.2-cp39-cp39-win-amd64.whl
- tensorflow-2.6.2-cp39-cp39-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功