-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSandLab.java
More file actions
180 lines (167 loc) · 5.47 KB
/
Copy pathSandLab.java
File metadata and controls
180 lines (167 loc) · 5.47 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Falling Sand Lab Code
* Nifty Assignments 2017
* AP CS A, May 2019
* Anthony DiMaggio
*/
import java.awt.*;
import java.util.*;
public class SandLab
{
public static void main(String[] args)
{
SandLab lab = new SandLab(120, 80);
lab.run();
}
//add constants for particle types here
public static final int EMPTY = 0;
public static final int METAL = 1;
public static final int SAND = 2;
public static final int WATER = 3;
public static final int BALLOON = 4;
public static final int CLOUD = 5;
public static final int RAINCLOUD = 6;
//do not add any more fields
private int[][] grid;
private SandDisplay display;
private int cols;
public SandLab(int numRows, int numCols)
{
//initialize names and display
String[] names;
names = new String[7];
names[EMPTY] = "Empty";
names[METAL] = "Metal";
names[SAND] = "Sand";
names[WATER] = "Water";
names[BALLOON] = "Balloon";
names[CLOUD] = "Cloud";
names[RAINCLOUD] = "Rain Cloud";
display = new SandDisplay("Falling Sand", numRows, numCols, names);
cols = numCols;
grid = new int[numRows][numCols];
}
//called when the user clicks on a location using the given tool
private void locationClicked(int row, int col, int tool)
{
//update clicked pixel
grid[row][col] = tool;
}
//copies each element of grid into the display
public void updateDisplay()
{
//sets specific colors for each material
for(int i = 0; i< grid.length;i++) {
for(int j = 0; j< cols; j++) {
if(grid[i][j] == EMPTY) {
display.setColor(i, j, new Color(0,0,0));
}else if(grid[i][j] == METAL) {
display.setColor(i, j, new Color(200,200,200));
}else if (grid[i][j] == SAND) {
display.setColor(i, j, new Color(232, 227, 83));
}else if (grid[i][j] == WATER) {
display.setColor(i, j, new Color(51, 162, 214));
}else if (grid[i][j] == BALLOON) {
display.setColor(i, j, new Color(20, 200, 20));
}else if (grid[i][j] == CLOUD) {
display.setColor(i, j, new Color(255,255,255));
}else if (grid[i][j] == RAINCLOUD) {
display.setColor(i, j, new Color(120, 120, 120));
}
}
}
}
//called repeatedly.
//causes one random particle to maybe do something.
public void step()
{
Random r = new Random();
int srow = r.nextInt(grid.length);
int scol = r.nextInt(cols);
//FALLING SAND!
if (srow != grid.length-1 && grid[srow][scol] == SAND && grid[srow+1][scol] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow+1][scol] = SAND;
}else if(srow != grid.length-1 && grid[srow][scol] == SAND && grid[srow+1][scol] == WATER) {
grid[srow][scol] = WATER;
grid[srow+1][scol] = SAND;
}
else if(grid[srow][scol] == WATER) {
//random water movement
int dir = r.nextInt(4);
if(dir == 0 && scol !=0 && grid[srow][scol-1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol-1] = WATER;
}
if((dir == 1 || dir == 3) && srow != grid.length-1 && grid[srow+1][scol] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow+1][scol] = WATER;
}
if(dir == 2 && scol != cols -1 && grid[srow][scol+1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol+1] = WATER;
}
} else if(srow != 0 && grid[srow][scol] == BALLOON && (grid[srow-1][scol] == EMPTY || grid[srow-1][scol] == WATER ||grid[srow-1][scol] == BALLOON )) {
//balloon moves up if water or sand isn't blocking.
if(grid[srow-1][scol] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow-1][scol] = BALLOON;
}else if(grid[srow-1][scol] == WATER) {
grid[srow][scol] = WATER;
grid[srow-1][scol] = BALLOON;
} else if(grid[srow-1][scol] == BALLOON) {
//if there's another balloon above, first balloon tries to move around
int dir = r.nextInt(2);
if(dir == 0 && scol !=0 && grid[srow][scol-1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol-1] = BALLOON;
}
if(dir == 1 && scol != cols -1 && grid[srow][scol+1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol+1] = BALLOON;
}
}
}else if(grid[srow][scol] == CLOUD ) {
//Clouds and rainclouds move in horizontal line
int dir = r.nextInt(2);
if(dir == 0 && scol !=0 && grid[srow][scol-1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol-1] = CLOUD;
}
if(dir == 1 && scol != cols -1 && grid[srow][scol+1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol+1] = CLOUD;
}
}else if(grid[srow][scol] == RAINCLOUD ) {
//Clouds and rainclouds move in horizontal line
int dir = r.nextInt(2);
if(dir == 0 && scol !=0 && grid[srow][scol-1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol-1] = RAINCLOUD;
}
if(dir == 1 && scol != cols -1 && grid[srow][scol+1] == EMPTY) {
grid[srow][scol] = EMPTY;
grid[srow][scol+1] = RAINCLOUD;
}
dir = r.nextInt(10);
//raincloud spawns water only 10% of time because it was too agressive before
if(srow != grid.length-1 && grid[srow+1][scol] == EMPTY && dir == 0) {
grid[srow+1][scol] = WATER;
}
}
}
//do not modify
public void run()
{
while (true)
{
for (int i = 0; i < display.getSpeed(); i++)
step();
updateDisplay();
display.repaint();
display.pause(1); //wait for redrawing and for mouse
int[] mouseLoc = display.getMouseLocation();
if (mouseLoc != null) //test if mouse clicked
locationClicked(mouseLoc[0], mouseLoc[1], display.getTool());
}
}
}