Repository navigation
Expand file tree
/
Copy pathMaze.java
More file actions
115 lines (96 loc) · 3.28 KB
/
Copy pathMaze.java
File metadata and controls
115 lines (96 loc) · 3.28 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
import java.awt.Color;
import java.awt.Graphics2D;
public class Maze {
public static final int TILE_SIZE = 24;
public static final int ROWS = 25;
public static final int COLS = 25;
private static final String[] LAYOUT = {
"#########################", // 0
"#.......................#", // 1
"#.####.....###.....####.#", // 2
"#.####.....###.....####.#", // 3
"#.####.....###.....####.#", // 4
"#.......................#", // 5
"#.##...###.....###...##.#", // 6
"#.##...###.....###...##.#", // 7
"#.##...###.....###...##.#", // 8
"#.......................#", // 9
"#.........## ##.........#", // 10
"#.........# #.........#", // 11
"#.......... ..........#", // 12
"#.........# #.........#", // 13
"#.........#####.........#", // 14
"#.......................#", // 15
"#.##...###.....###...##.#", // 16
"#.##...###.....###...##.#", // 17
"#.##...###.....###...##.#", // 18
"#.......................#", // 19
"#.####.....###.....####.#", // 20
"#.####.....###.....####.#", // 21
"#.####.....###.....####.#", // 22
"#.......................#", // 23
"#########################", // 24
};
private char[][] grid;
private int pelletCount;
public Maze() {
reset();
}
public void reset() {
grid = new char[ROWS][COLS];
pelletCount = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
char ch = LAYOUT[row].charAt(col);
grid[row][col] = ch;
if (ch == '.') {
pelletCount++;
}
}
}
}
public boolean isWall(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
return true; // treat out-of-bounds as a wall so nothing walks off the map
}
return grid[row][col] == '#';
}
public boolean hasPellet(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
return false;
}
return grid[row][col] == '.';
}
public int eatPellet(int row, int col) {
if (hasPellet(row, col)) {
grid[row][col] = ' ';
pelletCount--;
return 10;
}
return 0;
}
public int getPelletCount() {
return pelletCount;
}
public void draw(Graphics2D g2d) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
int x = col * TILE_SIZE;
int y = row * TILE_SIZE;
char ch = grid[row][col];
if (ch == '#') {
g2d.setColor(Color.BLUE);
g2d.fillRoundRect(x + 1, y + 1, TILE_SIZE - 2, TILE_SIZE - 2, 6, 6);
} else if (ch == '.') {
g2d.setColor(new Color(255, 235, 160));
int dotSize = 6;
g2d.fillOval(
x + TILE_SIZE / 2 - dotSize / 2,
y + TILE_SIZE / 2 - dotSize / 2,
dotSize, dotSize
);
}
}
}
}
}