-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.java
129 lines (111 loc) · 3.77 KB
/
Particle.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
126
127
128
129
import java.awt.Color;
import java.util.Random;
public class Particle {
private String type = "empty";
private Color color = Color.WHITE;
private static final double gravity = 9.81;
private int x;
private int y;
public Particle(int x, int y) {
this.x = x;
this.y = y;
}
public String getType() {
return type;
}
public Color getColor() {
return color;
}
public void makeConcrete() {
type = "concrete";
color = Color.GRAY;
}
public void makeWater() {
type = "water";
color = Color.blue;
}
public void makeSand(float hue) {
type = "sand";
color = Color.getHSBColor(hue, 1, 1);
}
public void makeEmpty() {
type = "empty";
color = Color.BLACK;
}
public void behave(Particle[][] grid, Particle[][] newGrid, int i, int j) {
if (type.equals("empty")) {
if (newGrid[i][j] == null) {
newGrid[i][j] = this;
}
} else if (type.equals("sand")) {
sandBehave(grid, newGrid, i, j);
} else if (type.equals("water")) {
waterBehave(grid, newGrid, i, j);
} else if (type.equals("concrete")) {
newGrid[i][j] = this;
} else {
System.out.println("Unknown type: " + type);
}
}
private void waterBehave(Particle[][] grid, Particle[][] newGrid, int i, int j) {
if (i + 1 < grid.length && grid[i + 1][j].type.equals("empty")) {
newGrid[i + 1][j] = this;
newGrid[i][j] = new Particle(i, j);
} else if (j - 1 >= 0 && j + 1 < grid[0].length && isEmpty(grid[i][j - 1]) && isWater(grid[i - 1][j])
&& isEmpty(grid[i][j + 1])) {
if (coinFlip()) {
newGrid[i][j - 1] = this;
} else {
newGrid[i][j + 1] = this;
}
if (newGrid[i][j] == null) {
newGrid[i][j] = new Particle(i, j);
}
// && isEmpty(grid[i][j - 1]) && isEmpty(grid[i][j + 1]
} else if (j - 1 > 0 && isEmpty(grid[i][j - 1]) && isWater(grid[i - 1][j])) {
newGrid[i][j - 1] = this;
if (newGrid[i][j] == null) {
newGrid[i][j] = new Particle(i, j);
}
} else if (j + 1 < grid[0].length && i < grid.length - 2 && isEmpty(grid[i][j + 1])
&& isEmpty(newGrid[i][j + 1])
&& isWater(grid[i + 1][j])) {
newGrid[i][j + 1] = this;
if (newGrid[i][j] == null) {
newGrid[i][j] = new Particle(i, j);
}
} else {
newGrid[i][j] = this;
}
}
private boolean isWater(Particle particle) {
if (particle == null) {
return false;
}
return particle.type.equals("water");
}
private boolean coinFlip() {
Random r = new Random();
return r.nextBoolean();
}
private boolean isEmpty(Particle p) {
if (p == null) {
return true;
}
return p.type.equals("empty");
}
private void sandBehave(Particle[][] grid, Particle[][] newGrid, int i, int j) {
if (i + 1 < grid.length && grid[i + 1][j].type.equals("empty")) {
newGrid[i + 1][j] = this;
newGrid[i][j] = new Particle(i, j);
} else if (i + 1 < grid.length && j + 1 < grid[0].length && grid[i + 1][j + 1].type.equals("empty")) {
newGrid[i + 1][j + 1] = this;
newGrid[i][j] = new Particle(i, j);
} else if (i + 1 < grid.length && j - 1 >= 0 && grid[i + 1][j - 1].type.equals("empty")) {
newGrid[i + 1][j - 1] = this;
newGrid[i][j] = new Particle(i, j);
} else {
newGrid[i][j] = this;
}
}
}