-
Notifications
You must be signed in to change notification settings - Fork 3
/
CellPattern.java
executable file
·87 lines (83 loc) · 2.7 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* プリセットとして提供するセルパターンをまとめたクラス
* @author Atsuya Sato
*/
import java.util.Random;
public class CellPattern{
enum Pattern { GLIDER, SPACESHIP, GALAXY }
static Random rnd=new Random();
static int r = rnd.nextInt(3)+1;
//グライダーパターン
private final static int[][] glider_ptn = {
{r,1,r},
{0,0,r},
{r,r,r}
};
//宇宙船パターン
private final static int[][] spaceship_ptn = {
{1,1,3,2,1,2,3,2,1,2,4,4,1,4},
{2,0,0,0,0,0,0,0,0,0,0,0,0,2},
{3,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,3},
{3,0,0,0,0,0,0,0,0,0,0,0,0,3},
{2,0,0,0,0,0,0,0,0,0,0,0,0,4},
{2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{4,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,2},
{1,0,0,0,0,0,0,0,0,0,0,0,0,4},
{3,0,0,0,0,0,0,0,0,0,0,0,0,1},
{3,0,0,0,0,0,0,0,0,0,0,0,0,2},
{3,0,0,0,0,0,0,0,0,0,0,0,0,4},
{1,2,2,1,4,3,1,2,3,4,2,3,3,2}
};
//銀河パターン
private final static int[][] galaxy_ptn = {
{1,2,3,4,1,2,3,4,1,2,3,2,1,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,2},
{4,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,3},
{3,0,0,0,0,0,0,0,0,0,0,0,0,2},
{1,0,0,0,0,0,0,0,0,0,0,0,0,4},
{2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{4,0,0,0,0,0,0,0,0,0,0,0,0,3},
{3,0,0,0,0,0,0,0,0,0,0,0,0,2},
{1,0,0,0,0,0,0,0,0,0,0,0,0,4},
{2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{4,0,0,0,0,0,0,0,0,0,0,0,0,3},
{3,0,0,0,0,0,0,0,0,0,0,0,0,4},
{1,2,3,1,4,2,1,2,1,4,2,3,1,2}
};
/**
* パターンの生成
* @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();
}
}
}
}
}