You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
79 lines
2.6 KiB
12 months ago
|
package technology.zim
|
||
|
|
||
12 months ago
|
import it.unimi.dsi.util.XoRoShiRo128PlusRandom
|
||
12 months ago
|
import technology.zim.data.Directions
|
||
|
import technology.zim.data.Tile
|
||
12 months ago
|
import technology.zim.data.TileProperties
|
||
|
import technology.zim.util.TrimmableArrayList
|
||
|
import kotlin.collections.ArrayList
|
||
12 months ago
|
|
||
12 months ago
|
|
||
|
//Build the maze
|
||
|
//Options:
|
||
|
//DFS - Long corridors
|
||
|
//Prim's - Solid maze
|
||
|
//Wall-builder?
|
||
|
//https://emmilco.github.io/path_finder/
|
||
|
|
||
|
//http://weblog.jamisbuck.org/2011/1/10/maze-generation-prim-s-algorithm
|
||
|
//http://weblog.jamisbuck.org/2011/1/3/maze-generation-kruskal-s-algorithm
|
||
|
|
||
12 months ago
|
object MazeFinder {
|
||
|
val frontier = TrimmableArrayList<Tile>()
|
||
|
var startingTile = World.getRandomLocation()
|
||
|
val randGen = XoRoShiRo128PlusRandom()
|
||
|
|
||
|
val tempInGraph = ArrayList<Pair<Tile, TileProperties>>()
|
||
12 months ago
|
|
||
|
fun primsAlgorithm() {
|
||
12 months ago
|
cleanUp()
|
||
|
//val randomGen = Random
|
||
|
|
||
|
|
||
|
|
||
|
startingTile = World.getRandomLocation()
|
||
|
startingTile.getProperties().visited = true
|
||
12 months ago
|
//Choose an arbitrary vertex from G (the graph), and add it to some (initially empty) set V.
|
||
12 months ago
|
frontier.addAll(startingTile.getAdjacentTiles(false))
|
||
12 months ago
|
|
||
|
//Choose a random edge from G, that connects a vertex in V with another vertex not in V.
|
||
|
var current: Tile
|
||
12 months ago
|
var inGraph: Pair<Tile, Directions>
|
||
|
var adjacentExplored: Set<Pair<Tile, Directions>>
|
||
12 months ago
|
while(frontier.isNotEmpty()) {
|
||
|
//Grab a random tile from the frontier, mark it as visited
|
||
12 months ago
|
val random = randGen.nextInt(frontier.size())
|
||
|
current = frontier.data[random]
|
||
|
current.getProperties().visited = true
|
||
|
frontier.removeAt(random)
|
||
12 months ago
|
|
||
|
|
||
12 months ago
|
//Find adjacent tiles that are in the graph
|
||
|
adjacentExplored = current.getAdjacent(true)
|
||
12 months ago
|
|
||
12 months ago
|
//Select a random tile from possibleTiles
|
||
|
inGraph = adjacentExplored.elementAt(randGen.nextInt(adjacentExplored.size))
|
||
|
|
||
|
//Connect the frontier tile to the graph
|
||
|
current.connect(inGraph.second)
|
||
|
//Add current's unexplored tiles to the frontier, if not already in frontier
|
||
|
frontier.addAll(current.getAdjacentTiles(false))
|
||
|
|
||
|
|
||
|
//print(World.toString())
|
||
|
//println("--------------------------------------------")
|
||
12 months ago
|
|
||
|
}
|
||
12 months ago
|
println("prim")
|
||
12 months ago
|
}
|
||
|
|
||
|
|
||
12 months ago
|
fun cleanUp() {
|
||
|
//Clean up the frontier
|
||
|
frontier.clear()
|
||
12 months ago
|
}
|
||
|
//Todo: Consider growing World with null values, then use Mazefinder to instantiate as the maze is developed
|
||
|
//Consequence: World becomes nullable, requiring null checks to be added in order to avoid... issues
|
||
|
//It's probable that this will only be needed at large scales
|
||
12 months ago
|
|
||
|
}
|