-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oscillator.java
115 lines (94 loc) · 3.19 KB
/
Oscillator.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
/* CS 230 Final project: All Cells Must Die - Game of Life
* Author: Cassandra Zheng, Fiona Fan, Candice Gong
* File: Oscillator.java
* Written together by Candice
*
* This file is for oscillators
*/
import java.lang.Exception.*;
import java.util.*;
public class Oscillator extends Shape
{
//constructors
public Oscillator (String name, boolean[][] shape, int width, int height)
{
super(name, shape, width, height);
//automatically generate all of the oscillator's morphisms
setFullPeriod();
ifOscillator=true;
}
public Oscillator (boolean[][] shape, int width, int height)
{
super(shape, width, height);
setFullPeriod();
ifOscillator=true;
}
//add the next period to the oscillator's periods
public void addPeriod(boolean[][] nextPeriod){
periods.add(nextPeriod);
}
//add the next period to the oscillator's periods, with the index of which period it is
public void addPeriod(int index, boolean[][] period){
periods.add(index, period);
}
//set all periods of the oscilator, @return a linked list of full boolean arrays
public LinkedList<boolean[][]> fullPeriod() throws InputNotOscillatorException {
LinkedList<boolean[][]> result = new LinkedList<boolean[][]>();
//add the current shape to the result
result.add(shape);
boolean[][] nextPeriod;
int i = 1;
do{
if( i == STOP_CYCLE_INDEX ) { // if after a number of times the shape is still not in its original form, then it's not an oscillator
throw new InputNotOscillatorException ("An oscillator should repeat its first period");
}
//get next period pattern
nextPeriod = generatePeriod(result.getLast());
//add it to linked list
result.add(nextPeriod);
i++;
}while(!Arrays.deepEquals(nextPeriod, result.getFirst()));
result.removeLast();
return result;
}
public void setFullPeriod(){
periods = fullPeriod();
}
//generate the next period
public static boolean[][] generatePeriod(boolean[][] currentPeriod){
boolean[][] result = new boolean[currentPeriod.length][currentPeriod.length];
for (int i = 0; i < currentPeriod.length; i ++) {
for (int j = 0; j < currentPeriod.length; j ++) {
result[i][j] = Helper.statusInNextGen(i, j, currentPeriod);
}
}
return result;
}
//set the shape to be the next generation morphism
public void nextGeneration(){
shape = generatePeriod(shape);
}
//get the next period morphism
public void nextPeriod(){
int currentIndex = super.getCurrentPeriodNum();
try{
shape = periods.get(currentIndex+1);
}catch(IndexOutOfBoundsException e){
shape = periods.getFirst();
}
}
//toString method
public String toString(){
String result = "";//NAME + ": ";
//System.out.println("The current shape:\n" + booleanArrayToString(shape));
int startingPoint = super.getCurrentPeriodNum();
//System.out.println("the starting point is: " + startingPoint);
do
{
result += ("(Period " + (getCurrentPeriodNum()+1) + ") " + super.toString());
nextPeriod();
}
while (getCurrentPeriodNum() != startingPoint);
return result;
}
}