本程序由四个类组成:
其中Init_data,用于初始化各个活细胞的状态
judge_state,用于判断下一代的细胞状态,并进行更新。
set_color,用于给GUI界面中各个细胞涂色
set_frame,用于设置GUI界面的布局
1 /*初始化细胞状态类 2 * 四个模式 3 * 1、easy模式,一个大写的I 4 * 2、love模式,一个大写的LOVE 5 * 3、arrow模式,一个粗箭头 6 * 4、random模式,根据随机函数生成活细胞数 7 */ 8 package game_life_self; 9 10 import java.util.Random; 11 12 public class Init_data{ 13 static boolean[][] state; 14 static int rand; 15 public Init_data(boolean[][] state_one) 16 { 17 state = state_one; 18 } 19 20 public void init_one() 21 { 22 for(int i=1;i<state.length;i++) 23 { 24 for(int j=1;j<state[i].length-1;j++) 25 { 26 state[i][j] = false; 27 } 28 } 29 30 //LOVE 31 //L 32 state[9][3] = true; 33 state[10][3] = true; 34 state[11][3] = true; 35 state[12][3] = true; 36 state[13][3] = true; 37 state[14][3] = true; 38 state[14][4] = true; 39 state[14][5] = true; 40 41 //O 42 state[9][8] = true; 43 state[10][7] = true; 44 state[11][7] = true; 45 state[12][7] = true; 46 state[13][7] = true; 47 state[14][8] = true; 48 state[10][9] = true; 49 state[11][9] = true; 50 state[12][9] = true; 51 state[13][9] = true; 52 53 //V 54 state[9][11] = true; 55 state[10][12] = true; 56 state[11][13] = true; 57 state[12][14] = true; 58 state[13][15] = true; 59 state[14][16] = true; 60 state[13][17] = true; 61 state[12][18] = true; 62 state[11][19] = true; 63 state[10][20] = true; 64 state[9][21] = true; 65 66 67 //E 68 state[9][23] = true; 69 state[9][24] = true; 70 state[9][25] = true; 71 state[9][26] = true; 72 state[10][23] = true; 73 state[11][23] = true; 74 state[12][23] = true; 75 state[12][24] = true; 76 state[12][25] = true; 77 state[12][26] = true; 78 state[13][23] = true; 79 state[14][23] = true; 80 state[15][23] = true; 81 state[15][24] = true; 82 state[15][25] = true; 83 state[15][26] = true; 84 85 86 } 87 88 public void init_two() 89 { 90 for(int i=1;i<state.length;i++) 91 { 92 for(int j=1;j<state[i].length-1;j++) 93 { 94 state[i][j] = false; 95 } 96 } 97 98 state[23][3] = true; 99 state[23][4] = true; 100 state[23][5] = true; 101 state[22][3] = true; 102 state[22][4] = true; 103 state[22][5] = true; 104 state[21][3] = true; 105 state[21][4] = true; 106 state[21][5] = true; 107 state[20][3] = true; 108 state[20][4] = true; 109 state[20][5] = true; 110 state[19][3] = true; 111 state[19][4] = true; 112 state[19][5] = true; 113 state[18][3] = true; 114 state[18][4] = true; 115 state[18][5] = true; 116 state[17][3] = true; 117 state[17][4] = true; 118 state[17][5] = true; 119 state[16][3] = true; 120 state[16][4] = true; 121 state[16][5] = true; 122 123 124 125 state[16][6] = true; 126 state[16][7] = true; 127 state[16][8] = true; 128 state[16][9] = true; 129 state[16][10] = true; 130 state[16][11] = true; 131 state[16][12] = true; 132 state[16][13] = true; 133 state[17][6] = true; 134 state[17][7] = true; 135 state[17][8] = true; 136 state[17][9] = true; 137 state[17][10] = true; 138 state[17][11] = true; 139 state[17][12] = true; 140 state[17][13] = true; 141 state[18][6] = true; 142 state[18][7] = true; 143 state[18][8] = true; 144 state[18][9] = true; 145 state[18][10] = true; 146 state[18][11] = true; 147 state[18][12] = true; 148 state[18][13] = true; 149 150 state[15][9] = true; 151 state[14][9] = true; 152 state[13][9] = true; 153 state[12][9] = true; 154 state[11][9] = true; 155 state[10][9] = true; 156 state[9][9] = true; 157 state[9][8] = true; 158 state[9][7] = true; 159 state[9][6] = true; 160 state[9][5] = true; 161 162 state[15][10] = true; 163 state[14][10] = true; 164 state[13][10] = true; 165 state[12][10] = true; 166 state[11][10] = true; 167 state[10][10] = true; 168 state[9][10] = true; 169 170 state[15][11] = true; 171 state[14][11] = true; 172 state[13][11] = true; 173 state[12][11] = true; 174 state[11][11] = true; 175 state[10][11] = true; 176 state[9][11] = true; 177 178 state[15][12] = true; 179 state[14][12] = true; 180 state[13][12] = true; 181 state[12][12] = true; 182 state[11][12] = true; 183 state[10][12] = true; 184 state[9][12] = true; 185 186 state[15][13] = true; 187 state[14][13] = true; 188 state[13][13] = true; 189 state[12][13] = true; 190 state[11][13] = true; 191 state[10][13] = true; 192 state[9][13] = true; 193 state[9][14] = true; 194 state[9][15] = true; 195 state[9][16] = true; 196 state[9][17] = true; 197 198 state[3][11] = true; 199 state[4][10] = true;state[4][11] = true;state[4][12] = true; 200 state[5][9] = true;state[5][10] = true;state[5][11] = true;state[5][12] = true;state[5][13] = true; 201 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; 202 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; 203 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; 204 205 } 206 207 public void init_three() 208 { 209 for(int i=1;i<state.length;i++) 210 { 211 for(int j=1;j<state[i].length-1;j++) 212 { 213 rand = (int) (Math.random()*100); 214 if (rand<50) { 215 state[i][j] = false; 216 } 217 else 218 state[i][j] = true; 219 } 220 } 221 } 222 public void init_zero() 223 { 224 for(int i=1;i<state.length;i++) 225 { 226 for(int j=1;j<state[i].length-1;j++) 227 { 228 state[i][j] = false; 229 } 230 } 231 state[9][3] = true; 232 state[9][4] = true; 233 state[9][6] = true; 234 state[9][7] = true; 235 state[9][5] = true; 236 state[10][5] = true; 237 state[11][5] = true; 238 state[12][5] = true; 239 state[13][5] = true; 240 state[14][5] = true; 241 state[14][4] = true; 242 state[14][6] = true; 243 state[14][3] = true; 244 state[14][7] = true; 245 246 } 247 }
1 /*主类 2 * 生命游戏开始入口 3 * editor: 4 * 软件1501 刘辉 5 * time:2017-9-7 6 */ 7 8 package game_life_self; 9 10 import javax.swing.Action; 11 import javax.swing.JFrame; 12 import javax.swing.JLabel; 13 import javax.swing.JPanel; 14 15 import java.awt.Color; 16 import java.awt.GridLayout; 17 import java.awt.Panel; 18 import java.awt.Window; 19 import java.awt.event.ActionEvent; 20 import java.awt.event.ActionListener; 21 22 import javax.swing.JMenuBar; 23 import javax.swing.JMenu; 24 import javax.swing.JMenuItem; 25 import javax.swing.JOptionPane; 26 27 public class start_frame extends Thread{ 28 static start_frame start; 29 static Init_data init_data; 30 static judge_state judge; 31 static set_color set_color; 32 static set_frame frame; 33 //存放细胞状态 34 static boolean[][] state_one; 35 static JPanel[][] jPanel; 36 //初始化GUI界面 37 public start_frame(int row,int col) 38 { 39 state_one = new boolean[row][col]; 40 frame = new set_frame(row,col); 41 jPanel = frame.jPanel; 42 } 43 44 //主函数 45 public static void main(String[] args) 46 { 47 //创建游戏对象 48 start = new start_frame(30,30); 49 init_data = new Init_data(state_one); 50 judge = new judge_state(state_one, jPanel); 51 set_color = new set_color(state_one, jPanel); 52 } 53 }
1 /*判断状态类 2 * 算法: 3 * 根据游戏规则判断中心细胞的下一步演化规则,并将状态计入state_one[][]中。 4 * nCount:周围活细胞的个数 5 */ 6 7 8 package game_life_self; 9 10 import java.awt.Color; 11 12 import javax.swing.JPanel; 13 14 public class judge_state { 15 static boolean [][] state_one; 16 JPanel[][] jPanel; 17 //初始化类 18 public judge_state(boolean [][] state,JPanel[][] jpanel_one) 19 { 20 state_one = state; 21 jPanel = jpanel_one; 22 } 23 24 //判断中心生命的状态并更新 25 public void judge() { 26 int life = 0; 27 for (int i = 1; i < state_one.length - 1; i++) { 28 for (int j = 1; j < state_one[i].length - 1; j++) { 29 //循环判断中心生命周围的各个点的状态 30 int nCount = 0; 31 if (jPanel[i - 1][j - 1].getBackground() == Color.black) { 32 nCount++; 33 } 34 if (jPanel[i - 1][j].getBackground() == Color.black) { 35 nCount++; 36 } 37 if (jPanel[i - 1][j + 1].getBackground() == Color.black) { 38 nCount++; 39 } 40 if (jPanel[i][j - 1].getBackground() == Color.black) { 41 nCount++; 42 } 43 if (jPanel[i][j + 1].getBackground() == Color.black) { 44 nCount++; 45 } 46 if (jPanel[i + 1][j - 1].getBackground() == Color.black) { 47 nCount++; 48 } 49 if (jPanel[i + 1][j].getBackground() == Color.black) { 50 nCount++; 51 } 52 if (jPanel[i + 1][j + 1].getBackground() == Color.black) { 53 nCount++; 54 } 55 if (nCount==3) { 56 state_one[i][j] = true; 57 } 58 else if (nCount == 2) { 59 state_one[i][j] = state_one[i][j]; 60 } 61 else 62 state_one[i][j] = false; 63 64 65 } 66 } 67 } 68 }
1 /*涂色类 2 * 根据state[][]中各个表格的状态进行涂色 3 * 底色为白色,活跃生命为黑色 4 */ 5 6 7 package game_life_self; 8 9 import java.awt.Color; 10 11 import javax.swing.JPanel; 12 13 public class set_color { 14 static boolean [][] state_one; 15 static JPanel[][] jPanel; 16 //初始化类 17 public set_color(boolean [][] state,JPanel[][] jpanel_one) 18 { 19 state_one = state; 20 jPanel = jpanel_one; 21 } 22 //设置颜色 23 public static void paint() 24 { 25 26 //循环判断状态设置颜色 27 for(int i=1;i<state_one.length;i++) 28 { 29 for(int j=1;j<state_one[i].length-1;j++) 30 { 31 if (state_one[i][j]) { 32 jPanel[i][j].setBackground(Color.black); 33 } 34 else{ 35 jPanel[i][j].setBackground(Color.white); 36 } 37 } 38 } 39 } 40 }
1 /***************** 2 * 构建窗口类 3 * 菜单choice(控制):1、开始 2、继续 3、结束 4 * 菜单pattern(模式):1、easy 2、love型 3、arrow箭头型 4、随机模式 5 * 菜单speed(速度):1、100 2、1000 3、5000 6 * 菜单help(帮助):1、abstract(游戏规则介绍)2、editor(制作者) 7 * 红色栏:1、Number of remaining lives(剩余生命个数)2、step:生命演化步数 8 * 表格栏 30*30 9 */ 10 11 package game_life_self; 12 13 import java.awt.BorderLayout; 14 import java.awt.Color; 15 import java.awt.GridLayout; 16 import java.awt.event.ActionEvent; 17 import java.awt.event.ActionListener; 18 import java.util.regex.Pattern; 19 20 import javax.swing.JFrame; 21 import javax.swing.JMenu; 22 import javax.swing.JMenuBar; 23 import javax.swing.JMenuItem; 24 import javax.swing.JOptionPane; 25 import javax.swing.JPanel; 26 import javax.swing.JLabel; 27 import java.awt.FlowLayout; 28 29 public class set_frame { 30 JFrame iFrame; 31 //游戏迭代速度 32 static int speed = 1000; 33 //剩余生命个数 34 static int life = 0; 35 //演化步数 36 static int step_one = 0; 37 myThread thread = null; 38 static start_frame start_frame; 39 //显示细胞状态面板 40 static JPanel[][] jPanel; 41 //模式一(love) 42 static int pattern = 1; 43 //显示生命个数面板 44 static JPanel panel_1; 45 static JLabel number; 46 static JLabel step; 47 static boolean end = true; 48 public set_frame (int row ,int col) { 49 //建立窗口 50 iFrame = new JFrame("the game of life"); 51 //初始化边界 52 jPanel = new JPanel[row][col]; 53 iFrame.getContentPane().setLayout(new BorderLayout(0, 0)); 54 55 JPanel panel = new JPanel(); 56 iFrame.getContentPane().add(panel, BorderLayout.NORTH); 57 panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 58 59 number = new JLabel("Number of remaining lives: "+life+" "); 60 panel.add(number); 61 62 step = new JLabel("step: "+step_one); 63 panel.add(step); 64 65 panel.setBackground(Color.red); 66 panel_1 = new JPanel(); 67 iFrame.getContentPane().add(panel_1, BorderLayout.CENTER); 68 panel_1.setLayout(new GridLayout(30, 30, 2, 2)); 69 //初始化所有界面为白色 70 for(int i=0;i<row;i++) 71 { 72 for(int j=0;j<col;j++) 73 { 74 jPanel[i][j] = new JPanel(); 75 jPanel[i][j].setBackground(Color.white); 76 panel_1.add(jPanel[i][j]); 77 } 78 } 79 80 //设置图形界面大小 81 iFrame.setLocation(450, 180); 82 iFrame.setSize(500, 500); 83 84 JMenuBar menuBar = new JMenuBar(); 85 iFrame.setJMenuBar(menuBar); 86 87 JMenu choice = new JMenu("choice"); 88 menuBar.add(choice); 89 90 JMenuItem start = new JMenuItem("start"); 91 choice.add(start); 92 start.addActionListener(new start()); 93 94 JMenuItem continue_one = new JMenuItem("continue"); 95 choice.add(continue_one); 96 continue_one.addActionListener(new continue_one()); 97 98 99 JMenuItem stop = new JMenuItem("stop"); 100 choice.add(stop); 101 stop.addActionListener(new stop()); 102 103 JMenu pattern = new JMenu("pattern"); 104 menuBar.add(pattern); 105 106 JMenuItem easy = new JMenuItem("easy"); 107 pattern.add(easy); 108 easy.addActionListener(new easy()); 109 110 JMenuItem love = new JMenuItem("love"); 111 pattern.add(love); 112 love.addActionListener(new love()); 113 114 JMenuItem arrow = new JMenuItem("arrow"); 115 pattern.add(arrow); 116 arrow.addActionListener(new arrow()); 117 118 JMenuItem random = new JMenuItem("random"); 119 pattern.add(random); 120 random.addActionListener(new random()); 121 122 123 JMenu speed = new JMenu("speed"); 124 menuBar.add(speed); 125 126 JMenuItem speed_fast = new JMenuItem("100"); 127 speed.add(speed_fast); 128 speed_fast.addActionListener(new speed_fast()); 129 130 JMenuItem speed_middle = new JMenuItem("1000"); 131 speed.add(speed_middle); 132 speed_middle.addActionListener(new speed_middle()); 133 134 JMenuItem speed_low = new JMenuItem("5000"); 135 speed.add(speed_low); 136 speed_low.addActionListener(new speed_low()); 137 138 JMenu help = new JMenu("help"); 139 menuBar.add(help); 140 141 JMenuItem rule = new JMenuItem("abstract"); 142 help.add(rule); 143 rule.addActionListener(new rule()); 144 145 JMenuItem editor = new JMenuItem("editor"); 146 help.add(editor); 147 editor.addActionListener(new editor()); 148 149 iFrame.setVisible(true); 150 151 } 152 153 class myThread extends Thread{ 154 public myThread() { 155 } 156 public void run(){ 157 while(end) 158 { 159 life = 0; 160 start_frame.judge.judge(); 161 try { 162 sleep(speed); 163 } catch (InterruptedException e) { 164 // TODO Auto-generated catch block 165 e.printStackTrace(); 166 } 167 168 for (int m = 1; m < start_frame.state_one.length - 1; m++) 169 { 170 for (int n = 1; n < start_frame.state_one[m].length - 1; n++) 171 { 172 if (start_frame.state_one[m][n]==true) { 173 life ++; 174 } 175 } 176 } 177 step_one++; 178 number.setText("Number of remaining lives: "+life+" "); 179 step.setText("step: "+step_one); 180 181 start_frame.set_color.paint(); 182 183 if (life==0) { 184 end = false; 185 JOptionPane.showMessageDialog(null, "生命演化结束:\n" 186 + " 所用步数为"+step_one); 187 } 188 189 } 190 } 191 } 192 193 //控制游戏的开始 194 class start implements ActionListener 195 { 196 @Override 197 public void actionPerformed(ActionEvent e) { 198 // TODO Auto-generated method stub 199 //初始化逻辑地图 200 if (pattern==1) { 201 start_frame.init_data.init_one(); 202 }else if (pattern==2) { 203 start_frame.init_data.init_two(); 204 }else if (pattern==3) { 205 206 start_frame.init_data.init_three(); 207 }else { 208 start_frame.init_data.init_zero(); 209 } 210 //更新地图颜色 211 start_frame.set_color.paint(); 212 //初始化步数和剩余生命个数 213 life = 0; 214 step_one = 0; 215 end = true; 216 //控制线程的开断 217 if (thread != null) 218 thread.stop(); 219 thread = new myThread(); 220 thread.start(); 221 } 222 } 223 //控制游戏的继续 224 class continue_one implements ActionListener 225 { 226 227 @Override 228 public void actionPerformed(ActionEvent e) { 229 // TODO Auto-generated method stub 230 if(thread!=null) 231 thread.stop(); 232 thread = new myThread(); 233 thread.start(); 234 } 235 236 } 237 //控制游戏的停止 238 class stop implements ActionListener 239 { 240 @Override 241 public void actionPerformed(ActionEvent e) { 242 // TODO Auto-generated method stub 243 //控制线程的开断 244 if (thread != null) 245 thread.stop(); 246 thread = null; 247 } 248 249 } 250 //设置生命迭代速度(快速) 251 class speed_fast implements ActionListener 252 { 253 @Override 254 public void actionPerformed(ActionEvent e) { 255 // TODO Auto-generated method stub 256 speed = 100; 257 } 258 } 259 //设置生命迭代速度(中速) 260 class speed_middle implements ActionListener 261 { 262 @Override 263 public void actionPerformed(ActionEvent e) { 264 // TODO Auto-generated method stub 265 speed = 1000; 266 } 267 } 268 //设置生命迭代速度(慢速) 269 class speed_low implements ActionListener 270 { 271 @Override 272 public void actionPerformed(ActionEvent e) { 273 // TODO Auto-generated method stub 274 speed = 5000; 275 } 276 } 277 //游戏规则介绍 278 class rule implements ActionListener 279 { 280 @Override 281 public void actionPerformed(ActionEvent e) { 282 // TODO Auto-generated method stub 283 JOptionPane.showMessageDialog(null, " The rules of the life game \n" 284 + "Each cell's life and death follows the following principles: \n" 285 + " 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" 286 + " Two: If two cells live around a cell, the cell's life and death status remains unchanged.)\n" 287 + " 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"); 288 } 289 } 290 //程序编者 291 class editor implements ActionListener 292 { 293 294 @Override 295 public void actionPerformed(ActionEvent e) { 296 // TODO Auto-generated method stub 297 JOptionPane.showMessageDialog(null, "this game's editor:\n" 298 + " The software 1501 class(软件1501班) LiuHui(刘辉) \n"); 299 } 300 } 301 //模式中 love型 302 class love implements ActionListener 303 { 304 @Override 305 public void actionPerformed(ActionEvent e) { 306 // TODO Auto-generated method stub 307 pattern = 1; 308 } 309 310 } 311 //模式中 箭头型 312 class arrow implements ActionListener 313 { 314 315 @Override 316 public void actionPerformed(ActionEvent e) { 317 // TODO Auto-generated method stub 318 pattern = 2; 319 } 320 321 } 322 //随机模式 323 class random implements ActionListener 324 { 325 326 @Override 327 public void actionPerformed(ActionEvent e) { 328 // TODO Auto-generated method stub 329 pattern = 3; 330 } 331 332 } 333 //随机模式 334 class easy implements ActionListener 335 { 336 337 @Override 338 public void actionPerformed(ActionEvent e) { 339 // TODO Auto-generated method stub 340 pattern = 0; 341 } 342 343 } 344 }
程序运行结果:
2.1 I型细胞模型
初始I型的细胞状态:
细胞演化后的结果为:
从图中可以看出在演化5步后,细胞的剩余数为0,演化完毕。
2.2 LOVE细胞模型
初始LOVE型的细胞状态:
细胞演化后的结果为:
从图中可以看出在演化131步后,细胞的剩余数为11,保持恒定不变的状态,自此演化完毕。
2.3 箭头型细胞模型
初始箭头细胞的状态:
细胞演化后的结果为:
从图中可以看出在演化151步后,细胞的剩余数为15,保持恒定不变的状态,自此演化完毕。
2.4 随机细胞模型
初始随机细胞状态:
细胞演化后的结果为:
从图中可以看出在演化520步后,细胞的剩余数为22,保持恒定不变的状态,自此演化完毕。