-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
74 lines (65 loc) · 1.7 KB
/
Copy pathGrid.java
File metadata and controls
74 lines (65 loc) · 1.7 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
package model;
import java.util.*;
public class Grid {
public int rows, cols;
public Cell[][] board;
public int[] rowReq, colReq;
public Cell start, goal;
public Grid(int r, int c, int[] rr, int[] cr) {
rows = r;
cols = c;
rowReq = rr;
colReq = cr;
board = new Cell[r][c];
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
board[i][j] = new Cell(i, j);
}
public boolean inBounds(int r, int c) {
return r >= 0 && c >= 0 && r < rows && c < cols;
}
public List<Cell> getNeighbors(Cell c) {
int[] dr = { -1, 1, 0, 0 };
int[] dc = { 0, 0, -1, 1 };
List<Cell> list = new ArrayList<>();
for (int k = 0; k < 4; k++) {
int nr = c.row + dr[k];
int nc = c.col + dc[k];
if (inBounds(nr, nc) && !board[nr][nc].blocked)
list.add(board[nr][nc]);
}
return list;
}
public void resetVisited() {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
board[i][j].visited = false;
}
public void printGrid() {
System.out.println("\nGRID:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Cell c = board[i][j];
if (c == start)
System.out.print(" S ");
else if (c == goal)
System.out.print(" G ");
else if (c.blocked)
System.out.print(" X ");
else if (c.visited)
System.out.print(" * ");
else if (c.track)
System.out.print(" T ");
else
System.out.print(" . ");
}
System.out.println(" | " + rowReq[i]);
}
for (int j = 0; j < cols; j++)
System.out.print("―――");
System.out.println();
for (int j = 0; j < cols; j++)
System.out.print(" " + colReq[j] + " ");
System.out.println("\n");
}
}