-
Notifications
You must be signed in to change notification settings - Fork 2
/
LifeCell.java
executable file
·134 lines (130 loc) · 2.77 KB
/
LifeCell.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
/**
* ライフゲームで使用するCell
* @author Atsuya Sato
*/
class LifeCell extends JButton{
//周囲のセル
private ArrayList<LifeCell> surroundings;
//現在の生存フラグ
private boolean isLiving = false;
//世代更新後の生存フラグ
private boolean willLiving = false;
/**
* Constructors
*/
public LifeCell(){
super("");
surroundings = new ArrayList<LifeCell>();
setLayout();
}
public LifeCell(Rectangle rect) {
super("");
this.setBounds(rect);
surroundings = new ArrayList<LifeCell>();
setLayout();
}
/**
* レイアウトの設定
*/
private void setLayout(){
this.setLayout(null);
this.setBackground(Color.white);
// these next two lines do the magic..
this.setContentAreaFilled(true);
this.setOpaque(true);
this.setBorderPainted(false);
LifeCell button = this;
this.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if(button.isLiving){
button.forceKill();
}else{
button.forceSpawn();
}
}
}
);
}
/**
* 周囲のセルを追加
* @param cell:LifeCell
*/
public void addSurroundings(LifeCell cell){
surroundings.add(cell);
}
/**
* 世代交代を行う
*/
public void generationalChange(){
isLiving = willLiving;
willLiving = false;
if(isLiving){
setBackground(Color.black);
}else{
setBackground(Color.white);
}
}
/**
* 周りのセルを調べて、世代交代の準備
*/
public void checkSurroundings(){
int cnt = 0;
for(LifeCell cell : surroundings){
if(cell.isLiving) cnt++;
}
//誕生
if(!isLiving){
//生きているセルが3つ
if(cnt == Const.BIRTH_CNT){
this.willLiving = true;
}
return;
}
//クロージャ内で使用したいので、定数として再定義
if(isLiving){
//生存
if(Const.LIVING_RANGE.includes(cnt)){
this.willLiving = true;
}
//過疎
if(Const.LIVING_RANGE.lowerBound > cnt){
this.willLiving = false;
}
//過密
if(Const.LIVING_RANGE.upperBound < cnt){
this.willLiving = false;
}
}
}
/**
* ボタンを押して生成する場合
*/
public void forceSpawn(){
isLiving = true;
this.setBackground(Color.black);
}
/**
* ボタンを押してセルを消す場合
*/
public void forceKill(){
isLiving = false;
this.setBackground(Color.white);
}
/**
* 盤面初期化用
*/
public static void forceKillAll(ArrayList<LifeCell> cells){
for(LifeCell cell : cells){
cell.isLiving = false;
cell.willLiving = false;
cell.setBackground(Color.white);
}
}
}