-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeEngine.java
More file actions
96 lines (82 loc) · 1.92 KB
/
MazeEngine.java
File metadata and controls
96 lines (82 loc) · 1.92 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
public class MazeEngine{
boolean wall = false;
char[][] maze;
boolean solved;
int startX;
int startY;
public MazeEngine(char[][] input)
{
maze = input;
}
public void findPath(int row, int col)
{
/*
* If the value being looked at is the exit,
* we have solved the maze.
*/
if (maze[row][col] == '-')
{
solved = true;
return;
}
maze[row][col] = '.';
/*
* If the value on bottom is empty or is an exit,
* we recall the function.
*/
if (maze[row + 1][col] == ' ' || maze[row + 1][col] == '-')
{
findPath(row + 1, col);
}
/*
* If the value on right is empty or is an exit,
*/
else if (maze[row][col + 1] == ' ' || maze[row][col + 1] == '-')
{
findPath(row, col + 1);
}
/*
* If the value on top is empty or is an exit,
*/
else if (maze[row - 1][col] == ' ' || maze[row - 1][col] == '-')
{
findPath(row -1, col);
}
/*
* If the value on left is empty or is an exit,
*/
else if (maze[row][col - 1] == ' ' || maze[row][col - 1] == '-')
{
findPath(row, col - 1);
}
else
{
wall = true;
return;
}
if (wall)
{
wall = false;
findPath(row, col);
}
if (solved)
{
maze[row][col] = '+';
}
}
public void printMaze(int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public char[][] fileout()
{
return maze;
}
}