Skip to content

Commit eda53b4

Browse files
committed
added Prims Generation to maze_generate using Java
1 parent a96f29c commit eda53b4

File tree

4 files changed

+311
-0
lines changed

4 files changed

+311
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Coordinate{
2+
private int xcor, ycor;
3+
4+
public Coordinate(int x, int y){
5+
xcor = x;
6+
ycor = y;
7+
}
8+
9+
// ACCESSOR METHODS
10+
public int getX(){
11+
return xcor;
12+
}
13+
14+
public int getY(){
15+
return ycor;
16+
}
17+
18+
// MODIFIERS
19+
public void setX(int x){
20+
xcor = x;
21+
}
22+
23+
public void setY(int y){
24+
ycor = y;
25+
}
26+
27+
public String toString(){
28+
return "(" + xcor + "," + ycor + ")";
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
import java.util.ArrayList;
2+
import java.io.PrintWriter;
3+
import java.io.IOException;
4+
5+
public class PrimsGenerator{
6+
private int row, col;
7+
public Square[][] maze;
8+
private ArrayList<Coordinate[]> frontiers;
9+
10+
public PrimsGenerator(int r, int c) throws IOException{
11+
row = r;
12+
col = c;
13+
maze = new Square[row][col];
14+
frontiers = new ArrayList<Coordinate[]>();
15+
fillWithWall();
16+
generate();
17+
addSE();
18+
//writeToFile();
19+
}
20+
21+
private void fillWithWall(){
22+
for (int r = 0; r < row; r++){
23+
for (int c = 0; c < col; c++){
24+
maze[r][c] = new Square("#");
25+
}
26+
}
27+
}
28+
29+
private void generate(){
30+
Coordinate start = randomSquare();
31+
frontiers.add(new Coordinate[]{start, start});
32+
33+
34+
while (!frontiers.isEmpty()){
35+
int randF = (int)(Math.random() * frontiers.size());
36+
Coordinate[] fSq = frontiers.remove(randF);
37+
// for (Coordinate c : fSq){
38+
// System.out.println(c);
39+
// }
40+
41+
int r1 = fSq[0].getX();
42+
int c1 = fSq[0].getY();
43+
int r2 = fSq[1].getX();
44+
int c2 = fSq[1].getY();
45+
46+
if (maze[r2][c2].getType().equals("#")){
47+
maze[r1][c1] = new Square(" ");
48+
maze[r2][c2] = new Square(" ");
49+
50+
51+
if (c2 > 1 && maze[r2][c2 - 2].getType().equals("#")){
52+
Coordinate cor1 = new Coordinate(r2, c2 - 1);
53+
Coordinate cor2 = new Coordinate(r2, c2 - 2);
54+
frontiers.add(new Coordinate[]{cor1, cor2});
55+
}
56+
if (r2 > 1 && maze[r2 - 2][c2].getType().equals("#")){
57+
Coordinate cor1 = new Coordinate(r2 - 1, c2);
58+
Coordinate cor2 = new Coordinate(r2 - 2, c2);
59+
frontiers.add(new Coordinate[]{cor1, cor2});
60+
}
61+
if (c2 < (col - 2) && maze[r2][c2 + 2].getType().equals("#")){
62+
Coordinate cor1 = new Coordinate(r2, c2 + 1);
63+
Coordinate cor2 = new Coordinate(r2, c2 + 2);
64+
frontiers.add(new Coordinate[]{cor1, cor2});
65+
}
66+
if (r2 < (row - 2) && maze[r2 + 2][c2].getType().equals("#")){
67+
Coordinate cor1 = new Coordinate(r2 + 1, c2);
68+
Coordinate cor2 = new Coordinate(r2 + 2, c2);
69+
frontiers.add(new Coordinate[]{cor1, cor2});
70+
}
71+
}
72+
}
73+
}
74+
75+
// returns coordinates of random square within borders
76+
private Coordinate randomSquare(){
77+
int x = (int)(Math.random() * (row - 2)) + 1; // excludes border squares
78+
int y = (int)(Math.random() * (col - 2)) + 1;
79+
return new Coordinate(x, y);
80+
}
81+
82+
private void addSE(){
83+
boolean check = false;
84+
for (int r = 0; r < row; r++){
85+
if (check)
86+
break;
87+
for (int c = 0; c < col; c++){
88+
if (maze[r][c].getType().equals(" ")){
89+
maze[r][c] = new Square("S");
90+
check = true;
91+
break;
92+
}
93+
}
94+
}
95+
96+
check = false;
97+
for (int r = row - 1; r >= 0; r--){
98+
if (check)
99+
break;
100+
for (int c = col - 1; c >= 0; c--){
101+
if (maze[r][c].getType().equals(" ")){
102+
maze[r][c] = new Square("E");
103+
check = true;
104+
break;
105+
}
106+
}
107+
}
108+
}
109+
/*
110+
private void writeToFile() throws IOException{
111+
try {
112+
PrintWriter writer = new PrintWriter("Mazes/primsgeneratedfile.dat", "UTF-8");
113+
writer.println(this.toString());
114+
writer.close();
115+
}
116+
catch (IOException e){
117+
throw new IOException("unable to write to file");
118+
}
119+
}
120+
*/
121+
private static String removeDoubleWalls(String content){
122+
// check and remove double top wall
123+
int width = widthString(content);
124+
int iS = indexS(content);
125+
if (iS > (widthString(content) * 2)){ // double wall on top
126+
// System.out.println("removed totopp wall");
127+
content = content.substring(width + 1);
128+
}
129+
130+
// check and remove double left wall
131+
iS = indexS(content);
132+
width = widthString(content);
133+
if (iS == width + 3){ // double wall on left
134+
// System.out.println("removed lefleftt wall");
135+
for (int i = 0; i < content.length(); i += width){
136+
content = content.substring(0, i) + content.substring(i + 1);
137+
}
138+
}
139+
140+
// check and remove double right wall
141+
int iE = indexE(content);
142+
width = widthString(content);
143+
if (content.substring(iE + 3, iE + 4).equals("\n")){ // double wall on right
144+
// System.out.println("removed right wall");
145+
for (int i = width - 1; i < content.length(); i += width){
146+
content = content.substring(0, i) + content.substring(i + 1);
147+
}
148+
}
149+
150+
// check and remove double bottom wall
151+
iE = indexE(content);
152+
width = widthString(content);
153+
if (iE < content.length() - (width * 2)){ // double wall on bottom
154+
// System.out.println("removed bottom wall");
155+
content = content.substring(0, content.length() - width);
156+
}
157+
158+
return content;
159+
}
160+
161+
private static int indexS(String s){
162+
return s.indexOf("S");
163+
}
164+
165+
private static int indexE(String s){
166+
return s.indexOf("E");
167+
}
168+
169+
private static int widthString(String s){
170+
return s.indexOf("\n");
171+
}
172+
173+
private static int heightString(String s){
174+
int height = 1;
175+
String search = s;
176+
while (search.indexOf("\n") != -1){
177+
height++;
178+
search = search.substring(search.indexOf("\n") + 1);
179+
}
180+
return height;
181+
}
182+
183+
public String toString(){
184+
String output = "";
185+
186+
int c = col + 2;
187+
while (c > 0){
188+
output += "#";
189+
c--;
190+
}
191+
output += "\n";
192+
193+
for (Square[] row : maze){
194+
output += "#";
195+
for (Square s : row){
196+
output += s.getType();
197+
}
198+
output += "#\n";
199+
}
200+
201+
c = col + 2;
202+
while (c > 0){
203+
output += "#";
204+
c--;
205+
}
206+
207+
output = removeDoubleWalls(output);
208+
209+
if (output.substring(output.length() - 1).equals("\n"))
210+
output = output.substring(0, output.length() - 1);
211+
212+
return output;
213+
}
214+
215+
public static void main(String[] args) throws IOException{
216+
PrimsGenerator pg = new PrimsGenerator(100, 200);
217+
System.out.print(pg);
218+
}
219+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Oleksandr Taradachuk - Prims Generator
2+
Generates maze string - using Prim's generation - with start of maze in top left and bottom of maze in bottom left with the ability to write to a file if desired.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
public class Square{
2+
private String type;
3+
private int colorR, colorG, colorB;
4+
private static final String allTypes = "# SE*?.";
5+
6+
// constructor
7+
public Square(String s){
8+
checkValidity(s);
9+
type = s;
10+
setColors();
11+
}
12+
13+
// checks if maze block is one of: "# SE*"
14+
// # wall, _ empty space, S start, E end, * path
15+
private void checkValidity(String s) throws IllegalArgumentException{
16+
if ("# SE*?.".indexOf(s) == -1){
17+
throw new IllegalArgumentException("Maze content includes an invalid type: " + s);
18+
}
19+
}
20+
21+
// SETTING COLOR OF SQUARE
22+
private void setColors(){
23+
colorR = setColorR();
24+
colorG = setColorG();
25+
colorB = setColorB();
26+
}
27+
28+
private int setColorR(){
29+
int[] reds = {0, 255, 0, 255, 255,0,0};
30+
return reds[allTypes.indexOf(type)];
31+
}
32+
33+
private int setColorG(){
34+
int[] greens = {0, 255, 255, 0, 165,255,0};
35+
return greens[allTypes.indexOf(type)];
36+
}
37+
38+
private int setColorB(){
39+
int[] blues = {0, 255, 0, 0, 0,0,255};
40+
return blues[allTypes.indexOf(type)];
41+
}
42+
43+
44+
45+
// ACCESSOR METHODS
46+
public String getType(){
47+
return type;
48+
}
49+
50+
51+
public int[] getColor(){ // returns int array of RGB colors
52+
int[] output = {colorR, colorG, colorB};
53+
return output;
54+
}
55+
56+
// returns "Square type: ___...Color RGB: _,_,_"
57+
public String toString(){
58+
return "Square type: " + type + "\nColor RGB: [" + colorR + "," + colorG + "," + colorB + "]";
59+
}
60+
}

0 commit comments

Comments
 (0)