forked from hoohack/CodeInJavaNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.java
More file actions
172 lines (126 loc) · 3.79 KB
/
Maze.java
File metadata and controls
172 lines (126 loc) · 3.79 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package chapter5;
import java.util.ArrayList;
import java.util.List;
public class Maze {
private int[][] map = {
{2,2,2,2,2,2,2},
{0,0,0,0,0,0,2},
{2,0,2,0,2,0,2},
{2,0,0,2,0,2,2},
{2,2,0,2,0,2,2},
{2,0,0,0,0,0,2},
{2,2,2,2,2,0,2}
};
private static int[][] DIRECTIONS = {{ 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0}};
private static int ROAD = 0;
private static int VISITED = 1;
private static int WALL = 2;
private static int EXIT = 3;
private int exitRow;
private int exitCol;
private int entryRow;
private int entryCol;
public int[][] getMap() {
return map;
}
public void setEntry(int row, int col) {
setEntryRow(row);
setEntryCol(col);
}
public void setExit(int row, int col) {
setExitRow(row);
setExitCol(col);
map[row][col] = EXIT;
}
private void setEntryRow(int row) {
entryRow = row;
}
private void setEntryCol(int col) {
entryCol = col;
}
public int getEntryRow() {
return entryRow;
}
public int getEntryCol() {
return entryCol;
}
private boolean isValidLocation(int row, int col) {
return map[row][col] == ROAD || map[row][col] == EXIT;
}
private boolean isWall(int row, int col) {
return map[row][col] == WALL;
}
private boolean isExplored(int row, int col) {
return map[row][col] == VISITED;
}
private void setVisited(int row, int col) {
if (map[row][col] == 0) {
map[row][col] = VISITED;
}
}
private void setNotPath(int row, int col) {
if (map[row][col] == VISITED) {
map[row][col] = 4;
}
}
public boolean isExit(int row, int col) {
return map[row][col] == EXIT;
}
private Coordinate getNextCoordinate(int row, int col, int i, int j) {
return new Coordinate(row + i, col + j);
}
private boolean explore(int row, int col, List<Coordinate> path) {
if (!isValidLocation(row, col)
|| isWall(row, col)
|| isExplored(row, col)) {
return false;
}
path.add(new Coordinate(row, col));
setVisited(row, col);
if (isExit(row, col)) {
return true;
}
for (int[] direction : DIRECTIONS) {
Coordinate coordinate = getNextCoordinate(
row, col, direction[0], direction[1]);
if (explore(coordinate.getRow(),coordinate.getCol(),path)) {
return true;
}
}
setNotPath(row, col);
path.remove(path.size() - 1);
return false;
}
public int getExitCol() {
return exitCol;
}
public void setExitCol(int exitCol) {
this.exitCol = exitCol;
}
public int getExitRow() {
return exitRow;
}
public void setExitRow(int exitRow) {
this.exitRow = exitRow;
}
public static void main(String[] args) {
Maze maze = new Maze();
List<Coordinate> path = new ArrayList<>();
maze.setEntry(1, 0);
maze.setExit(6, 5);
maze.explore(maze.getEntryRow(), maze.getEntryCol(), path);
int[][] map = maze.getMap();
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; col++) {
if (map[row][col] == 2) {
System.out.print("# ");
} else if (map[row][col] == 0 || map[row][col] == 4) {
System.out.print(" ");
} else if (map[row][col] == 1 || map[row][col] == 3) {
System.out.print("* ");
}
}
System.out.println();
}
}
}