-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCACell.java
More file actions
119 lines (94 loc) · 3.1 KB
/
Copy pathCACell.java
File metadata and controls
119 lines (94 loc) · 3.1 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
package edu.neu.csye6200.ca;
import java.util.logging.Logger;
/*
* @author: Dileep Reddy
* NUID: 001063317
* Class Description: CACell has individual properties of each cell like its row position column position rule
* It has a the logic to calculate the neighbour cells
*/
enum CACellState {
FROZEN, LIQUID, VAPOUR;
}
public abstract class CACell {
private int cellRowPos;
private int cellColPos;
private CACellState cellState;
protected CACrystal crystal;
private static Logger log = null;
/*
* Default constructor
*/
public CACell() {
}
/*
* Overloaded constructor
*/
public CACell(CACrystal crystal, CACellState cellState) {
log = Logger.getLogger(CACell.class.getName());
this.crystal = crystal;
this.cellState = cellState;
}
// CA Rule will implement this abstract method
public abstract CACellState getNextCellState();
//setter method for setting the cell state
protected void setState(CACellState state) {
cellState = state;
}
// returns the neighbors count for hexagonal grid
protected int getDesiredNeighborsCount(CACellState desiredState) {
int desiredNeighborCount = 0;
try {
int colStartPos,ColEndPos;
for (int rowCounter = -1; rowCounter < 2; rowCounter++) {
colStartPos = rowCounter == 0 ? -1 : (cellRowPos %2 == 0? -1 : 0); // Neighbor Rows and columns position changes for hexagon for even and odd rows
ColEndPos = rowCounter == 0? 3 : 2; // column positions differ for odd and even columns
for (int colCounter = colStartPos; colCounter < colStartPos+ColEndPos; colCounter++) {
if(isDesiredNeighbour(rowCounter,colCounter,desiredState))
desiredNeighborCount++;
}
}
} catch (Exception e) {
log.severe("Exception occured while getting Neighbor Count : " + e.toString());
desiredNeighborCount = 0;
}
return desiredNeighborCount;
}
// return true if the it is the desired neighbour
private boolean isDesiredNeighbour(int rowCounter,int colCounter,CACellState desiredState)
{
if (cellRowPos + rowCounter >= 0 && cellRowPos + rowCounter < getCrystal().getCrystalRows()
&& cellColPos + colCounter >= 0 && cellColPos + colCounter < getCrystal().getCrystalColumns()) {
if (getCrystal().getCellAt(cellRowPos + rowCounter, cellColPos + colCounter).getCellState()
.compareTo(desiredState) == 0) {
if (!(rowCounter == 0 && colCounter == 0)) // should not consider the current cell as neighbor
return true;
}
}
return false;
}
// Getters and Setters
public CACellState getCellState() {
return cellState;
}
public void setCellState(CACellState cellState) {
this.cellState = cellState;
}
public CACrystal getCrystal() {
return crystal;
}
public void setCrystal(CACrystal crystal) {
this.crystal = crystal;
}
public int getCellRowPos() {
return cellRowPos;
}
public void setCellRowPos(int cellRowPos) {
this.cellRowPos = cellRowPos;
}
public int getCellColPos() {
return cellColPos;
}
public void setCellColPos(int cellColPos) {
this.cellColPos = cellColPos;
}
}