Implemented MazeFinder, significant changes to supporting data structures to match developed understanding of Kotlin and Object-Oriented philosophy
parent
a5bced1cb1
commit
fa654e4ce8
@ -1,5 +1,32 @@
|
||||
package technology.zim.data
|
||||
|
||||
enum class Directions(val dif: Pair<Int, Int>) {
|
||||
NORTH(Pair(0, 1)), SOUTH(Pair(0, -1)), EAST(Pair(1, 0)), WEST(Pair(-1, 0))
|
||||
enum class Directions(val value: Pair<Int, Int>) {
|
||||
NORTH(Pair(0, 1)) {
|
||||
override fun opposite(): Directions {
|
||||
return SOUTH
|
||||
}
|
||||
},
|
||||
SOUTH(Pair(0, -1)) {
|
||||
override fun opposite(): Directions {
|
||||
return NORTH
|
||||
}
|
||||
},
|
||||
EAST(Pair(1, 0)) {
|
||||
override fun opposite(): Directions {
|
||||
return WEST
|
||||
}
|
||||
},
|
||||
WEST(Pair(-1, 0)) {
|
||||
override fun opposite(): Directions {
|
||||
return EAST
|
||||
}
|
||||
};
|
||||
|
||||
//Return the opposite direction
|
||||
abstract fun opposite(): Directions
|
||||
|
||||
fun all(): MutableSet<Directions> {
|
||||
return mutableSetOf(NORTH, SOUTH, EAST, WEST)
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,63 @@
|
||||
package technology.zim.data
|
||||
|
||||
import technology.zim.World
|
||||
|
||||
@JvmInline
|
||||
value class Tile(val loc: Pair<Int, Int>) {
|
||||
//Todo: Function for cell to add a connection
|
||||
//Todo: Refactor so Tiles know how to get their connections, not the world
|
||||
value class Tile(val value: Pair<Int, Int>) {
|
||||
|
||||
constructor(x: Int, y: Int): this(Pair(x, y))
|
||||
|
||||
//Connect two tiles together.
|
||||
//Calls utility function on the connected cell
|
||||
fun connect(dir: Directions) {
|
||||
val candidateTile = this+dir
|
||||
|
||||
//Ensure that the tile is within bounds
|
||||
if(candidateTile.isInBounds())
|
||||
{
|
||||
this.getProperties().add(dir)
|
||||
(this + dir).getProperties().add(dir.opposite())
|
||||
}
|
||||
else {
|
||||
//TODO: Consider just silently not connecting out-of-bounds values
|
||||
println("Attempted to connect to outside bounds: <" +
|
||||
candidateTile.value.first + ", " + candidateTile.value.second
|
||||
+ "> From Tile: <" + this.value.first + ", " +
|
||||
this.value.second + ">")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//Gets Tile objects for all connected directions
|
||||
//Used for finding a path through the maze
|
||||
fun getConnections(): MutableSet<Tile> {
|
||||
val listTiles = mutableSetOf<Tile>()
|
||||
for (dir: Directions in getProperties().connections) {
|
||||
//Use the ghost of linear algebra to identify potential neighbor tiles
|
||||
listTiles.add(this+dir)
|
||||
}
|
||||
return listTiles
|
||||
}
|
||||
|
||||
fun isInBounds(): Boolean {
|
||||
return value.first > 0 &&
|
||||
value.second > 0 &&
|
||||
value.first < World.tiles.data.size &&
|
||||
value.second < World.tiles.data[value.second].size
|
||||
}
|
||||
|
||||
//Get the properties of the tile at the given coordinates
|
||||
fun getProperties(): TileProperties {
|
||||
return World.tiles.data.elementAt(value.first).elementAt(value.second)
|
||||
}
|
||||
|
||||
//Get tile at given direction
|
||||
operator fun plus(dir: Directions): Tile {
|
||||
return Tile(value.first + dir.value.first, value.second + dir.value.second)
|
||||
}
|
||||
|
||||
//Get tile at direction opposite of given direction
|
||||
operator fun minus(dir: Directions): Tile {
|
||||
return Tile(value.first - dir.value.first, value.second - dir.value.second)
|
||||
}
|
||||
}
|
@ -1,11 +1,28 @@
|
||||
package technology.zim.data
|
||||
|
||||
import technology.zim.World
|
||||
import technology.zim.World.tiles
|
||||
|
||||
//Data holder for a Tile
|
||||
//Should contain a mutable set of connected directions
|
||||
//Later, can hold jumps to other locations other such fancy features
|
||||
|
||||
//For now, a simple inline class to mitigate memory usage
|
||||
|
||||
|
||||
|
||||
@JvmInline
|
||||
value class TileProperties(val connections:MutableSet<Directions> = mutableSetOf<Directions>()) {
|
||||
|
||||
//Remove a direction from the list of connections
|
||||
fun remove(dir: Directions) {
|
||||
connections.remove(dir)
|
||||
}
|
||||
|
||||
//Add a direction to the list of connections
|
||||
//Should only be accessed by the Tile class
|
||||
fun add(dir: Directions) {
|
||||
connections.add(dir)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue