生命游戏/Game of Life的Java实现(转)
首先简单介绍一下《生命游戏》
生命游戏其实是一个零玩家游戏。它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。具体如下图:
每个格子的生死遵循下面的原则:
1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。
2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3. 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变设定图像中每个像素的初始状态后依据上述的游戏规则演绎生命的变化,由于初始状态和迭代次数不同,将会得到令人叹服的优美图案)。
图案:
有三类图案:
第一类这种图案是固定不变的:
第二类:是按规律变化的:
第三类:是不断变化和移动的,但是有一定的周期性,最著名的就是 滑翔机:
Java实现代码:
package com.cisco.gendwang; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem; public class LifeGame extends JFrame
{
private final World world; public LifeGame(int rows, int columns)
{
world = new World(rows, columns);
new Thread(world).start();
add(world);
} public static void main(String[] args)
{
LifeGame frame = new LifeGame(40, 50); JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu); JMenu options = new JMenu("Options");
menu.add(options); JMenuItem arrow = options.add("Arrow");
arrow.addActionListener(frame.new ArrowActionListener()); JMenuItem square = options.add("Square");
square.addActionListener(frame.new SquareActionListener()); JMenu help = new JMenu("Help");
menu.add(help); frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1007, 859);
frame.setTitle("Game of Life");
frame.setVisible(true);
frame.setResizable(false);
} class ArrowActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.setArrow();
}
} class SquareActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.setSquare();
}
}
}
package com.cisco.gendwang; import java.awt.Graphics; import javax.swing.JPanel; public class World extends JPanel implements Runnable
{
private final int rows;
private final int columns;
private final CellStatus[][] generation1;
private final CellStatus[][] generation2;
private CellStatus[][] currentGeneration;
private CellStatus[][] nextGeneration;
private volatile boolean isChanging = false; public World(int rows, int columns)
{
this.rows = rows;
this.columns = columns;
generation1 = new CellStatus[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
generation1[i][j] = CellStatus.Dead;
}
} generation2 = new CellStatus[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
generation2[i][j] = CellStatus.Dead;
}
} currentGeneration = generation1;
nextGeneration = generation2;
} @Override
public void run()
{
while(true)
{
synchronized(this)
{
while(isChanging)
{
try
{
this.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
} repaint();
sleep(1); for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
evolve(i, j);
}
} CellStatus[][] temp = null;
temp = currentGeneration;
currentGeneration = nextGeneration;
nextGeneration = temp; for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
nextGeneration[i][j] = CellStatus.Dead;
}
}
}
}
} @Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if(currentGeneration[i][j] == CellStatus.Active)
{
g.fillRect(j * 20, i * 20, 20, 20);
}
else
{
g.drawRect(j * 20, i * 20, 20, 20);
}
}
}
} public void setArrow()
{
setShape(arrow);
} public void setSquare()
{
setShape(square);
} private void setShape(int[][] shape)
{
isChanging = true; int arrowsRows = shape.length;
int arrowsColumns = shape[0].length; int minimumRows = (arrowsRows < rows) ? arrowsRows: rows;
int minimumColumns = (arrowsColumns < columns) ? arrowsColumns : columns; synchronized(this)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
currentGeneration[i][j] = CellStatus.Dead;
}
} for(int i = 0; i < minimumRows; i++)
{
for(int j = 0; j < minimumColumns; j++)
{
if(shape[i][j] == 1)
{
currentGeneration[i][j] = CellStatus.Active;
}
}
} isChanging = false;
this.notifyAll();
}
} private void evolve(int x, int y)
{
int activeSurroundingCell = 0; if(isValidCell(x - 1, y - 1) && (currentGeneration[x - 1][y - 1] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x, y - 1) && (currentGeneration[x][y - 1] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x + 1, y - 1) && (currentGeneration[x + 1][y - 1] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x + 1, y) && (currentGeneration[x + 1][y] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x + 1, y + 1) && (currentGeneration[x + 1][y + 1] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x, y + 1) && (currentGeneration[x][y + 1] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x - 1, y + 1) && (currentGeneration[x - 1][y + 1] == CellStatus.Active))
{
activeSurroundingCell++;
} if(isValidCell(x - 1, y) && (currentGeneration[x - 1][y] == CellStatus.Active))
{
activeSurroundingCell++;
} if(activeSurroundingCell == 3)
{
nextGeneration[x][y] = CellStatus.Active;
}
else if(activeSurroundingCell == 2)
{
nextGeneration[x][y] = currentGeneration[x][y];
}
else
{
nextGeneration[x][y] = CellStatus.Dead;
}
} private boolean isValidCell(int x, int y)
{
if((x >= 0) && (x < rows) && (y >= 0) && (y < columns))
{
return true;
}
else
{
return false;
}
} private void sleep(int x)
{
try
{
Thread.sleep(1000 * x);
} catch (InterruptedException e)
{
e.printStackTrace();
}
} static enum CellStatus
{
Active,
Dead;
} private static final int[][] arrow = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
}
------------------------------------------ 更新2017年06月05日16:54:52 ------------------------------------------
项目地址:https://github.com/zpfbuaa/Game-Of-Life
项目代码:http://files.cnblogs.com/files/zpfbuaa/GameOfLife.rar
生命游戏/Game of Life的Java实现(转)的更多相关文章
- 生命游戏/Game of Life的Java实现
首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...
- 生命游戏 Java
本程序由四个类组成:其中Init_data,用于初始化各个活细胞的状态judge_state,用于判断下一代的细胞状态,并进行更新.set_color,用于给GUI界面中各个细胞涂色set_frame ...
- 实用---生命游戏 Java
本程序由四个类组成: 其中Init_data,用于初始化各个活细胞的状态judge_state,用于判断下一代的细胞状态,并进行更新.set_color,用于给GUI界面中各个细胞涂色set_fram ...
- Java实现 蓝桥杯 生命游戏
标题:生命游戏 康威生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 这个游戏在一个无限大的2D网格上进行. 初始时,每个小方格中居住着一个活着或死了的细胞. 下一时刻每个细胞的状态 ...
- java实现第八届蓝桥杯生命游戏
生命游戏 题目描述 康威生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 这个游戏在一个无限大的2D网格上进行. 初始时,每个小方格中居住着一个活着或死了的细胞. 下一时刻每个细胞的 ...
- Java实现 LeetCode 289 生命游戏
289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...
- [LeetCode] Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- React项目(二):生命游戏
引子 这是16年最后的一个练手项目,一贯的感觉就是,做项目容易,写说明文档难.更何况是一个唤起抑郁感觉的项目,码下的每个字,心就如加了一个千斤的砝码. 2016年,有些事我都已忘记,但我现在还记得.2 ...
- javascript小游戏--生命游戏
昨天参加Code Retreat的活动,"Code Retreat是一个一天的集中练习的活动,专注于软件开发和设计的基础". 要了解更多信息可前往 CodeRetreat官网 通过 ...
随机推荐
- 如何自学Android--转
原文地址:http://blog.csdn.net/lavor_zl/article/details/51217319 1. Java知识储备 本知识点不做重点讲解: 对于有基础的同学推荐看<J ...
- HMM 自学教程(四)隐马尔科夫模型
本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...
- 流行趋势:25款很酷的长阴影效果 LOGO 设计
长阴影其实就是扩展了对象的投影,感觉是一种光线照射下的影子,通常采用角度为 45 度的投影,给对象添加了一份立体感.长阴影(Long Shadow)概念来自于最新非常流行的扁平化设计(Flat Des ...
- Web 开发人员必备的随机 JSON 数据生成工具
在 Web 开发中,经常会需要一些测试数据来测试接口或者功能时候正确.JSON Generator 就是这样一款生成随机 JSON 数据的在线工具,Web 开发人员必备,记得收藏和分享啊. 您可能感兴 ...
- EPANET中的哈希文件——hash.c
/*-----------------------------------------------------------------------------** hash.c**** Imp ...
- zabbix配fpmmm(mpm)数据传送不了问题解决
我们环境用zabbix mpm来监控mysql,不过最近官网已经不叫mpm了,而是叫fpmmm,理由为: fpmmm is the successor of mpm. mpm was renamed ...
- IE11之F12 Developer Tools--概述篇
打开Developer Tools的方法: a. 点击F12 b. 在浏览器中选择Tools-->F12 Develooper Tools 打开后图示: 从上图我们可以看到,Developer ...
- 查找最小的k 个元素之C#算法实现
紧接着上一篇微软编程面试100题,这次想解决的是查找最小的K个元素,题目是:输入n 个整数,输出其中最小的k 个.例如输入1,2,3,4,5,6,7 和8 这8 个数字,则最小的4 个数字为1,2,3 ...
- struts2基础——自定义拦截器
一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...
- RequireJS 模块化加载框架使用
RequireJS 是一个遵循 AMD 规范的模块化加载框架 与上文seajs一样,这里简单介绍其相关用法 同样的,首先是下载好 require.js --> http://requirejs. ...