-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.java
53 lines (45 loc) · 1.32 KB
/
World.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
import java.util.Stack;
public class World {
private Particle[][] grid;
private Particle[][] newGrid;
public int width;
public int height;
public World(int width, int height) {
this.height = height;
this.width = width;
grid = new Particle[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
grid[i][j] = new Particle(i, j);
}
}
step();
}
public void step() {
newGrid = new Particle[grid.length][grid[0].length];
for (int i = grid.length - 1; i >= 0; i--) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j].behave(grid, newGrid, i, j);
}
}
grid = newGrid;
}
public Particle get(int x, int y) {
return grid[x][y];
}
public void addSand(int x, int y, float hue) {
if (x < 0 || x >= width || y < 0 || y >= height)
return;
grid[x][y].makeSand(hue);
}
public void addConcrete(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height)
return;
grid[x][y].makeConcrete();
}
public void addWater(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height)
return;
grid[x][y].makeWater();
}
}