生命游戏 Java

Posted 刘辉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生命游戏 Java相关的知识,希望对你有一定的参考价值。

本程序由四个类组成:

其中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 }