-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCellPattern.java
executable file
·69 lines (67 loc) · 1.73 KB
/
CellPattern.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
/**
* プリセットとして提供するセルパターンをまとめたクラス
* @author Atsuya Sato
*/
public class CellPattern{
enum Pattern { GLIDER, SPACESHIP, GALAXY }
//グライダーパターン
private final static int[][] glider_ptn = {
{0,1,0},
{0,0,1},
{1,1,1}
};
//宇宙船パターン
private final static int[][] spaceship_ptn = {
{1,0,0,1,0},
{0,0,0,0,1},
{1,0,0,0,1},
{0,1,1,1,1}
};
//銀河パターン
private final static int[][] galaxy_ptn = {
{1,1,1,1,1,1,0,1,1},
{1,1,1,1,1,1,0,1,1},
{0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0},
{1,1,0,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1,1}
};
/**
* パターンの生成
* @param cells 盤面の二重配列
* @param p パターンの指定
*/
public static void patternGenerator(LifeCell[][] cells,CellPattern.Pattern p){
int pattern[][] = {{}};
switch(p){
case GLIDER:
pattern = glider_ptn;
break;
case SPACESHIP:
pattern = spaceship_ptn;
break;
case GALAXY:
pattern = galaxy_ptn;
break;
}
int x_max = cells[0].length;
int y_max = cells.length;
//盤面の大きさより、パターンの生成領域超えていた場合
if(Const.sponeLocation.width + pattern[0].length > x_max){ return; }
if(Const.sponeLocation.height + pattern.length > y_max){ return; }
//パターンに合わせて、強制的に盤面塗り替え
for(int y = 0; y < pattern.length; y++){
for(int x = 0; x < pattern[0].length; x++){
LifeCell cell = cells[Const.sponeLocation.height + y][Const.sponeLocation.width + x];
if(pattern[y][x] == 1){
cell.forceSpawn();
}else{
cell.forceKill();
}
}
}
}
}