This repository was archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
55 lines (46 loc) · 1.5 KB
/
Copy pathGrid.java
File metadata and controls
55 lines (46 loc) · 1.5 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
package pixelgame;
import java.util.PritorityQueue;
public class Grid {
/** The length of one side of the square grid */
private int N;
/** 2D array of the terrain of the grid */
private Terrain.TerrainType[][] terrain;
/** 2D array of all the Pixels of the game. */
private Pixel[][] pixels; //An alternative to a 2D array is a PQ.
/** Creates square grid with a side length of size with no terrain and pixels. */
public Grid(int size) {
N = size;
terrain = new Terrain.TerrainType[N][N];
pixels = new Pixel[N][N];
}
/** Checks whether or not the coordinates specified are in the board, and if they aren't
* throws an exception. */
private void checkBounds(int x, int y) {
if (!((x < N && x >= 0) && (y < N && y >= 0))) {
throw new IllegalArgumentException("Coordinates are out of bounds.");
}
}
/** Returns the occupant at x and y. */
public Pixel get(int x, int y) {
checkBounds(x, y);
return pixels[x][y];
}
/** Returns true if spot at coordinates x and y is empty. */
private boolean isEmpty(int x, int y) {
checkBounds(x, y);
return pixels[x][y] == null;
}
/** Places Pixel p at location (x, y). */
public void place(Pixel p, int x, int y) {
checkBounds(x, y);
if (isEmpty(x, y)) {
pixels[x][y] = p;
} else {
throw new IllegalArgumentException(String.format("Tried to place a %s at "
+ "(%d, %d).", x, y));
}
}
/** Draws out the NxN grid. */
public void drawGrid() {
}
}