-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
47 lines (38 loc) · 1.73 KB
/
Grid.java
File metadata and controls
47 lines (38 loc) · 1.73 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
import java.awt.Color;
import java.awt.Graphics;
public class Grid extends Sprite {
public Grid(int x, int y, int squareSize) {
super(x, y, squareSize);
}
/**
* Draws the board using an externally passed 2D integer matrix and a 1D color lookup table.
* * @param g The graphics context to draw with.
* @param board The 2D array of integers (e.g., -1 for empty, 0 for Blue, 1 for Yellow...)
* @param pieceColors The 1D array of actual Color objects mapped to those index IDs.
*/
public void draw(Graphics g, int[][] board, Color[] pieceColors) {
int size = getSquareSize();
// Safety check to ensure arrays exist
if (board == null || pieceColors == null) return;
int rows = board.length;
int cols = board[0].length;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
int drawX = getX() + (c * size);
int drawY = getY() + (r * size);
int colorIndex = board[r][c];
// If the index is valid, look up the color and draw the played piece block
if (colorIndex >= 0 && colorIndex < pieceColors.length) {
Color blockColor = pieceColors[colorIndex];
drawSquare(g, drawX, drawY, blockColor);
} else {
// Otherwise, treat it as an empty tile (-1)
g.setColor(Color.WHITE);
g.fillRect(drawX, drawY, size, size);
g.setColor(Color.GRAY);
g.drawRect(drawX, drawY, size, size);
}
}
}
}
}