-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTownCell.java
86 lines (67 loc) · 2.01 KB
/
TownCell.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
package isp;
/**
*
* @author Ethan Gruening
* The abstract class defining a Town Cell, a singular unit in the Town, representing all types of users.
*
*/
public abstract class TownCell {
protected Town plain;
protected int row;
protected int col;
// constants to be used as indices.
protected static final int RESELLER = 0;
protected static final int EMPTY = 1;
protected static final int CASUAL = 2;
protected static final int OUTAGE = 3;
protected static final int STREAMER = 4;
public static final int NUM_CELL_TYPE = 5;
//Use this static array to take census.
public static final int[] nCensus = new int[NUM_CELL_TYPE];
public TownCell(Town p, int r, int c) {
plain = p;
row = r;
col = c;
}
/**
* Checks all neighborhood cell types in the neighborhood.
* @param counts of all customers
*/
public void census(int nCensus[]) {
// zero the counts of all customers
nCensus[RESELLER] = 0;
nCensus[EMPTY] = 0;
nCensus[CASUAL] = 0;
nCensus[OUTAGE] = 0;
nCensus[STREAMER] = 0;
//goes through each neighbor in a 3x3 grid
for(int i = row - 1; i <= row+1; i++){
for (int j = col - 1; j <= col+1; j++) {
//breaks when it evaluates itself, or an out of index
if(((i == row) & (j == col)) || (i < 0) || (j < 0) || (i >= plain.getLength()) || (j >= plain.getWidth())){
continue;
}
//increments the count of the Census based on the cell's type
TownCell temp = plain.grid[i][j];
if(State.CASUAL == temp.who()){ nCensus[CASUAL]++;}
else if(State.EMPTY == temp.who()){nCensus[EMPTY]++;}
else if(State.OUTAGE == temp.who()){nCensus[OUTAGE]++;}
else if(State.STREAMER == temp.who()){nCensus[STREAMER]++;}
else if(State.RESELLER == temp.who()){nCensus[RESELLER]++;}
}
}
}
/**
* Gets the identity of the cell.
*
* @return State of the cell
*/
public abstract State who();
/**
* Determines the cell type in the next cycle.
*
* @param tNew: town of the next cycle
* @return next Town cell
*/
public abstract TownCell next(Town tNew);
}