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" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <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."> <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/Mazefinder.kt" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/kotlin/data/Tile.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" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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/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> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -30,8 +25,8 @@
<list> <list>
<option value="JUnit5 Test Class" /> <option value="JUnit5 Test Class" />
<option value="Kotlin Enum" /> <option value="Kotlin Enum" />
<option value="Kotlin Class" />
<option value="Kotlin Data Class" /> <option value="Kotlin Data Class" />
<option value="Kotlin Class" />
</list> </list>
</option> </option>
</component> </component>
@ -171,7 +166,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1728930163649</updated> <updated>1728930163649</updated>
</task> </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 /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
@ -215,6 +218,7 @@
<MESSAGE value="Project Setup 2" /> <MESSAGE value="Project Setup 2" />
<MESSAGE value="Added intellij files... for better or for worse" /> <MESSAGE value="Added intellij files... for better or for worse" />
<MESSAGE value="Initial work on data structures. Saving for move to another computer." /> <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> </component>
</project> </project>

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

Loading…
Cancel
Save