Created
November 6, 2023 18:23
-
-
Save rebekah/bd1681ccce056df25fc560705a3c6572 to your computer and use it in GitHub Desktop.
SandLab.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.util.*; | |
import java.awt.Color; | |
import java.util.Random; | |
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; | |
//do not add any more fields | |
private int[][] grid; | |
int rows; | |
int cols; | |
private SandDisplay display; | |
public static final Color GREY = new Color(150,150,150); | |
public static final Color BLACK = new Color(0,0,0); | |
public static final Color YELLOW = new Color(255,255,0); | |
public static final Color BLUE = new Color(0,0,255); | |
public SandLab(int numRows, int numCols) | |
{ | |
String[] names; | |
names = new String[4]; | |
names[EMPTY] = "Empty"; | |
names[METAL] = "Metal"; | |
names[SAND] = "Sand"; | |
names[WATER] = "Water"; | |
grid = new int[numRows][numCols]; | |
rows = numRows; | |
cols = numCols; | |
display = new SandDisplay("Falling Sand", numRows, numCols, names); | |
} | |
//called when the user clicks on a location using the given tool | |
private void locationClicked(int row, int col, int tool) | |
{ | |
grid[row][col] = tool; | |
} | |
//copies each element of grid into the display | |
public void updateDisplay() | |
{ | |
for(int i=0; i < rows; i++){ | |
for(int j=0; j < cols; j++){ | |
int particle = grid[i][j]; | |
if(particle == METAL){ | |
display.setColor(i,j,GREY); | |
} else if(particle == SAND) { | |
display.setColor(i,j,YELLOW); | |
} else if(particle == WATER){ | |
display.setColor(i,j,BLUE); | |
} else { | |
display.setColor(i,j,BLACK); | |
} | |
} | |
} | |
} | |
//called repeatedly. | |
//causes one random particle to maybe do something. | |
public void step() | |
{ | |
Random rand = new Random(); | |
int row = rand.nextInt(rows); | |
int col = rand.nextInt(cols); | |
if( | |
grid[row][col] == SAND && | |
row != rows - 1 && | |
grid[row+1][col] != METAL && | |
grid[row+1][col] != SAND | |
){ | |
if(grid[row+1][col] == WATER){ | |
grid[row][col] = WATER; | |
} else { | |
grid[row][col] = EMPTY; | |
} | |
grid[row+1][col] = SAND; | |
} else if(grid[row][col] == WATER) { | |
int direction = rand.nextInt(3); | |
if( | |
direction == 0 && | |
col != 0 && | |
grid[row][col-1] == EMPTY | |
) { | |
grid[row][col] = EMPTY; | |
grid[row][col-1] = WATER; | |
} else if( | |
direction == 1 && | |
col != cols-1 && | |
grid[row][col+1] == EMPTY | |
) { | |
grid[row][col] = EMPTY; | |
grid[row][col+1] = WATER; | |
} else if( | |
direction == 2 && | |
row != rows-1 && | |
grid[row+1][col] == EMPTY | |
) { | |
grid[row][col] = EMPTY; | |
grid[row+1][col] = 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()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment