-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
84 lines (62 loc) · 1.85 KB
/
Copy pathWorld.java
File metadata and controls
84 lines (62 loc) · 1.85 KB
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
public abstract class World implements Cloneable {
int generation = 0;
Pattern pattern;
public World(String format) throws PatternFormatException {
pattern = new Pattern(format);
}
public World(Pattern pattern) {
this.pattern = pattern;
}
public int getWidth() {
return pattern.getWidth();
}
public int getHeight() {
return pattern.getHeight();
}
public int getGenerationCount() {
return this.generation;
}
protected void incrementGenerationCount() {
this.generation++;
}
protected Pattern getPattern() {
return pattern;
}
public void nextGeneration() {
nextGenerationImpl();
incrementGenerationCount();
}
public abstract World clone() throws CloneNotSupportedException;
protected abstract void nextGenerationImpl();
public abstract boolean getCell(int c, int r);
public abstract void setCell(int col, int row, boolean val);
protected int countNeighbours(int col, int row) {
int count = 0;
if (getCell(col + 1, row + 1))
count++;
if (getCell(col, row + 1))
count++;
if (getCell(col - 1, row + 1))
count++;
if (getCell(col - 1, row - 1))
count++;
if (getCell(col, row - 1))
count++;
if (getCell(col + 1, row - 1))
count++;
if (getCell(col + 1, row))
count++;
if (getCell(col - 1, row))
count++;
return count;
}
protected boolean computeCell(int col, int row) {
if (!getCell(col, row) && countNeighbours(col, row) == 3) {
return true;
}
if (getCell(col, row) && countNeighbours(col, row) == 2 || countNeighbours(col, row) == 3) {
return true;
}
return false;
}
}