-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCACrystal.java
More file actions
185 lines (165 loc) · 6.42 KB
/
Copy pathCACrystal.java
File metadata and controls
185 lines (165 loc) · 6.42 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package edu.neu.csye6200.ca;
import java.util.Random;
/*
* @author: Dileep Reddy
* NUID: 001063317
* Class Description: CACrystal holds the 2D array of CACells and also initializes the crystal based on the rule selected
* It also fetches next crystals based on next cell states in CA Rule class
*/
public class CACrystal {
private int crystalRows,crystalColumns;
private RuleNames ruleName;
protected CACell[][] arrCells;
CACell caFrozen = new CARule(this.ruleName, this, CACellState.FROZEN);
CACell caLiquid = new CARule(this.ruleName, this, CACellState.LIQUID);
public CACrystal(RuleNames rule, int crystalRows, int crystalColumns) {
this.ruleName = rule;
this.crystalRows = crystalRows;
this.crystalColumns = crystalColumns;
this.arrCells = new CACell[crystalRows][crystalColumns];
regionInitialize();
}
public CACrystal(CACrystal previousCrystal) {
ruleName = previousCrystal.ruleName;
crystalRows = previousCrystal.crystalRows;
crystalColumns = previousCrystal.crystalColumns;
arrCells = new CACell[crystalRows][crystalColumns];
regionInitialize();
}
/*initializes the entire region with basic rule Vapour assuming all the cells around us are in vapour state
*/
private void regionInitialize() {
for (int row = 0; row < crystalRows; row++) {
for (int col = 0; col < crystalColumns; col++) {
CACell ca = new CARule(this.ruleName, this, CACellState.VAPOUR);
ca.setCellRowPos(row);
ca.setCellColPos(col);
ca.setCrystal(this);
this.arrCells[row][col] = ca;
}
}
createInitialCrystal();
}
/*creates different initial crystals for different rules
*47 - center 62 - center column */
private void createInitialCrystal() {
CACell caFrozen = new CARule(this.ruleName, this, CACellState.FROZEN);
CACell caLiquid = new CARule(this.ruleName, this, CACellState.LIQUID);
int row = getCrystalRows()/2;
int col = getCrystalColumns()/2;
if(this.ruleName == RuleNames.SingleCrystal) {
this.arrCells[row][col] = caFrozen;
}
else if(this.ruleName == RuleNames.RandomCrystals){
for(int i = 0; i < 15; i++) {
Random rand = new Random();
this.arrCells[rand.nextInt(94)][rand.nextInt(125)] = caFrozen;
this.arrCells[rand.nextInt(94)][rand.nextInt(125)] = caLiquid;
this.arrCells[rand.nextInt(94)][rand.nextInt(125)] = caLiquid;
this.arrCells[rand.nextInt(94)][rand.nextInt(125)] = caLiquid;
}
}
else if(this.ruleName == RuleNames.Butterfly){
this.arrCells[getCrystalRows() / 2][getCrystalColumns() / 2] = caFrozen;
this.arrCells[getCrystalRows()/2 - 2][getCrystalColumns()/2 - 2] = caFrozen;
this.arrCells[getCrystalRows()/2 - 2][getCrystalColumns()/2 + 2] = caFrozen;
this.arrCells[getCrystalRows()/2 + 2][getCrystalColumns()/2 - 2] = caFrozen;
this.arrCells[getCrystalRows()/2 + 2][getCrystalColumns()/2 + 2] = caFrozen;
this.arrCells[getCrystalRows()/2 -4 ][getCrystalColumns()/2 - 4] = caFrozen;
this.arrCells[getCrystalRows()/2 - 4 ][getCrystalColumns()/2 + 4] = caFrozen;
this.arrCells[getCrystalRows()/2 + 4][getCrystalColumns()/2 + 4] = caFrozen;
this.arrCells[getCrystalRows()/2 + 4][getCrystalColumns()/2 - 4] = caFrozen;
this.arrCells[getCrystalRows()/2 -4 ][getCrystalColumns()/2] = caLiquid;
this.arrCells[getCrystalRows()/2 - 2 ][getCrystalColumns()/2] = caLiquid;
this.arrCells[getCrystalRows()/2 + 2][getCrystalColumns()/2] = caLiquid;
this.arrCells[getCrystalRows()/2 + 4][getCrystalColumns()/2] = caLiquid;
this.arrCells[getCrystalRows()/2 ][getCrystalColumns()/2 + 2] = caLiquid;
this.arrCells[getCrystalRows()/2 ][getCrystalColumns()/2 + 4] = caLiquid;
this.arrCells[getCrystalRows()/2][getCrystalColumns()/2 -2 ] = caLiquid;
this.arrCells[getCrystalRows()/2][getCrystalColumns()/2 - 4] = caLiquid;
}
else if(this.ruleName == RuleNames.Highway){
for(int i = 0; i < getCrystalRows(); i++)
{
for(int j =0; j < getCrystalColumns(); j++) {
if((i == getCrystalRows()/2 && j %2 == 0) || (j== getCrystalColumns()/2 && i%2 == 1))
{
this.arrCells[i][j] = caLiquid;
}
}
}
for(int i = 0; i < 47; i = i+2) {
this.arrCells[47 - i][62 - i] = caFrozen;
this.arrCells[47 + i][62 + i] = caFrozen;
this.arrCells[47 - i][62 + i] = caFrozen;
this.arrCells[47 + i][62 - i] = caFrozen;
}
}
else if(this.ruleName == RuleNames.Garden){
for(int i = 0; i < getCrystalRows(); i++)
{
for(int j =0; j < getCrystalColumns(); j++) {
if((i%5 == 0) && (j % 4 == 0))
{
this.arrCells[i][j] = caLiquid;
}
if((i%4 == 0) && (j % 5 == 0))
{
this.arrCells[i][j] = caFrozen;
}
}
}
}
}
/* creates next crystals based previous crystal cell states */
public CACrystal createNextCrystal(int counter) {
CACrystal newCrystal = new CACrystal(this);
CACellState[][] newCellStates;
newCellStates = nextCellStates(counter);
for (int i = 0; i < getCrystalRows(); i++) {
for (int j = 0; j < getCrystalColumns(); j++) {
newCrystal.getCellAt(i, j).setState(newCellStates[i][j]);
}
}
return newCrystal;
}
/*returns CACell based on row and column value*/
public CACell getCellAt(int row, int col) {
if ((row < 0) || (row >= getCrystalRows())) {
throw new RuntimeException("The referenced cell at " + row + "is not valid row in the current region.");
}
if ((col < 0) || (col >= getCrystalColumns())) {
throw new RuntimeException("The referenced cell at " + col + "is not valid column in the current region.");
}
return arrCells[row][col];
}
/*gets nextCellStates based on selected rules*/
public CACellState[][] nextCellStates(int counter) {
CACellState[][] nextStates = new CACellState[getCrystalRows()][getCrystalColumns()];
for (int i = 0; i < getCrystalRows(); i++) {
for (int j = 0; j < getCrystalColumns(); j++) {
nextStates[i][j] = getCellAt(i, j).getNextCellState();
}
}
return nextStates;
}
/*Getters and Setters*/
public int getCrystalRows() {
return crystalRows;
}
public void setCrystalRows(int regionWidth) {
this.crystalRows = regionWidth;
}
public int getCrystalColumns() {
return crystalColumns;
}
public void setCrystalColumns(int regionHeight) {
this.crystalColumns = regionHeight;
}
public RuleNames getRuleName() {
return ruleName;
}
public void setRuleName(RuleNames ruleName) {
this.ruleName = ruleName;
}
}