/Users/lyon/j4p/src/bookExamples/ch08ArraysAndVectors/maze/MazeGenerator.java

1    package bookExamples.ch08ArraysAndVectors.maze; 
2     
3     
4    public class MazeGenerator { 
5        private final char space = '.'; 
6        private final char wall = '#'; 
7        private final char start = 'S'; 
8        private final char end = 'E'; 
9        private char maze[][] = null; 
10       private static int a = 20; 
11       private static int d = 20; 
12    
13       public void markStart(java.awt.Point p) { 
14           getMaze()[p.x][p.y] = getStart(); 
15       } 
16    
17       public int frand(int s, int e) { 
18           return s + (int) (Math.random() * (e - s)); 
19       } 
20    
21       public void markEnd(java.awt.Point p) { 
22           getMaze()[p.x][p.y] = getEnd(); 
23       } 
24    
25       public MazeGenerator(int w, int h) { 
26           setMaze(new char[w][h]); 
27           init(); 
28       } 
29    
30       public static void main(String args[]) { 
31           int x = (int) (Math.random() * 10) + (int) (Math.random() * 10); 
32           int y = (int) (Math.random() * 10); 
33    
34           MazeGenerator mg = new MazeGenerator(getA(), getD()); 
35    
36           mg.markStart(new java.awt.Point(1, 1)); 
37           mg.markEnd(new java.awt.Point((getA() - 2), getD() - 2)); 
38           mg.print(mg.getStringArray()); 
39    
40           String mazeString[] = mg.getStringArray(); 
41    
42           MazeUtility.setMazeString(mazeString); 
43           MazeUtility.solve(); 
44           MazeUtility.print(); 
45       } 
46    
47       public void init() { 
48           fillWithSpaces(); 
49           addWall3(); 
50           addWall2(); 
51           addWall1(); 
52           addWall0(); 
53           addinnerwalls(); 
54    
55       } 
56    
57       public void addWall1() { 
58           for (int y = 0; y < getMaze().length; y++) 
59               getMaze()[y][getMaze()[0].length - 1] = getWall(); 
60       } 
61    
62       public void addWall2() { 
63           for (int x = 0; x < getMaze()[0].length; x++) 
64               getMaze()[getMaze().length - 1][x] = getWall(); 
65       } 
66    
67       public void addWall3() { 
68           for (int y = 0; y < getMaze().length; y++) 
69               getMaze()[y][0] = getWall(); 
70       } 
71    
72       public void addWall0() { 
73           for (int x = 0; x < getMaze()[0].length; x++) 
74               getMaze()[0][x] = getWall(); 
75       } 
76    
77       public void fillWithSpaces() { 
78           for (int r = 0; r < getMaze().length; r++) 
79               for (int col = 0; col < getMaze()[r].length; col++) 
80                   getMaze()[r][col] = getSpace(); 
81       } 
82    
83       public void addinnerwalls() { 
84           for (int r = 0; r < getMaze().length - 1; r++) { 
85               int x = (int) (Math.random() * 10 + 1); 
86               int y = (int) (Math.random() * 10 + 1); 
87               getMaze()[x][y] = getWall(); 
88    
89               for (int col = 0; col < getMaze()[r].length - 1; col++) 
90                   getMaze()[x + (getA() - 12)][y] = getWall(); 
91    
92               for (int col = 0; col < getMaze()[r].length - 1; col++) 
93                   getMaze()[x][y + (getA() - 12)] = getWall(); 
94    
95               for (int col = 0; col < getMaze()[r].length - 1; col++) 
96                   getMaze()[x + (getA() - 12)][y + (getA() - 12)] = getWall(); 
97    
98               for (int col = 0; col < getMaze()[r].length - 1; col++) 
99                   getMaze()[x][y] = getWall(); 
100   
101              for (int col = 0; col < getMaze()[r].length - 1; col++) 
102                  getMaze()[x][y] = getWall(); 
103   
104              if (getA() > 20) 
105                  for (int col = 0; col < getMaze()[r].length - 1; col++) { 
106                      for (int z = 5; z < 26; z++) { 
107                          getMaze()[15][z] = getWall(); 
108                          getMaze()[z][15] = getWall(); 
109                          getMaze()[z - 1][16] = getWall(); 
110                          getMaze()[16][z - 1] = getWall(); 
111                      } 
112                  } 
113   
114          } 
115      } 
116   
117      public String[] getStringArray() { 
118          String s[] = new String[getMaze().length]; 
119          for (int r = 0; r < getMaze().length; r++) 
120              s[r] = new String(getMaze()[r]); 
121          return s; 
122      } 
123   
124      public void print(String a[]) { 
125          for (int i = 0; i < a.length; i++) 
126              System.out.println(a[i]); 
127      } 
128   
129      public void print() { 
130          for (int r = 0; r < getMaze().length; r++) { 
131              System.out.println(); 
132              for (int col = 0; col < getMaze()[r].length; col++) 
133                  System.out.print(getMaze()[r][col]); 
134          } 
135      } 
136   
137      public char getSpace() { 
138          return space; 
139      } 
140   
141      public char getWall() { 
142          return wall; 
143      } 
144   
145      public char getStart() { 
146          return start; 
147      } 
148   
149      public char getEnd() { 
150          return end; 
151      } 
152   
153      public char[][] getMaze() { 
154          return maze; 
155      } 
156   
157      public void setMaze(char[][] maze) { 
158          this.maze = maze; 
159      } 
160   
161      public static int getA() { 
162          return a; 
163      } 
164   
165      public static void setA(int a) { 
166          MazeGenerator.a = a; 
167      } 
168   
169      public static int getD() { 
170          return d; 
171      } 
172   
173      public static void setD(int d) { 
174          MazeGenerator.d = d; 
175      } 
176  } 
177   
178   
179   
180