From 2f5baf8934ebc65bbcb40285080a396db15d6ce4 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Sun, 5 Feb 2012 15:52:29 +0400 Subject: [PATCH] modified maze example but could not get it to compile --- .../testFiles/webDemoExamples2/cases/maze.kt | 65 +++++++------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/translator/testFiles/webDemoExamples2/cases/maze.kt b/translator/testFiles/webDemoExamples2/cases/maze.kt index f599ea9f829..4467dee98a8 100644 --- a/translator/testFiles/webDemoExamples2/cases/maze.kt +++ b/translator/testFiles/webDemoExamples2/cases/maze.kt @@ -18,9 +18,6 @@ * 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. */ -package maze - -import java.util.Collections.* import java.util.* /** @@ -31,10 +28,10 @@ import java.util.* fun findPath(maze : Maze) : List<#(Int, Int)>? { val previous = HashMap<#(Int, Int), #(Int, Int)> - val queue = ArrayList<#(Int, Int)> + val queue = LinkedList<#(Int, Int)> val visited = HashSet<#(Int, Int)> - queue.add(maze.start) + queue.offer(maze.start) visited.add(maze.start) while (!queue.isEmpty()) { val cell = queue.poll() @@ -134,27 +131,28 @@ fun main(args : Array) { // UTILITIES fun printMaze(str : String) { - val maze = makeMaze(str) + val maze = makeMaze(str) - println("Maze:") - val path = findPath(maze) - for (i in 0..maze.height - 1) { - for (j in 0..maze.width - 1) { - val cell = #(i, j) - print( - if (maze.walls[i][j]) "O" - else if (cell == maze.start) "I" - else if (cell == maze.end) "$" - else if (path != null && path.contains(cell)) "~" - else " " - ) - } - println("") - } - println("Result: " + if (path == null) "No path" else "Path found") - println("") + println("Maze:") + val path = findPath(maze) + for (i in 0..maze.height - 1) { + for (j in 0..maze.width - 1) { + val cell = #(i, j) + print( + if (maze.walls[i][j]) "O" + else if (cell == maze.start) "I" + else if (cell == maze.end) "$" + else if (path != null && path.contains(cell)) "~" + else " " + ) + } + println("") + } + println("Result: " + if (path == null) "No path" else "Path found") + println("") } + /** * 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 @@ -207,32 +205,19 @@ fun makeMaze(s : String) : Maze { return Maze(w.size, lines.size, data, start.sure(), end.sure()) } + // An excerpt from the Standard Library val String?.indices : IntRange get() = IntRange(0, this.sure().size) -val String.size : Int - get() = length - fun Map.set(k : K, v : V) { put(k, v) } fun comparator (f : (T, T) -> Int) : Comparator = object : Comparator { - override fun compare(o1 : T, o2 : T) : Int = f(o1, o2) -} - -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) + override fun compare(o1 : T, o2 : T) : Int = f(o1, o2) + override fun equals(p : Any?) : Boolean = false } fun > Array.to(result: C) : C { for (elem in this) - result.add(elem) + result.add(elem) return result } - -fun Array.toList() : List = this.to(ArrayList())