Partial refactor of World into an 'object' and delegating functions to their appropriate objects.

main
Bryson Zimmerman 12 months ago
parent 50f2951f00
commit a5bced1cb1

@ -4,16 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="741ceb24-e493-47c1-9f25-1dbd8b151381" name="Changes" comment="Initial work on data structures. Saving for move to another computer.">
<change afterPath="$PROJECT_DIR$/src/main/kotlin/Mazefinder.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/data/Directions.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/data/TileProperties.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/data/WorldData.kt" afterDir="false" />
<list default="true" id="741ceb24-e493-47c1-9f25-1dbd8b151381" name="Changes" comment="Filled out World class necessities, created appropriate data holders. Should be ready for Mazefinder">
<change afterPath="$PROJECT_DIR$/src/main/kotlin/data/Tile.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/Pathfinder.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/Pathfinder.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/Tile.kt" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/Walls.kt" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/World.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/World.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/data/WorldData.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/data/WorldData.kt" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -30,8 +25,8 @@
<list>
<option value="JUnit5 Test Class" />
<option value="Kotlin Enum" />
<option value="Kotlin Class" />
<option value="Kotlin Data Class" />
<option value="Kotlin Class" />
</list>
</option>
</component>
@ -171,7 +166,15 @@
<option name="project" value="LOCAL" />
<updated>1728930163649</updated>
</task>
<option name="localTasksCounter" value="5" />
<task id="LOCAL-00005" summary="Filled out World class necessities, created appropriate data holders. Should be ready for Mazefinder">
<option name="closed" value="true" />
<created>1729358883715</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1729358883715</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
@ -215,6 +218,7 @@
<MESSAGE value="Project Setup 2" />
<MESSAGE value="Added intellij files... for better or for worse" />
<MESSAGE value="Initial work on data structures. Saving for move to another computer." />
<option name="LAST_COMMIT_MESSAGE" value="Initial work on data structures. Saving for move to another computer." />
<MESSAGE value="Filled out World class necessities, created appropriate data holders. Should be ready for Mazefinder" />
<option name="LAST_COMMIT_MESSAGE" value="Filled out World class necessities, created appropriate data holders. Should be ready for Mazefinder" />
</component>
</project>

@ -1,6 +1,7 @@
package technology.zim
import technology.zim.data.Directions
import technology.zim.data.Tile
import technology.zim.data.TileProperties
import technology.zim.data.WorldData
import java.util.*
@ -13,38 +14,35 @@ import java.util.*
//Each element contains a TileProperties
// Which currently only contains the edges of the "graph", stored as directions
class World(private val tiles: WorldData) {
//TODO: Constructor that calls Mazefinder algorithm with given dimensions,
constructor(xmin: Int, ymin: Int) : this(WorldData(xmin, ymin))
object World {
val tiles = WorldData(ArrayList<ArrayList<TileProperties>>())
//Returns a coordinate pair
fun getRandomLocation(): Pair<Int, Int> {
return Pair((0..tiles.data.size).random(), (0..tiles.data[0].size).random())
fun getRandomLocation(): Tile {
return Tile(Pair((0..tiles.data.size).random(), (0..tiles.data[0].size).random()))
}
//Get the properties of the tile at the given coordinates
fun tile(coords: Pair<Int, Int>): TileProperties {
return tiles.data.elementAt(coords.first).elementAt(coords.second)
fun getProperties(tile: Tile): TileProperties {
return tiles.data.elementAt(tile.loc.first).elementAt(tile.loc.second)
}
//TODO: Add function for creating connections
//fun addConnection(at: Pair<Int, Int>, dir: Directions)
//Sort out what cells are connected. Induces some CPU overhead compared to storing a simple list
//Benefit is smaller memory footprint by using references to singleton enums
fun getConnections(at: Pair<Int, Int>): Set<Pair<Int, Int>> {
val listTiles = ArrayList<Pair<Int, Int>>()
for (dir: Directions in tile(at).connections) {
fun getConnections(at: Tile): Set<Tile> {
val listTiles = ArrayList<Tile>()
for (dir: Directions in getProperties(at).connections) {
//Use the ghost of linear algebra to identify potential neighbor tiles
val candidateTile = Pair<Int, Int>(at.first + dir.dif.first, at.second + dir.dif.second)
val candidateTile = Tile(Pair(at.loc.first + dir.dif.first, at.loc.second + dir.dif.second))
//Ensure that the tile is within bounds
if(candidateTile.first > 0 &&
candidateTile.second > 0 &&
candidateTile.first < tiles.data.size &&
candidateTile.second < tiles.data[candidateTile.first].size) {
if(candidateTile.loc.first > 0 &&
candidateTile.loc.second > 0 &&
candidateTile.loc.first < tiles.data.size &&
candidateTile.loc.second < tiles.data[candidateTile.loc.first].size) {
listTiles.add(candidateTile)
}
}

@ -0,0 +1,7 @@
package technology.zim.data
@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
}

@ -5,7 +5,7 @@ package technology.zim.data
@JvmInline
value class WorldData constructor(val data: ArrayList<ArrayList<TileProperties>>) {
constructor(xmin : Int, ymin : Int) : this(ArrayList<ArrayList<TileProperties>>()) {
fun setSize(xmin : Int, ymin : Int) {
with(data) {
this.ensureCapacity(xmin)
this.fill(ArrayList<TileProperties>())

Loading…
Cancel
Save