Compare commits

...

6 Commits

@ -4,8 +4,8 @@ import technology.zim.data.Directions
import technology.zim.data.Tile
class HierarchicalPathfinding {
companion object {
//TODO: Run tests of World with nested ArrayList vs TileNavigatedArray
@JvmStatic
fun main(args: Array<String>) {
val n = 1000
@ -17,19 +17,17 @@ class HierarchicalPathfinding {
println("Pathfinding")
startTime = System.currentTimeMillis()
MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n-1, (n-1)))
endTime = System.currentTimeMillis()
val MapBackedPathfinderTime = endTime - startTime
World.tiles.scrubDirections(listOf(Directions.FRONTIER, Directions.INPATH, Directions.NOPATH))
startTime = System.currentTimeMillis()
ArrayBackedPathfinder.generatePath(Tile(0, 0), Tile(n-1, (n-1)))
endTime = System.currentTimeMillis()
val ArrayBackedPathfinderTime = endTime - startTime
//World.scrubDirections(listOf(Directions.FRONTIER, Directions.INPATH, Directions.NOPATH))
startTime = System.currentTimeMillis()
//MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n-1, (n-1)))
endTime = System.currentTimeMillis()
val MapBackedPathfinderTime = endTime - startTime
println(World.toString())
println(n*n)
@ -40,7 +38,7 @@ class HierarchicalPathfinding {
//Clear the maze of pathfinding markers before running another pathfinding algorithm
fun clearMaze() {
World.tiles.scrubDirections(listOf(Directions.FRONTIER, Directions.INPATH))
World.scrubDirections(listOf(Directions.FRONTIER, Directions.INPATH))
}
fun buildMaze(n: Int) {
@ -55,9 +53,6 @@ class HierarchicalPathfinding {
println(World.toString())
println(e.message)
}
}
}
}

@ -46,7 +46,7 @@ object MapBackedPathfinder {
//Otherwise, the tile has been reached and this path is not better, so carry on
gVals.put(candidateTile, currentG + 1)
frontier.insert(candidateTile)
World.update(candidateTile, candidateTile.getProperties() +Directions.FRONTIER)
World.update(candidateTile, candidateTile.getProperties() +Directions.HMFRONTIER)
}
}
} while( current != end)
@ -70,7 +70,7 @@ object MapBackedPathfinder {
var lowestG = Int.MAX_VALUE
var lowestCost = end
while(current != start) {
World.update(current, current.getProperties() + Directions.INPATH)
World.update(current, current.getProperties() + Directions.HMINPATH)
current.getConnections().forEach { candidateTile ->
val candidateG = gVals.get(candidateTile) ?: -1
if(candidateTile.isInBounds() && candidateG != -1 && candidateG < lowestG ) {

@ -1,10 +1,12 @@
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.*
import technology.zim.data.Directions.*
import technology.zim.data.TileNavigatedArray
//Singleton object containing a set of tiles
//Has helper functions included
@ -19,7 +21,7 @@ import technology.zim.data.Directions.*
object World {
//Default size should be 10
val tiles = WorldData(ArrayList<ArrayList<TileProperties>>())
val tiles = TileNavigatedArray<TileProperties>()
var sizeX = 10 //Default size
var sizeY = 10
const val ANSI_RESET = "\u001B[0m"
@ -34,11 +36,11 @@ object World {
fun update(tile: Tile, to: TileProperties) {
tiles.value[tile.x()][tile.y()] = to
tiles.set(tile, to)
}
fun get(tile: Tile): TileProperties {
return tiles.value[tile.x()][tile.y()]
return tiles.get(tile)?: TileProperties(0)
}
//Returns a coordinate pair
@ -49,20 +51,29 @@ object World {
fun setSize(x: Int, y: Int) {
sizeX = x
sizeY = y
tiles.setSize(x, y)
tiles.resize(x, y)
}
//TODO: https://en.wikipedia.org/wiki/Box-drawing_characters
//^ make the maze look like a maze
//Accepts a list of directions, removes those directions from every TileProperties in WorldData
fun scrubDirections(rem: List<Directions>) {
var mask = rem.fold(0) { sum, element -> sum + element.dir}
mask = mask.inv()
tiles.forEachIndexed {
index, tile ->
tiles[index] = TileProperties((tile?:TileProperties(0)).connections and(mask))
}
}
override fun toString(): String {
val str = StringBuilder()
var inPath = false
var checked = false
//Reading left to right, top to bottom - can do a simple for each on the rows
for (y in 0..tiles.value[0].size - 1) {
for (y in 0..sizeY - 1) {
//Upper line: Print each tile, print right-hand connections
for (x in 0..tiles.value[0].size - 1) {
for (x in 0..sizeX - 1) {
if(get(Tile(x, y)).connections and(INPATH.dir) != 0)
inPath = true
else if (get(Tile(x, y)).connections and(FRONTIER.dir) != 0)

@ -7,9 +7,13 @@ enum class Directions(val dir: Int) {
LEFT(4),
RIGHT(8),
MANIFEST(16),//Was added to MazeFinder's frontier, but the pathfinder has a frontier so this is named something other than frontier
FRONTIER(32), //Added to the pathfinder's frontier
INPATH(64), //Chosen by the pathfinder
NOPATH(128) //Tile is not a viable path
FRONTIER(32), //Added to the ArrayBackedPathfinder's frontier
INPATH(64), //Chosen by the ArrayBackedPathfinder
HMFRONTIER(128), //Tile is not a viable path
HMINPATH(256),
BFSFRONTIER(512),
BFSINPATH(1024),
;
companion object {

@ -18,11 +18,11 @@ class TileNavigatedArray<T>(val data: ArrayList<T?> = ArrayList<T?>(), var colSi
}
}
//TODO: Test resize properly
fun resize(colSize:Int, rowSize:Int) {
this.colSize = colSize
this.rowSize = rowSize
for(i in data.size..colSize*rowSize-data.size) {
data.clear()
for(i in 0..rowSize*colSize){
data.add(null)
}
assert(data.size == this.colSize*this.rowSize)

@ -1,23 +1,9 @@
package technology.zim.data
//Data structure wrapper for a set of tiles
//Todo: Test converting to a single row-major array, multiplying the y value by row size to find the element position
//Todo: Convert to TileNavigatedArray
@JvmInline
value class WorldData(val value: ArrayList<ArrayList<TileProperties>>) {
//Accepts a list of directions, removes those directions from every TileProperties in WorldData
fun scrubDirections(rem: List<Directions>) {
var mask = rem.fold(0) { sum, element -> sum + element.dir}
mask = mask.inv()
value.forEachIndexed {
x, row ->
row.forEachIndexed {
y, tile ->
value[x][y] = TileProperties(tile.connections and(mask))
}
}
}
class WorldData(val value: ArrayList<ArrayList<TileProperties>>) {
fun setSize(xmin : Int, ymin : Int) {
val emptyTile = TileProperties(0)
@ -35,7 +21,5 @@ value class WorldData(val value: ArrayList<ArrayList<TileProperties>>) {
x.add(emptyTile)
}
}
//println("WorldData now sized at: " + value.size + ", " + value[0].size)
}
}

@ -13,46 +13,41 @@ class MazeFinderTest {
MazeFinder.primsAlgorithm()
}
//Top row must have at least one south connection and no north connections
@Test
fun topRowOutOfBoundsCheck() {
var southExists = false
World.tiles.value.forEach {
col ->
if(col[0].isSouth())
for(x in 0..<World.sizeX) {
assert(!World.get(Tile(x, 0)).isNorth())
if(World.get(Tile(x, 0)).isSouth())
southExists = true
}
assert(southExists)
}
//Bottom row cannot have any south connections
@Test
fun bottomRowOutOfBoundsCheck() {
var southNotExists = true
World.tiles.value.forEach {
col ->
if(col[9].isSouth())
southNotExists = false
for(x in 0..<World.sizeX) {
assert(!World.get(Tile(x, World.sizeY-1)).isSouth())
}
assert(southNotExists)
}
@Test
fun allTilesVisited() {
World.tiles.value.forEach {
col ->
col.forEach {
tile ->
assert(tile.visited())
for(x in 0..<World.sizeX) {
for(y in 0..<World.sizeY) {
assert(World.get(Tile(x, y)).visited())
}
}
}
@Test
fun allTilesHaveConnections() {
World.tiles.value.forEachIndexed {
x,
col ->
col.forEachIndexed { y,
tileprop ->
for(x in 0..<World.sizeX) {
for(y in 0..<World.sizeY) {
val tileprop = World.get(Tile(x, y))
if(!tileprop.visited())
println("Empty Connections at: " + Tile(x, y).toString())
assert(tileprop.visited())

@ -1,18 +1,24 @@
import technology.zim.ArrayBackedPathfinder
import technology.zim.data.Tile
import technology.zim.data.TileHeap
import kotlin.math.abs
import kotlin.test.Test
import kotlin.test.BeforeTest
//TODO: update to use Tile adjustment to heap
class TileHeapTest {
companion object {
var gVals = HashMap<Tile, Int>()
var heap = TileHeap(Tile(20, 20), gVals)
var heap = TileHeap(Tile(20, 20), this::fValue)
private fun fValue(prospect: Tile, end: Tile): Int {
return hValue(prospect, end).plus(gVals.get(prospect) ?: 0)
}
private fun hValue(prospect: Tile, end:Tile): Int {
return abs(prospect.x() - end.x()) + abs(prospect.y() - end.y())
}
}
@BeforeTest
fun setUp() {
heap = TileHeap(Tile(20, 20), gVals)
heap = TileHeap(Tile(20, 20), Companion::fValue)
arrayOf(Tile(0, 0), Tile(1, 1), Tile(5, 5), Tile(4, 4), Tile(19, 19), Tile(2, 2)).forEach { item ->
heap.insert(item)
}

Loading…
Cancel
Save