本程序由四个类组成:

其中Init_data,用于初始化各个活细胞的状态
judge_state,用于判断下一代的细胞状态,并进行更新。
set_color,用于给GUI界面中各个细胞涂色
set_frame,用于设置GUI界面的布局
 /*初始化细胞状态类
* 四个模式
* 1、easy模式,一个大写的I
* 2、love模式,一个大写的LOVE
* 3、arrow模式,一个粗箭头
* 4、random模式,根据随机函数生成活细胞数
*/
package game_life_self; import java.util.Random; public class Init_data{
static boolean[][] state;
static int rand;
public Init_data(boolean[][] state_one)
{
state = state_one;
} public void init_one()
{
for(int i=1;i<state.length;i++)
{
for(int j=1;j<state[i].length-1;j++)
{
state[i][j] = false;
}
} //LOVE
//L
state[9][3] = true;
state[10][3] = true;
state[11][3] = true;
state[12][3] = true;
state[13][3] = true;
state[14][3] = true;
state[14][4] = true;
state[14][5] = true; //O
state[9][8] = true;
state[10][7] = true;
state[11][7] = true;
state[12][7] = true;
state[13][7] = true;
state[14][8] = true;
state[10][9] = true;
state[11][9] = true;
state[12][9] = true;
state[13][9] = true; //V
state[9][11] = true;
state[10][12] = true;
state[11][13] = true;
state[12][14] = true;
state[13][15] = true;
state[14][16] = true;
state[13][17] = true;
state[12][18] = true;
state[11][19] = true;
state[10][20] = true;
state[9][21] = true; //E
state[9][23] = true;
state[9][24] = true;
state[9][25] = true;
state[9][26] = true;
state[10][23] = true;
state[11][23] = true;
state[12][23] = true;
state[12][24] = true;
state[12][25] = true;
state[12][26] = true;
state[13][23] = true;
state[14][23] = true;
state[15][23] = true;
state[15][24] = true;
state[15][25] = true;
state[15][26] = true; } public void init_two()
{
for(int i=1;i<state.length;i++)
{
for(int j=1;j<state[i].length-1;j++)
{
state[i][j] = false;
}
} state[23][3] = true;
state[23][4] = true;
state[23][5] = true;
state[22][3] = true;
state[22][4] = true;
state[22][5] = true;
state[21][3] = true;
state[21][4] = true;
state[21][5] = true;
state[20][3] = true;
state[20][4] = true;
state[20][5] = true;
state[19][3] = true;
state[19][4] = true;
state[19][5] = true;
state[18][3] = true;
state[18][4] = true;
state[18][5] = true;
state[17][3] = true;
state[17][4] = true;
state[17][5] = true;
state[16][3] = true;
state[16][4] = true;
state[16][5] = true; state[16][6] = true;
state[16][7] = true;
state[16][8] = true;
state[16][9] = true;
state[16][10] = true;
state[16][11] = true;
state[16][12] = true;
state[16][13] = true;
state[17][6] = true;
state[17][7] = true;
state[17][8] = true;
state[17][9] = true;
state[17][10] = true;
state[17][11] = true;
state[17][12] = true;
state[17][13] = true;
state[18][6] = true;
state[18][7] = true;
state[18][8] = true;
state[18][9] = true;
state[18][10] = true;
state[18][11] = true;
state[18][12] = true;
state[18][13] = true; state[15][9] = true;
state[14][9] = true;
state[13][9] = true;
state[12][9] = true;
state[11][9] = true;
state[10][9] = true;
state[9][9] = true;
state[9][8] = true;
state[9][7] = true;
state[9][6] = true;
state[9][5] = true; state[15][10] = true;
state[14][10] = true;
state[13][10] = true;
state[12][10] = true;
state[11][10] = true;
state[10][10] = true;
state[9][10] = true; state[15][11] = true;
state[14][11] = true;
state[13][11] = true;
state[12][11] = true;
state[11][11] = true;
state[10][11] = true;
state[9][11] = true; state[15][12] = true;
state[14][12] = true;
state[13][12] = true;
state[12][12] = true;
state[11][12] = true;
state[10][12] = true;
state[9][12] = true; state[15][13] = true;
state[14][13] = true;
state[13][13] = true;
state[12][13] = true;
state[11][13] = true;
state[10][13] = true;
state[9][13] = true;
state[9][14] = true;
state[9][15] = true;
state[9][16] = true;
state[9][17] = true; state[3][11] = true;
state[4][10] = true;state[4][11] = true;state[4][12] = true;
state[5][9] = true;state[5][10] = true;state[5][11] = true;state[5][12] = true;state[5][13] = true;
state[6][8] = true;state[6][9] = true;state[6][10] = true;state[6][11] = true;state[6][12] = true;state[6][13] = true;state[6][14] = true;
state[7][7] = true;state[7][8] = true;state[7][9] = true;state[7][10] = true;state[7][11] = true;state[7][12] = true;state[7][13] = true;state[7][14] = true;state[7][15] = true;
state[8][6] = true;state[8][7] = true;state[8][8] = true;state[8][9] = true;state[8][10] = true;state[8][11] = true;state[8][12] = true;state[8][13] = true;state[8][14] = true;state[8][15] = true;state[8][16] = true; } public void init_three()
{
for(int i=1;i<state.length;i++)
{
for(int j=1;j<state[i].length-1;j++)
{
rand = (int) (Math.random()*100);
if (rand<50) {
state[i][j] = false;
}
else
state[i][j] = true;
}
}
}
public void init_zero()
{
for(int i=1;i<state.length;i++)
{
for(int j=1;j<state[i].length-1;j++)
{
state[i][j] = false;
}
}
state[9][3] = true;
state[9][4] = true;
state[9][6] = true;
state[9][7] = true;
state[9][5] = true;
state[10][5] = true;
state[11][5] = true;
state[12][5] = true;
state[13][5] = true;
state[14][5] = true;
state[14][4] = true;
state[14][6] = true;
state[14][3] = true;
state[14][7] = true; }
}
 /*主类
* 生命游戏开始入口
* editor:
* 软件1501 刘辉
* time:2017-9-7
*/ package game_life_self; import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane; public class start_frame extends Thread{
static start_frame start;
static Init_data init_data;
static judge_state judge;
static set_color set_color;
static set_frame frame;
//存放细胞状态
static boolean[][] state_one;
static JPanel[][] jPanel;
//初始化GUI界面
public start_frame(int row,int col)
{
state_one = new boolean[row][col];
frame = new set_frame(row,col);
jPanel = frame.jPanel;
} //主函数
public static void main(String[] args)
{
//创建游戏对象
start = new start_frame(30,30);
init_data = new Init_data(state_one);
judge = new judge_state(state_one, jPanel);
set_color = new set_color(state_one, jPanel);
}
}
 /*判断状态类
* 算法:
* 根据游戏规则判断中心细胞的下一步演化规则,并将状态计入state_one[][]中。
* nCount:周围活细胞的个数
*/ package game_life_self; import java.awt.Color; import javax.swing.JPanel; public class judge_state {
static boolean [][] state_one;
JPanel[][] jPanel;
//初始化类
public judge_state(boolean [][] state,JPanel[][] jpanel_one)
{
state_one = state;
jPanel = jpanel_one;
} //判断中心生命的状态并更新
public void judge() {
int life = 0;
for (int i = 1; i < state_one.length - 1; i++) {
for (int j = 1; j < state_one[i].length - 1; j++) {
//循环判断中心生命周围的各个点的状态
int nCount = 0;
if (jPanel[i - 1][j - 1].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i - 1][j].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i - 1][j + 1].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i][j - 1].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i][j + 1].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i + 1][j - 1].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i + 1][j].getBackground() == Color.black) {
nCount++;
}
if (jPanel[i + 1][j + 1].getBackground() == Color.black) {
nCount++;
}
if (nCount==3) {
state_one[i][j] = true;
}
else if (nCount == 2) {
state_one[i][j] = state_one[i][j];
}
else
state_one[i][j] = false; }
}
}
}
 /*涂色类
* 根据state[][]中各个表格的状态进行涂色
* 底色为白色,活跃生命为黑色
*/ package game_life_self; import java.awt.Color; import javax.swing.JPanel; public class set_color {
static boolean [][] state_one;
static JPanel[][] jPanel;
//初始化类
public set_color(boolean [][] state,JPanel[][] jpanel_one)
{
state_one = state;
jPanel = jpanel_one;
}
//设置颜色
public static void paint()
{ //循环判断状态设置颜色
for(int i=1;i<state_one.length;i++)
{
for(int j=1;j<state_one[i].length-1;j++)
{
if (state_one[i][j]) {
jPanel[i][j].setBackground(Color.black);
}
else{
jPanel[i][j].setBackground(Color.white);
}
}
}
}
}
 /*****************
* 构建窗口类
* 菜单choice(控制):1、开始 2、继续 3、结束
* 菜单pattern(模式):1、easy 2、love型 3、arrow箭头型 4、随机模式
* 菜单speed(速度):1、100 2、1000 3、5000
* 菜单help(帮助):1、abstract(游戏规则介绍)2、editor(制作者)
* 红色栏:1、Number of remaining lives(剩余生命个数)2、step:生命演化步数
* 表格栏 30*30
*/ package game_life_self; import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern; import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.FlowLayout; public class set_frame {
JFrame iFrame;
//游戏迭代速度
static int speed = 1000;
//剩余生命个数
static int life = 0;
//演化步数
static int step_one = 0;
myThread thread = null;
static start_frame start_frame;
//显示细胞状态面板
static JPanel[][] jPanel;
//模式一(love)
static int pattern = 1;
//显示生命个数面板
static JPanel panel_1;
static JLabel number;
static JLabel step;
static boolean end = true;
public set_frame (int row ,int col) {
//建立窗口
iFrame = new JFrame("the game of life");
//初始化边界
jPanel = new JPanel[row][col];
iFrame.getContentPane().setLayout(new BorderLayout(0, 0)); JPanel panel = new JPanel();
iFrame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); number = new JLabel("Number of remaining lives: "+life+" ");
panel.add(number); step = new JLabel("step: "+step_one);
panel.add(step); panel.setBackground(Color.red);
panel_1 = new JPanel();
iFrame.getContentPane().add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new GridLayout(30, 30, 2, 2));
//初始化所有界面为白色
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
jPanel[i][j] = new JPanel();
jPanel[i][j].setBackground(Color.white);
panel_1.add(jPanel[i][j]);
}
} //设置图形界面大小
iFrame.setLocation(450, 180);
iFrame.setSize(500, 500); JMenuBar menuBar = new JMenuBar();
iFrame.setJMenuBar(menuBar); JMenu choice = new JMenu("choice");
menuBar.add(choice); JMenuItem start = new JMenuItem("start");
choice.add(start);
start.addActionListener(new start()); JMenuItem continue_one = new JMenuItem("continue");
choice.add(continue_one);
continue_one.addActionListener(new continue_one()); JMenuItem stop = new JMenuItem("stop");
choice.add(stop);
stop.addActionListener(new stop()); JMenu pattern = new JMenu("pattern");
menuBar.add(pattern); JMenuItem easy = new JMenuItem("easy");
pattern.add(easy);
easy.addActionListener(new easy()); JMenuItem love = new JMenuItem("love");
pattern.add(love);
love.addActionListener(new love()); JMenuItem arrow = new JMenuItem("arrow");
pattern.add(arrow);
arrow.addActionListener(new arrow()); JMenuItem random = new JMenuItem("random");
pattern.add(random);
random.addActionListener(new random()); JMenu speed = new JMenu("speed");
menuBar.add(speed); JMenuItem speed_fast = new JMenuItem("100");
speed.add(speed_fast);
speed_fast.addActionListener(new speed_fast()); JMenuItem speed_middle = new JMenuItem("1000");
speed.add(speed_middle);
speed_middle.addActionListener(new speed_middle()); JMenuItem speed_low = new JMenuItem("5000");
speed.add(speed_low);
speed_low.addActionListener(new speed_low()); JMenu help = new JMenu("help");
menuBar.add(help); JMenuItem rule = new JMenuItem("abstract");
help.add(rule);
rule.addActionListener(new rule()); JMenuItem editor = new JMenuItem("editor");
help.add(editor);
editor.addActionListener(new editor()); iFrame.setVisible(true); } class myThread extends Thread{
public myThread() {
}
public void run(){
while(end)
{
life = 0;
start_frame.judge.judge();
try {
sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} for (int m = 1; m < start_frame.state_one.length - 1; m++)
{
for (int n = 1; n < start_frame.state_one[m].length - 1; n++)
{
if (start_frame.state_one[m][n]==true) {
life ++;
}
}
}
step_one++;
number.setText("Number of remaining lives: "+life+" ");
step.setText("step: "+step_one); start_frame.set_color.paint(); if (life==0) {
end = false;
JOptionPane.showMessageDialog(null, "生命演化结束:\n"
+ " 所用步数为"+step_one);
} }
}
} //控制游戏的开始
class start implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//初始化逻辑地图
if (pattern==1) {
start_frame.init_data.init_one();
}else if (pattern==2) {
start_frame.init_data.init_two();
}else if (pattern==3) { start_frame.init_data.init_three();
}else {
start_frame.init_data.init_zero();
}
//更新地图颜色
start_frame.set_color.paint();
//初始化步数和剩余生命个数
life = 0;
step_one = 0;
end = true;
//控制线程的开断
if (thread != null)
thread.stop();
thread = new myThread();
thread.start();
}
}
//控制游戏的继续
class continue_one implements ActionListener
{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(thread!=null)
thread.stop();
thread = new myThread();
thread.start();
} }
//控制游戏的停止
class stop implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//控制线程的开断
if (thread != null)
thread.stop();
thread = null;
} }
//设置生命迭代速度(快速)
class speed_fast implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed = 100;
}
}
//设置生命迭代速度(中速)
class speed_middle implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed = 1000;
}
}
//设置生命迭代速度(慢速)
class speed_low implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed = 5000;
}
}
//游戏规则介绍
class rule implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, " The rules of the life game \n"
+ "Each cell's life and death follows the following principles: \n"
+ " One: If a cell has three living cells (a total of eight cells around a cell), then the living cells (i.e. if the cells had to die, then born into, if for a living, original is unchanged). \n"
+ " Two: If two cells live around a cell, the cell's life and death status remains unchanged.)\n"
+ " Three: In other cases, the cell is dead (that is, if the cell had previously lived, it would have died and if it had been dead, it would remain the same)\n"+"\n");
}
}
//程序编者
class editor implements ActionListener
{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "this game's editor:\n"
+ " The software 1501 class(软件1501班) LiuHui(刘辉) \n");
}
}
//模式中 love型
class love implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pattern = 1;
} }
//模式中 箭头型
class arrow implements ActionListener
{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pattern = 2;
} }
//随机模式
class random implements ActionListener
{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pattern = 3;
} }
//随机模式
class easy implements ActionListener
{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pattern = 0;
} }
}
 

程序运行结果:

2.1  I型细胞模型

初始I型的细胞状态:

实用---生命游戏 Java-LMLPHP

细胞演化后的结果为:

实用---生命游戏 Java-LMLPHP

从图中可以看出在演化5步后,细胞的剩余数为0,演化完毕。

2.2  LOVE细胞模型

初始LOVE型的细胞状态:

实用---生命游戏 Java-LMLPHP

细胞演化后的结果为:

实用---生命游戏 Java-LMLPHP

从图中可以看出在演化131步后,细胞的剩余数为11,保持恒定不变的状态,自此演化完毕。

2.3 箭头型细胞模型

初始箭头细胞的状态:

实用---生命游戏 Java-LMLPHP

细胞演化后的结果为:

实用---生命游戏 Java-LMLPHP

从图中可以看出在演化151步后,细胞的剩余数为15,保持恒定不变的状态,自此演化完毕。

2.4 随机细胞模型

初始随机细胞状态:

实用---生命游戏 Java-LMLPHP

细胞演化后的结果为:

实用---生命游戏 Java-LMLPHP

从图中可以看出在演化520步后,细胞的剩余数为22,保持恒定不变的状态,自此演化完毕。

05-11 22:52