Repository navigation
Expand file tree
/
Copy pathPackedWorld.java
More file actions
96 lines (86 loc) · 2.43 KB
/
Copy pathPackedWorld.java
File metadata and controls
96 lines (86 loc) · 2.43 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
package uk.ac.cam.efxlb2.oop.tick5;
import java.io.IOException;
public class PackedWorld extends World implements Cloneable{
private long world;
@Override
public Object clone() throws CloneNotSupportedException{
PackedWorld cloned = (PackedWorld) super.clone();
cloned.world=0L;
for (int i=0;i<this.getHeight();i++) {
for (int j=0;j<this.getWidth();j++) {
cloned.setCell(i,j,getCell(i,j));
}
}
return cloned;
}
public PackedWorld(World pw) {
super(pw);//possibly covariant parameter types
world=0L;
for (int i=0;i<this.getHeight();i++) {
for (int j=0;j<this.getWidth();j++) {
setCell(i,j,pw.getCell(i,j));
}
}
}
public PackedWorld(String format) throws Exception,PatternFormatException {
super(format);
String[] splitarray = format.split(":");
world=0L;
try{
if(getHeight() * getWidth() > 64){
throw new Exception("Too long.");
}
getPattern().initialise(this);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public PackedWorld packedcopy(PackedWorld input) throws PatternFormatException {
PackedWorld output = new PackedWorld(input.getPattern());
return output;
}
public PackedWorld(Pattern pattern) throws PatternFormatException{
super(pattern);
world = 0L;
getPattern().initialise(this);
}
public boolean getCell(int row, int col) {
int position=col+row*getWidth();
return ((1 & (world>>position))==1);//modify the stuff
}
public void setCell(int row, int col, boolean val) {
if (((col<0)|(col>getWidth()))|((row<0)|(row>getHeight()))) System.out.println("Values need checking");
else {
int position=col+row*getWidth();
if (val==true) {
long modifyingagent1=(1L<<position);
world=(world|modifyingagent1);
}
else {
world=~world;
long modifyingagent=(1L<<position);
world=(world|modifyingagent);
world=~world;
}
}
}
public void nextGenerationImpl() {
long newWorld=0L;
for(int i = 0; i < getHeight(); i++) {//i is the row count
for(int j = 0; j < getWidth(); j++) {//j is column
int position=j+i*getWidth();
if (computeCell(j,i)) {
long modifyingagent1=(1L<<position);
newWorld=(newWorld|modifyingagent1);
}
else {
newWorld=~newWorld;
long modifyingagent=(1L<<position);
newWorld=(newWorld|modifyingagent);
newWorld=~newWorld;
}
}
}
world=newWorld;
}
}