细胞生命游戏

细胞生命游戏

简介:
“人口过少”:任何活细胞如果活邻居少于2个,则死掉。
“正常”:任何活细胞如果活邻居为2个或3个,则继续活。
“人口过多”:任何活细胞如果活邻居大于3个,则死掉。
“繁殖”:任何死细胞如果活邻居正好是3个,则活过来。
这里假设领域为3*3的大小

利用java实现,三个类

Main.java

1
2
3
4
5
6
7
8
package life_game3;
import javax.swing.*;

public class Main {
public static void main(String []args) {
Paint ptPaint = new Paint();
}
}

Logic.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

public class Logic {
public static final int ROWS = 10;
public static final int COLS = 10;
public static final int WIDTH = 40;
public static final int HIGHT = 40;

private int map[][] = new int[][] {
{1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1},
{0,1,0,0,1,1,0,1,0,1},
{0,0,1,1,0,0,0,1,0,0},
{0,0,1,1,0,0,1,0,1,0},
{0,1,0,0,1,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1},
};

public Logic() {
;
}

public int[][] newmap(int [][] map){
// copy new 2d array
int [][] map0 = new int [ROWS][COLS];
for(int i=0;i<ROWS;i++) {
map0[i] =map[i].clone();
}

//go through the map
for(int i=0;i<ROWS;i++) {
for(int j=0;j<COLS;j++) {
int count =0;
if(i-1>=0 && j-1>=0 && map[i-1][j-1]==1) {count++;}
if(i-1>=0 && j>=0 && map[i-1][j]==1) {count++;}
if(i-1>=0 && j+1<COLS && map[i-1][j+1]==1) {count++;}

if(i>=0 && j-1>=0 && map[i][j-1]==1) {count++;}
if(i>=0 && j+1<COLS && map[i][j+1]==1) {count++;}

if(i+1<ROWS && j-1>=0 && map[i+1][j-1]==1) {count++;}
if(i+1<ROWS && j>=0 && map[i+1][j]==1) {count++;}
if(i+1<ROWS && j+1<COLS && map[i+1][j+1]==1) {count++;}

if(! (count==2 || count==3)) {
map0[i][j]=0;
}
if( count==3 ) {
map0[i][j]=1;
}


}
}
return map0;

}

public int[][] getMap(){
return map;
}

public void updateMap(){
this.map = newmap(map);
}
}

Paint.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package life_game3;
import javax.swing.*;
import java.awt.*;

public class Paint extends JPanel{
Logic gamelogic = new Logic();
int blank =0;

public Paint() {
JFrame frame = new JFrame("生命游戏");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setBounds(800, 400, 420,445);

new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}).start();
frame.setResizable(false);
frame.add(this);
frame.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
int map[][] = gamelogic.getMap();
gamelogic.updateMap();
g.setColor(Color.black);
for(int i=0;i<gamelogic.ROWS;i++) {
for(int j=0;j<gamelogic.COLS;j++) {
if(map[i][j]==1) {
g.fillRect( j*gamelogic.WIDTH+blank,i*gamelogic.HIGHT, gamelogic.WIDTH, gamelogic.HIGHT);
}
else if(map[i][j]==0){
g.drawRect(j*gamelogic.WIDTH+blank,i*gamelogic.HIGHT, gamelogic.WIDTH, gamelogic.HIGHT);
}
}
}
}
}

遇到的一个小问题

JFrame挡住了界面
解决方法:不要直接在JFrame中画,在JPannel中画,然后将其添加到JFrame里面去。

参考文章:paint定时刷新