-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayWorld.java
More file actions
113 lines (99 loc) · 3.06 KB
/
Copy pathArrayWorld.java
File metadata and controls
113 lines (99 loc) · 3.06 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
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
package uk.ac.cam.efxlb2.oop.tick5;
public class ArrayWorld extends World implements Cloneable{
private boolean[][] world;
private boolean[] deadRow;
public boolean[] getdeadRow() {
return deadRow;
}
@Override
public Object clone() throws CloneNotSupportedException{ //cloning constructor
ArrayWorld cloned = (ArrayWorld) super.clone();
cloned.deadRow = this.getdeadRow();
cloned.world=new boolean[this.getHeight()][this.getWidth()];
int counter=0;
for (int i=0;i<this.getHeight();i++) {
for (int j=0;j<this.getWidth();j++) {
cloned.setCell(i,j,getCell(i,j));
if (getCell(i,j)==true) {counter++;}
}
if (counter==0) {
cloned.world[i]=deadRow;
}
counter=0;
}
return cloned;
}
public ArrayWorld(ArrayWorld aw) {//cloning constructor
super(aw);//possibly covariant parameter types
deadRow=aw.getdeadRow();
world=new boolean[this.getHeight()][this.getWidth()];
int counter=0;
for (int i=0;i<this.getHeight();i++) {
for (int j=0;j<this.getWidth();j++) {
setCell(i,j,aw.getCell(i,j));
if (aw.getCell(i,j)==true) {counter++;}
}
if (counter==0) {
world[i]=deadRow;
}
counter=0;
}
}
public ArrayWorld(String format) throws PatternFormatException{ //constructor
super(format);
deadRow = new boolean[this.getWidth()];
world =new boolean[this.getHeight()][this.getWidth()];
getPattern().initialise(this);
int counter=0;
for (int i=0;i<this.getHeight();i++) {
for (int j=0;j<this.getWidth();j++) {
if (this.getCell(i,j)==true) {counter++;}
}
if (counter==0) {
world[i]=deadRow;
}
counter=0;
}
}
public ArrayWorld(Pattern pattern) throws PatternFormatException{//constructor
super(pattern);
deadRow = new boolean[this.getWidth()];
world = new boolean[this.getHeight()][this.getWidth()];
getPattern().initialise(this);
int counter=0;
for (int i=0;i<this.getHeight();i++) {
for (int j=0;j<this.getWidth();j++) {
if (this.getCell(i,j)==true) {counter++;}
}
if (counter==0) {
world[i]=deadRow;
}
counter=0;
}
}
public boolean getCell(int row, int col) {
if (row<0||row>=this.getHeight()) return false;
if (col<0||col>=this.getWidth()) return false;
return world[row][col];
}
public ArrayWorld arraycopy(ArrayWorld input) throws PatternFormatException {
ArrayWorld output = new ArrayWorld(input.getPattern());
//need to copy some state - what state?
return output;
}
public void setCell(int row, int col, boolean newval) {
if (row<0||row>=this.getHeight()) System.out.println("Values need checking");
if (col<0||col>=this.getWidth()) System.out.println("Values need checking");
world[row][col]=newval;
}
public void nextGenerationImpl() {
boolean[][] World=new boolean[this.getHeight()][];
for(int i = 0; i < this.getHeight(); i++) {
World[i] = new boolean[this.getWidth()];
for(int j = 0; j < this.getWidth(); j++) {
World[i][j] = computeCell(i,j);
}
}
world=World; //loops back to the main one
}
}