Repository navigation
Expand file tree
/
Copy pathWorld.java
More file actions
153 lines (125 loc) · 3.87 KB
/
Copy pathWorld.java
File metadata and controls
153 lines (125 loc) · 3.87 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
import java.util.Random;
public class World {
/**
* Size of the world
*/
int _size;
/**
* The world, a 2D array of Cells which can be living or dead.
*/
Cell[][] _world;
/**
* A random number generator.
*/
Random _rng;
/**
* Initial world constructor.
* @param size Size of world
* @param seed Random number generator seed
* @param percent Initial percentage of cells alive
*/
public World(int size, int seed, int percent) {
_size = size;
_rng = new Random(seed);
_world = generateBoard(size, percent);
}
/**
* Iterated (non-initial) world constructor.
* @param cells Cells living in the new world
* @param rng Random number generator
*/
public World(Cell[][] cells, Random rng) {
_size = cells.length;
_rng = rng;
_world = cells;
}
/**
* Generate the initial state of a cell, given the percentage
* chance that it is living.
* @param percent percentage chance that it is living
* @return state of cell, State.ALIVE or State.DEAD
*/
private State generateInitialState(int percent) {
int livingChance = _rng.nextInt(100);
State toReturn = (livingChance < percent) ? State.ALIVE : State.DEAD;
return toReturn;
}
/**
* The number of living neighbors that a cell has.
* @param world the world
* @param x x location of cell
* @param y y location of cell
* @return
*/
private int getNumNeighbors(Cell[][] world, int x, int y) {
int size = world.length;
//This was the code I refactored. I removed the mod (%) of the values that could potentially be negative and only used the negative-checking if-statements
//on the left and up commands. Additionally, there was a large for-loop that seemed unnecessary, so I removed it.
//BEGIN REFACTORED CODE
int leftX = (x - 1);
int rightX = (x + 1)%size;
int upY = (y - 1);
int downY = (y + 1)%size;
if (leftX == -1) { leftX = size - 1; }
if (upY == -1) { upY = size - 1; }
//END REFACTORED CODE
int numNeighbors = 0;
if (world[leftX][upY].isAlive()) { numNeighbors++; }
if (world[leftX][downY].isAlive()) { numNeighbors++; }
if (world[leftX][y].isAlive()) { numNeighbors++; }
if (world[rightX][upY].isAlive()) { numNeighbors++; }
if (world[rightX][downY].isAlive()) { numNeighbors++; }
if (world[rightX][y].isAlive()) { numNeighbors++; }
if (world[x][upY].isAlive()) { numNeighbors++; }
if (world[x][downY].isAlive()) { numNeighbors++; }
return numNeighbors;
}
/**
* Go through one iteration of this World and return new World.
* @return New world
*/
public World iterate() {
Cell[][] newCells = new Cell[_size][_size];
for (int j = 0; j < _size; j++ ) {
for (int k = 0; k < _size; k++) {
newCells[j][k] = new Cell(_world[j][k].iterate(getNumNeighbors(_world, j, k)), j, k);
}
}
return new World(newCells, _rng);
}
/**
* Convert this World to a string for display.
* @return String representation of world
*/
public String toString() {
StringBuilder toReturn = new StringBuilder();
toReturn.append(" ");
for (int j= 0; j < _size; j++) {
toReturn.append(j % 10);
}
toReturn.append("\n");
for (int j = 0; j < _size; j++ ) {
toReturn.append((j % 10) + " ");
for (int k = 0; k < _size; k++) {
toReturn.append(_world[j][k].getStateRep());
}
toReturn.append("\n");
}
return toReturn.toString();
}
/**
* Generate initial game board.
* @param size Size of board
* @param percent Percent alive
* @return Initial world
*/
private Cell[][] generateBoard(int size, int percent) {
Cell[][] world = new Cell[size][size];
for (int j = 0; j < size; j++ ) {
for (int k = 0; k < size; k++) {
world[j][k] = new Cell(generateInitialState(percent), j, k);
}
}
return world;
}
}