modified maze example but could not get it to compile
This commit is contained in:
@@ -18,9 +18,6 @@
|
|||||||
* I want to get the prize, and this program helps me do so as soon
|
* I want to get the prize, and this program helps me do so as soon
|
||||||
* as I possibly can by finding a shortest path through the maze.
|
* as I possibly can by finding a shortest path through the maze.
|
||||||
*/
|
*/
|
||||||
package maze
|
|
||||||
|
|
||||||
import java.util.Collections.*
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,10 +28,10 @@ import java.util.*
|
|||||||
fun findPath(maze : Maze) : List<#(Int, Int)>? {
|
fun findPath(maze : Maze) : List<#(Int, Int)>? {
|
||||||
val previous = HashMap<#(Int, Int), #(Int, Int)>
|
val previous = HashMap<#(Int, Int), #(Int, Int)>
|
||||||
|
|
||||||
val queue = ArrayList<#(Int, Int)>
|
val queue = LinkedList<#(Int, Int)>
|
||||||
val visited = HashSet<#(Int, Int)>
|
val visited = HashSet<#(Int, Int)>
|
||||||
|
|
||||||
queue.add(maze.start)
|
queue.offer(maze.start)
|
||||||
visited.add(maze.start)
|
visited.add(maze.start)
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
val cell = queue.poll()
|
val cell = queue.poll()
|
||||||
@@ -134,27 +131,28 @@ fun main(args : Array<String>) {
|
|||||||
// UTILITIES
|
// UTILITIES
|
||||||
|
|
||||||
fun printMaze(str : String) {
|
fun printMaze(str : String) {
|
||||||
val maze = makeMaze(str)
|
val maze = makeMaze(str)
|
||||||
|
|
||||||
println("Maze:")
|
println("Maze:")
|
||||||
val path = findPath(maze)
|
val path = findPath(maze)
|
||||||
for (i in 0..maze.height - 1) {
|
for (i in 0..maze.height - 1) {
|
||||||
for (j in 0..maze.width - 1) {
|
for (j in 0..maze.width - 1) {
|
||||||
val cell = #(i, j)
|
val cell = #(i, j)
|
||||||
print(
|
print(
|
||||||
if (maze.walls[i][j]) "O"
|
if (maze.walls[i][j]) "O"
|
||||||
else if (cell == maze.start) "I"
|
else if (cell == maze.start) "I"
|
||||||
else if (cell == maze.end) "$"
|
else if (cell == maze.end) "$"
|
||||||
else if (path != null && path.contains(cell)) "~"
|
else if (path != null && path.contains(cell)) "~"
|
||||||
else " "
|
else " "
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
println("")
|
println("")
|
||||||
}
|
}
|
||||||
println("Result: " + if (path == null) "No path" else "Path found")
|
println("Result: " + if (path == null) "No path" else "Path found")
|
||||||
println("")
|
println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A maze is encoded in the string s: the big 'O' letters are walls.
|
* A maze is encoded in the string s: the big 'O' letters are walls.
|
||||||
* I stand where a big 'I' stands and the prize is marked with
|
* I stand where a big 'I' stands and the prize is marked with
|
||||||
@@ -207,32 +205,19 @@ fun makeMaze(s : String) : Maze {
|
|||||||
return Maze(w.size, lines.size, data, start.sure(), end.sure())
|
return Maze(w.size, lines.size, data, start.sure(), end.sure())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// An excerpt from the Standard Library
|
// An excerpt from the Standard Library
|
||||||
val String?.indices : IntRange get() = IntRange(0, this.sure().size)
|
val String?.indices : IntRange get() = IntRange(0, this.sure().size)
|
||||||
|
|
||||||
val String.size : Int
|
|
||||||
get() = length
|
|
||||||
|
|
||||||
fun <K, V> Map<K, V>.set(k : K, v : V) { put(k, v) }
|
fun <K, V> Map<K, V>.set(k : K, v : V) { put(k, v) }
|
||||||
|
|
||||||
fun comparator<T> (f : (T, T) -> Int) : Comparator<T> = object : Comparator<T> {
|
fun comparator<T> (f : (T, T) -> Int) : Comparator<T> = object : Comparator<T> {
|
||||||
override fun compare(o1 : T, o2 : T) : Int = f(o1, o2)
|
override fun compare(o1 : T, o2 : T) : Int = f(o1, o2)
|
||||||
}
|
override fun equals(p : Any?) : Boolean = false
|
||||||
|
|
||||||
fun String.split(s : String) = (this as java.lang.String).split(s)
|
|
||||||
|
|
||||||
fun println(message : Any?) {
|
|
||||||
System.out?.println(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun print(message : Any?) {
|
|
||||||
System.out?.print(message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
||||||
for (elem in this)
|
for (elem in this)
|
||||||
result.add(elem)
|
result.add(elem)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user