-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualization.java
More file actions
118 lines (104 loc) · 3.96 KB
/
Visualization.java
File metadata and controls
118 lines (104 loc) · 3.96 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
116
117
118
import java.util.List;
import java.util.Set;
import java.util.HashSet;
public class Visualization {
public static void displayMaze(Maze maze) {
int rowCount = maze.getRows();
int colCount = maze.getCols();
boolean[][] hWalls = maze.getHorizontalWalls();
boolean[][] vWalls = maze.getVerticalWalls();
Cell start = maze.getStart();
Cell end = maze.getEnd();
// draw the top border
for (int c = 0; c < colCount; c++) {
System.out.print("+---");
}
System.out.println("+");
// print each row of cells
for (int r = 0; r < rowCount; r++) {
// print cells and their right walls
System.out.print("|");
for (int c = 0; c < colCount; c++) {
// mark start, end, or empty space
if (r == start.getRow() && c == start.getColumn()) {
System.out.print(" S ");
} else if (r == end.getRow() && c == end.getColumn()) {
System.out.print(" E ");
} else {
System.out.print(" ");
}
// choose between a wall and an opening
if (vWalls[r][c]) {
System.out.print("|");
} else {
System.out.print(" ");
}
}
System.out.println();
// underline each cell with horizontal walls
for (int c = 0; c < colCount; c++) {
System.out.print("+");
if (hWalls[r][c]) {
System.out.print("---");
} else {
System.out.print(" ");
}
}
System.out.println("+");
}
}
public static void displaySolution(Maze maze, List<Cell> path) {
if (path == null || path.isEmpty()) {
System.out.println("No solution found!");
return;
}
int rowCount = maze.getRows();
int colCount = maze.getCols();
boolean[][] hWalls = maze.getHorizontalWalls();
boolean[][] vWalls = maze.getVerticalWalls();
Cell start = maze.getStart();
Cell end = maze.getEnd();
// keep a set for quick path checks
Set<Cell> pathCells = new HashSet<>(path);
// draw the top border
for (int c = 0; c < colCount; c++) {
System.out.print("+---");
}
System.out.println("+");
// print each row of cells
for (int r = 0; r < rowCount; r++) {
// print cells and their right walls
System.out.print("|");
for (int c = 0; c < colCount; c++) {
Cell currentCell = new Cell(r, c);
// mark start, end, path, or empty space
if (r == start.getRow() && c == start.getColumn()) {
System.out.print(" S ");
} else if (r == end.getRow() && c == end.getColumn()) {
System.out.print(" E ");
} else if (pathCells.contains(currentCell)) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
// choose between a wall and an opening
if (vWalls[r][c]) {
System.out.print("|");
} else {
System.out.print(" ");
}
}
System.out.println();
// underline each cell with horizontal walls
for (int c = 0; c < colCount; c++) {
System.out.print("+");
if (hWalls[r][c]) {
System.out.print("---");
} else {
System.out.print(" ");
}
}
System.out.println("+");
}
}
}