-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.java
125 lines (94 loc) · 3.39 KB
/
Cell.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
import java.awt.*;
import java.util.ArrayList;
public class Cell extends GameObject {
protected State state = State.Dead;
private boolean firstrun = true;
public void setIndex(int xIndex, int yIndex)
{
this.xIndex = xIndex;
this.yIndex = yIndex;
}
public int xIndex()
{
return this.xIndex;
}
public int yIndex()
{
return this.yIndex;
}
public void setState(State state)
{
this.state = state;
}
public State getState()
{
return this.state;
}
public int NumberOfNeighbors()
{
int numNeighbors = 0;
State NW = State.Dead;
State N = State.Dead;
State NE = State.Dead;
State W = State.Dead;
State E = State.Dead;
State SW = State.Dead;
State S = State.Dead;
State SE = State.Dead;
try {NW = GameHandler.prevList.get(xIndex-1).get(yIndex-1).getState();}catch (Exception e){};
try {N = GameHandler.prevList.get(xIndex).get(yIndex-1).getState();}catch (Exception e){};
try {NE = GameHandler.prevList.get(xIndex+1).get(yIndex - 1).getState();}catch (Exception e){};
try {W = GameHandler.prevList.get(xIndex-1).get(yIndex).getState();}catch (Exception e){};
try {E = GameHandler.prevList.get(xIndex+1).get(yIndex).getState();}catch (Exception e){};
try {SW = GameHandler.prevList.get(xIndex-1).get(yIndex+1).getState();}catch (Exception e){};
try {S = GameHandler.prevList.get(xIndex).get(yIndex+1).getState();}catch (Exception e){};
try {SE = GameHandler.prevList.get(xIndex+1).get(yIndex+1).getState();}catch (Exception e){};
if(NW == State.Alive){ numNeighbors++;}
if(N == State.Alive){ numNeighbors++;}
if(NE == State.Alive){ numNeighbors++;}
if(W == State.Alive){ numNeighbors++;}
if(E == State.Alive){ numNeighbors++;}
if(SW == State.Alive){ numNeighbors++;}
if(S == State.Alive){ numNeighbors++;}
if(SE == State.Alive){ numNeighbors++;}
return numNeighbors;
}
@Override
public State tick()
{
if(NumberOfNeighbors() < 2 || NumberOfNeighbors() > 3){
if(this.state == State.Alive) {
System.out.println("Cell " + xIndex + ", " + yIndex + " has died");
}
setState(State.Dead);
}
if(NumberOfNeighbors() == 3){
if(this.state == State.Dead) {
System.out.println("Cell " + xIndex + ", " + yIndex + " was born");
}
setState(State.Alive);
}
return this.state;
}
@Override
public void render(Graphics g)
{
if(state == State.Alive)
{
g.setColor(Color.green);
g.fillRect(x, y, Game.CELLSIZE, Game.CELLSIZE);
}
else if(state == State.Dead)
{
g.setColor(Color.gray);
g.drawRect(x, y, Game.CELLSIZE, Game.CELLSIZE);
}
g.setColor(Color.red);
g.drawString(String.valueOf(NumberOfNeighbors()), x+10, y+10);
g.drawString(xIndex + ", " + yIndex, x+20, y+20);
}
public Cell(int x, int y)
{
super(x, y, ID.Cell);
}
}