-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualization.java
More file actions
94 lines (80 loc) · 3.39 KB
/
Visualization.java
File metadata and controls
94 lines (80 loc) · 3.39 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
/* Makes the actual display or visualization of the maze and path.
It used ASCII characters to create all parts of the maze.
*/
import java.util.List;
public class Visualization {
private final Maze maze;
// Constructor for Visualization class
public Visualization(Maze maze) {
this.maze = maze;
}
// Prints the maze with the given path, start, and end
// Uses ASCII characters to represent walls, paths, start, and end
public void printMazeWithPath(List<Cell> path, Cell start, Cell end) {
int rows = maze.rows;
int cols = maze.cols;
// Creates a 2D array to mark cells
boolean[][] onPath = new boolean[rows][cols];
// Mark the cells that are part of the path
if (path != null) {
// Uses the correct field names 'row' and 'col' from the Cell object
for (int i = 0; i < path.size(); i++) {
Cell c = path.get(i);
onPath[c.row][c.col] = true;
}
}
// Print the top border
for (int c = 0; c < cols; c++) System.out.print("+---");
System.out.println("+");
// Access wall status
boolean[][] openRight = maze.openRight;
boolean[][] openDown = maze.openDown;
// Print each row of the maze
for (int rowPrint = 0; rowPrint < rows; rowPrint++) {
// Print cell contents for the rows
System.out.print("|"); // Far left boundary
for (int columnPrint = 0; columnPrint < cols; columnPrint++) { // Local variable c is fine here
// Cell content printing
Cell currentCell = maze.grid[rowPrint][columnPrint]; // Accesses the grid
if (currentCell.equals(start)) {
System.out.print(" S ");
}
else if (currentCell.equals(end)) {
System.out.print(" E ");
}
else if (onPath[rowPrint][columnPrint]) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
// Vertical wall to the right of the current cell
if (columnPrint == cols - 1) {
System.out.print("|"); // Far right boundary
} else {
// Check openRight status for interior vertical wall
if (openRight[rowPrint][columnPrint]) {
System.out.print(" ");
} else {
System.out.print("|");
}
}
}
System.out.println();
// Print horizontal walls
if (rowPrint < rows - 1) {
for (int columnPrint = 0; columnPrint < cols; columnPrint++) {
// Check openDown status for the wall below cell (rowPrint,columnPrint)
if (openDown[rowPrint][columnPrint]) {
System.out.print("+ ");
} else {
System.out.print("+---");
}
}
System.out.println("+");
}
}
// Print the bottmom border
for (int columnPrint = 0; columnPrint < cols; columnPrint++) System.out.print("+---");
System.out.println("+");
}
}