-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.java
160 lines (139 loc) · 5.13 KB
/
Shape.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* CS 230 Final project: All Cells Must Die - Game of Life
* File: Shape.java
* Written together by Candice
*/
import java.util.LinkedList;
import java.util.Hashtable;
public class Shape
{
final protected String NAME;
final protected int STOP_CYCLE_INDEX = 100;
protected boolean[][] shape;
protected int width;
protected int height;
protected LinkedList<boolean[][]> periods;
protected boolean ifOscillator;
//constructor, input with name, shape and the sizes
public Shape(String name, boolean[][] shape, int width, int height)
{
this.NAME = name;
this.shape = shape;
this.width = width;
this.height = height;
periods = new LinkedList<boolean[][]>();
periods.add(shape);
}
//constructor, input with, shape and the sizes, used for search
public Shape(boolean[][] shape, int width, int height)
{
this.NAME = "";
this.shape = shape;
this.width = width;
this.height = height;
periods = new LinkedList<boolean[][]>();
periods.add(shape);
}
//getters and setters
public String getName(){
return NAME;
}
public int getWidth(){
return width;
}
public boolean getChar(){
return ifOscillator;
}
public int getTotalPeriodNum(){
return periods.size();
}
public int getCurrentPeriodNum(){
return periods.indexOf(shape);
}
public int getAliveCellNum() {
int count = 0;
for (int row = 0; row < shape.length; row ++){
for (int col = 0; col < shape[row].length; col ++){
if (shape[row][col]) count ++;
}
}
return count;
}
public boolean[][] getCurrentShape(){
return shape;
}
//generate next generation
public void nextGeneration(){
shape = Helper.generatePeriod(shape);
}
//a recursive method to check if the input shape match one in periods
public boolean isIdentical(Shape s){
//System.out.println(NAME);
for (boolean[][] b : periods) {
if (Helper.isIdentical(b, s.getCurrentShape())) return true;
}
return false;
}
//helper method
public static String booleanArrayToString (boolean[][] b) {
String result = "";
for (int row = 0; row < b.length; row ++){
for (int col = 0; col < b[row].length; col ++){
if (b[row][col]) result += (" x ");
else result += " - ";
}
result += "\n";
}
return result;
}
//toString method
public String toString() {
String result = NAME + ":\n";
result += booleanArrayToString (shape);
// for (int row = 0; row < shape.length; row ++){
// for (int col = 0; col < shape[row].length; col ++){
// if (shape[row][col]) result += (" x ");
// else result += " - ";
// }
// result += "\n";
// }
return result;
}
//for testing purposes
public static void main (String[] args){
boolean[][] shapeA = new boolean[2][2];
shapeA[0][0] = true;
shapeA[0][1] = true;
shapeA[1][0] = true;
shapeA[1][1] = true;
//Shape a = new Shape("Block", shapeA, 2, 2);
//System.out.println(a.toString());
//Shape b = new Shape("Test", new boolean[][] {{true, false},{true, true}}, 2, 2);
//System.out.println(b.toString());
Shape BLOCK = new Stabilizer("Block", new boolean[][] {{true, true},{true, true}}, 2, 2 );
Shape TUB = new Stabilizer("Tub", new boolean[][] {{false, true, false}, {true, false, true}, {false, true, false}}, 3, 3);
Shape BOAT = new Stabilizer("Boat", new boolean[][] {{true, true, false}, {true, false, true}, {false, true, false}}, 3, 3);
Shape BEEHIVE = new Stabilizer("Beehive", new boolean[][] {{false, true, true, false}, {true, false, false, true}, {false, true, true, false}}, 4, 4);
Shape EATER = new Stabilizer("Eater", new boolean[][] {{true, true, false, false}, {true, false, true, false}, {false, false, true, false}, {false, false, true, true}}, 4, 4);
Shape POND = new Stabilizer("Pond", new boolean[][] {{false, true, true, false}, {true, false, false, true}, {true, false, false, true}, {false, true, true, false}}, 4, 4);
// System.out.println(BLOCK.toString());
// System.out.println(TUB.toString());
// System.out.println(BOAT.toString());
// System.out.println(BEEHIVE.toString());
// System.out.println(EATER.toString());
// System.out.println(POND.toString());
// System.out.println(ShapeCollection.COLLECTION[6]);
// System.out.println(ShapeCollection.COLLECTION[8]);
// System.out.println(ShapeCollection.COLLECTION[9]);
//for(int i = 0; i < ShapeCollection.COLLECTION.length; i++){
//System.out.println(ShapeCollection.COLLECTION[i].toString());
//}
ShapeCollection sc = new ShapeCollection ();
Hashtable<Integer, LinkedList<Shape>> hasher = ShapeCollection.getHash();
LinkedList<Shape> list = ShapeCollection.hash.get(4);
boolean[][] blinker2ndStage = { {false, true, false}, {false, true, false}, {false, true, false}};
boolean[][] blinker1stStage = { {false, false, false}, {true, true, true}, {false, false, false}};
Shape test = new Shape (blinker1stStage, 3, 3);
// System.out.println(ShapeCollection.contains(test));
// System.out.println(ShapeCollection.getName(test));
}
}