Last active
December 4, 2015 19:19
-
-
Save justisr/4db7464d203b917e1dc5 to your computer and use it in GitHub Desktop.
I don't even know what this is. :/ But I've got it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Maze { | |
public static void main(String[] args){ | |
int width = 100; | |
int height = 40; | |
Room[][] maze = new Maze().generateMaze(width, height); | |
for(int i = 0; i < width; i++){ | |
System.out.print(" _"); | |
} | |
System.out.println(); | |
for(int y = 0; y < height; y++){ | |
for(int x = 0; x < width; x++){ | |
if(maze[x][y].leftWall){ | |
System.out.print("|"); | |
}else{ | |
System.out.print(" "); | |
} | |
if(y < height - 1){ | |
if(maze[x][y + 1].topWall){ | |
System.out.print("_"); | |
}else{ | |
System.out.print(" "); | |
} | |
}else{ | |
System.out.print("_"); | |
} | |
} | |
System.out.println("|"); | |
} | |
} | |
public Room[][] generateMaze(int width, int height){ | |
Room[][] maze = new Room[width][height]; | |
List<Room> frontiers = new ArrayList<Room>(); | |
Random r = new Random(); | |
Room startingRoom = new Room(r.nextInt(width), r.nextInt(height)); | |
maze[startingRoom.x][startingRoom.y] = startingRoom; | |
frontiers.add(startingRoom); | |
while(frontiers.size() > 0){ | |
Room chosen = frontiers.get(r.nextInt(frontiers.size())); | |
chosen.frontier = false; | |
List<Direction> emptyDirections = Direction.getEmptyDirections(maze, chosen.x, chosen.y); | |
List<Direction> validDirections = Direction.getValidDirections(maze, chosen.x, chosen.y); | |
if(validDirections.size() > 0){ | |
Direction randomDirection = validDirections.get(r.nextInt(validDirections.size())); | |
switch(randomDirection){ | |
case UP: | |
chosen.topWall = false; | |
break; | |
case DOWN: | |
maze[chosen.x][chosen.y + 1].topWall = false; | |
break; | |
case LEFT: | |
chosen.leftWall = false; | |
break; | |
default: | |
maze[chosen.x + 1][chosen.y].leftWall = false; | |
break; | |
} | |
} | |
for(Direction direction : emptyDirections){ | |
Room room; | |
switch(direction){ | |
case UP: | |
room = new Room(chosen.x, chosen.y - 1); | |
break; | |
case DOWN: | |
room = new Room(chosen.x, chosen.y + 1); | |
break; | |
case LEFT: | |
room = new Room(chosen.x - 1, chosen.y); | |
break; | |
default: | |
room = new Room(chosen.x + 1, chosen.y); | |
break; | |
} | |
maze[room.x][room.y] = room; | |
frontiers.add(room); | |
} | |
frontiers.remove(chosen); | |
} | |
return maze; | |
} | |
public class Room { | |
public boolean topWall = true, leftWall = true, frontier = true; | |
public int x = 0, y = 0; | |
public Room(int x, int y){ | |
this.x = x; | |
this.y = y; | |
} | |
@Override | |
public String toString(){ | |
return "Room{" + | |
"topWall=" + topWall + | |
", leftWall=" + leftWall + | |
", x=" + x + | |
", y=" + y + | |
'}'; | |
} | |
} | |
public enum Direction { | |
UP, DOWN, LEFT, RIGHT; | |
public static List<Direction> getEmptyDirections(Room[][] maze, int x, int y){ | |
List<Direction> directions = new ArrayList<Direction>(); | |
if(x > 0 && maze[x - 1][y] == null){ | |
directions.add(LEFT); | |
} | |
if(y > 0 && maze[x][y - 1] == null){ | |
directions.add(UP); | |
} | |
if(x < maze.length - 1 && maze[x + 1][y] == null){ | |
directions.add(RIGHT); | |
} | |
if(y < maze[x].length - 1 && maze[x][y + 1] == null){ | |
directions.add(DOWN); | |
} | |
return directions; | |
} | |
public static List<Direction> getValidDirections(Room[][] maze, int x, int y){ | |
List<Direction> directions = new ArrayList<Direction>(); | |
if(x > 0 && maze[x - 1][y] != null && maze[x][y].leftWall && !maze[x - 1][y].frontier){ | |
directions.add(LEFT); | |
} | |
if(y > 0 && maze[x][y - 1] != null && maze[x][y].topWall && !maze[x][y - 1].frontier){ | |
directions.add(UP); | |
} | |
if(x < maze.length - 1 && maze[x + 1][y] != null && maze[x + 1][y].leftWall && !maze[x + 1][y].frontier){ | |
directions.add(RIGHT); | |
} | |
if(y < maze[x].length - 1 && maze[x][y + 1] != null && maze[x][y + 1].topWall && !maze[x][y + 1].frontier){ | |
directions.add(DOWN); | |
} | |
return directions; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment