-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayWorld.java
111 lines (98 loc) · 3.41 KB
/
ArrayWorld.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
import java.util.*;
public class ArrayWorld extends World {
private boolean[][] world;
private boolean[] deadRow;
//the modifier "final" does not make the array immutable,
//it makes the variable (which contains the reference to the array) constant
//This means that we can't store another array in that variable,
//but we can mutate the array which is already in that variable
public ArrayWorld(String serial) throws PatternFormatException {
super(serial);
this.world = new boolean[this.getHeight()][this.getWidth()];
this.deadRow = new boolean[this.getWidth()];
this.getPattern().initialise(this);
this.manageRows(this.world);
}
public ArrayWorld(Pattern pattern) throws PatternFormatException {
super(pattern);
this.world = new boolean[this.getHeight()][this.getWidth()];
this.deadRow = new boolean[this.getWidth()];
this.getPattern().initialise(this);
this.manageRows(this.world);
}
public ArrayWorld(ArrayWorld otherWorld) throws PatternFormatException {
super(otherWorld.getPattern());
this.world = new boolean[this.getHeight()][this.getWidth()];
this.deadRow = new boolean[this.getWidth()];
this.generation = otherWorld.getGenerationCount();
int width = otherWorld.getWidth();
int height = otherWorld.getHeight();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.setCell(j, i, otherWorld.getCell(j, i));
}
}
this.manageRows(this.world);
}
public ArrayWorld clone() throws CloneNotSupportedException {
ArrayWorld cloned = null;
try {
cloned = new ArrayWorld(this.getPattern());
} catch (PatternFormatException e) {
System.out.println(e.getMessage());
}
cloned.generation = this.getGenerationCount();
int width = this.getWidth();
int height = this.getHeight();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cloned.setCell(j, i, this.getCell(j, i));
}
}
this.manageRows(cloned.world);
return cloned;
}
public void manageRows(boolean[][] world){
boolean flag = false;
for (int i = 0; i < world.length; i++) {
flag = false;
for (int j = 0; j < world[i].length; j++) {
if (world[i][j])
flag = true;
}
if(!flag)
world[i] = this.deadRow;
}
}
public boolean getCell(int col, int row) {
if (row < 0 || row >= this.getHeight()) {
return false;
}
if (col < 0 || col >= this.getWidth()) {
return false;
}
return this.world[row][col];
}
public void setCell(int col, int row, boolean value) {
if (row < 0 || row >= getHeight()) {
return;
}
if (col < 0 || col >= getWidth()) {
return;
}
if(world[row] == this.deadRow && value){
world[row] = new boolean[world[row].length];
}
this.world[row][col] = value;
this.manageRows(this.world);
}
protected void nextGenerationImpl() {
boolean[][] newField = new boolean[this.getHeight()][this.getWidth()];
for (int i = 0; i < this.getHeight(); i++) {
for (int j = 0; j < this.getWidth(); j++) {
newField[i][j] = this.computeCell(j, i);
}
}
this.world = newField;
}
}