/Users/lyon/j4p/src/bookExamples/ch08ArraysAndVectors/maze/MazeUtility.java
|
1 package bookExamples.ch08ArraysAndVectors.maze;
2
3
4 public class MazeUtility {
5 private final static int right = 1, left = 3, up = 2, down = 0;
6 private static int w = 0;
7 private static int h = 0;
8
9 private static String mazeString[] = null;
10 private static char maze[][] = null;
11
12 public static void setMazeString(String ms[]) {
13 mazeString = ms;
14 maze = getChars(mazeString);
15 }
16
17 public void setMaze(char c[][]) {
18 maze = c;
19 }
20
21
22 public static char[][] getChars(String s[]) {
23 char c[][] = new char[s.length][s[0].length()];
24 for (int r = 0; r < s.length; r++)
25 s[r].getChars(0, s[0].length(), c[r], 0);
26
27 return c;
28 }
29
30 public static void print() {
31 char c[][] = maze;
32 for (int r = 0; r < c.length; r++) {
33 for (int col = 0; col < c[r].length; col++)
34 System.out.print(c[r][col]);
35 System.out.println();
36 }
37 }
38
39 public static java.awt.Point getStart() {
40 for (int r = 0; r < maze.length; r++)
41 for (int c = 0; c < maze[0].length; c++)
42 if (maze[r][c] == 'S')
43 return new java.awt.Point(r, c);
44 return new java.awt.Point(0, 0);
45 }
46
47 public static java.awt.Point getExit() {
48 return new java.awt.Point(6, 16);
49 }
50
51 public static void markExit(java.awt.Point p) {
52 maze[p.x][p.y] = 'E';
53 }
54
55 public static boolean isDone(
56 char maze[][],
57 java.awt.Point p) {
58 return maze[p.x][p.y] == 'E';
59 }
60
61 public static void markStart(
62 java.awt.Point p) {
63 maze[p.x][p.y] = 'S';
64 }
65
66 public static void mark(java.awt.Point p) {
67 maze[p.x][p.y] = 'X';
68 }
69
70 public static void solve() {
71 java.awt.Point start = getStart();
72 //System.out.println("Start="+start);
73 //Point exit = getExit();
74 //System.out.println("Exit="+exit);
75 //markStart( start);
76 //markExit(exit);
77 mazeTraverse(start, left);
78 }
79
80 public static void mazeTraverse(java.awt.Point loc, int direction) {
81 if (isDone(maze, loc)) {
82 System.out.println("I am done!");
83 return;
84 }
85 w = maze.length;
86 h = maze[0].length;
87 int x = loc.x;
88 int y = loc.y;
89 mark(loc);
90 for (int move = direction, count = 0; count < 4; ++count, ++move, move %= 4) {
91 switch (move) {
92 case down:
93 if (validMove(x + 1, y)) {
94 mazeTraverse(new java.awt.Point(x + 1, y), left);
95 return;
96 }
97 break;
98 case right:
99 if (validMove(x, y + 1)) {
100 mazeTraverse(new java.awt.Point(x, y + 1), down);
101 return;
102 }
103 break;
104 case up:
105 if (validMove(x - 1, y)) {
106 mazeTraverse(new java.awt.Point(x - 1, y), right);
107 return;
108 }
109 break;
110 case left:
111 if (validMove(x, y - 1)) {
112 mazeTraverse(new java.awt.Point(x, y - 1), up);
113 return;
114 }
115 break;
116 }
117
118 }
119 }
120
121 public static boolean validMove(int r, int c) {
122 return (r >= 0 && r <= w && c >= 0 && c < h && maze[r][c] != '#');
123 }
124
125 public static int getRight() {
126 return right;
127 }
128
129 public static int getLeft() {
130 return left;
131 }
132
133 public static int getUp() {
134 return up;
135 }
136
137 public static int getDown() {
138 return down;
139 }
140
141 public static int getW() {
142 return w;
143 }
144
145 public static void setW(int w) {
146 MazeUtility.w = w;
147 }
148
149 public static int getH() {
150 return h;
151 }
152
153 public static void setH(int h) {
154 MazeUtility.h = h;
155 }
156
157 public static String[] getMazeString() {
158 return mazeString;
159 }
160
161 public static char[][] getMaze() {
162 return maze;
163 }
164
165 }