obsolete tests deleted

javautil modified
kotlin_lib.js now inside a function
strange bug with js_lib
raphael.kt added
This commit is contained in:
Pavel Talanov
2012-01-31 18:15:47 +04:00
parent 0f823a64f4
commit ccbc4c7ff6
23 changed files with 1849 additions and 1547 deletions
+388 -390
View File
File diff suppressed because it is too large Load Diff
+7
View File
@@ -0,0 +1,7 @@
package js.annotations;
annotation class NativeClass() {}
annotation class NativeFun(name : String)
annotation class LibraryClass() {}
annotation class LibraryFun(name : String) {}
+9 -2
View File
@@ -1,10 +1,17 @@
package js;
import js.annotations.LibraryFun
import js.annotations.LibraryClass
LibraryFun("println")
fun println()
LibraryFun("println")
fun println(s : Any?)
LibraryFun("print")
fun print(s : Any?)
LibraryFun("parseInt")
fun parseInt(s : String) : Int
LibraryClass
open class Exception()
LibraryClass
class NumberFormatException() : Exception()
+50 -94
View File
@@ -1,23 +1,35 @@
package java.util
import java.lang;
import js.annotations.LibraryClass
import js.annotations.*;
//LibraryClass
//public trait Comparator<T> {
// fun compare(obj1 : T, obj2 : T) : Int;
//}
//
//LibraryFun("comparator")
//public fun comparator<T>(f : (T, T) -> Int) : Comparator<T> {}
//
//LibraryFun("max")
//public fun max<T>(col : Collection<T>, comp : Comparator<T>) {}
LibraryClass
public trait Iterator<T> {
fun hasNext() : Boolean
fun next() : T
public open class Iterator<T>() {
open fun next() : T {}
open fun hasNext() : Boolean {}
}
LibraryClass
public open class ArrayList<erased E>() : java.util.List<E>, java.util.AbstractList<E>() {
public open class ArrayList<erased E>() : java.util.List<E> {
override public fun size() : Int {}
override public fun isEmpty() : Boolean {}
override public fun contains(o : Any?) : Boolean {}
override public fun indexOf(o : Any?) : Int {}
override public fun lastIndexOf(o : Any?) : Int {}
override public fun toArray() : Array<Any?> {}
override public fun toArray<erased T>(a : Array<out T>) : Array<T> {}
override public fun iterator() : Iterator<E> {}
// override public fun indexOf(o : Any?) : Int {}
// override public fun lastIndexOf(o : Any?) : Int {}
// override public fun toArray() : Array<Any?> {}
// override public fun toArray<erased T>(a : Array<out T>) : Array<T> {}
override public fun get(index : Int) : E {}
override public fun set(index : Int, element : E) : E {}
override public fun add(e : E) : Boolean {}
@@ -26,7 +38,7 @@ public open class ArrayList<erased E>() : java.util.List<E>, java.util.AbstractL
override public fun remove(o : Any?) : Boolean {}
override public fun clear() : Unit {}
override public fun addAll(c : java.util.Collection<out E>) : Boolean {}
override public fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean {}
// override public fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean {}
}
LibraryClass
@@ -34,15 +46,15 @@ public trait Collection<erased E> : java.lang.Iterable<E> {
open fun size() : Int
open fun isEmpty() : Boolean
open fun contains(o : Any?) : Boolean
override fun iterator() : Iterator<E>
open fun toArray() : Array<Any?>
open fun toArray<erased T>(a : Array<out T>) : Array<T>
override fun iterator() : java.util.Iterator<E>
// open fun toArray() : Array<Any?>
// open fun toArray<erased T>(a : Array<out T>) : Array<T>
open fun add(e : E) : Boolean
open fun remove(o : Any?) : Boolean
open fun containsAll(c : java.util.Collection<*>) : Boolean
//open fun containsAll(c : java.util.Collection<*>) : Boolean
open fun addAll(c : java.util.Collection<out E>) : Boolean
open fun removeAll(c : java.util.Collection<*>) : Boolean
open fun retainAll(c : java.util.Collection<*>) : Boolean
//open fun removeAll(c : java.util.Collection<*>) : Boolean
//open fun retainAll(c : java.util.Collection<*>) : Boolean
open fun clear() : Unit
}
@@ -51,56 +63,24 @@ public trait List<erased E> : java.util.Collection<E> {
override fun size() : Int
override fun isEmpty() : Boolean
override fun contains(o : Any?) : Boolean
override fun iterator() : Iterator<E>
override fun toArray() : Array<Any?>
override fun iterator() : java.util.Iterator<E>
// override fun toArray() : Array<Any?>
// Simulate Java's array covariance
override fun toArray<erased T>(a : Array<out T>) : Array<T>
// override fun toArray<erased T>(a : Array<out T>) : Array<T>
override fun add(e : E) : Boolean
override fun remove(o : Any?) : Boolean
override fun containsAll(c : java.util.Collection<*>) : Boolean
// override fun containsAll(c : java.util.Collection<*>) : Boolean
override fun addAll(c : java.util.Collection<out E>) : Boolean
open fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean
override fun removeAll(c : java.util.Collection<*>) : Boolean
override fun retainAll(c : java.util.Collection<*>) : Boolean
// open fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean
// override fun removeAll(c : java.util.Collection<*>) : Boolean
// override fun retainAll(c : java.util.Collection<*>) : Boolean
override fun clear() : Unit
open fun get(index : Int) : E
open fun set(index : Int, element : E) : E
open fun add(index : Int, element : E) : Unit
open fun remove(index : Int) : E
open fun indexOf(o : Any?) : Int
open fun lastIndexOf(o : Any?) : Int
}
LibraryClass
abstract public class AbstractCollection<erased E> protected () : java.util.Collection<E> {
abstract override public fun iterator() : java.util.Iterator<E>
abstract override public fun size() : Int
override public fun isEmpty() : Boolean {}
override public fun contains(o : Any?) : Boolean {}
override public fun toArray() : Array<Any?> {}
override public fun toArray<erased T>(a : Array<out T>) : Array<T> {}
override public fun add(e : E) : Boolean {}
override public fun remove(o : Any?) : Boolean {}
override public fun containsAll(c : java.util.Collection<*>) : Boolean {}
override public fun addAll(c : java.util.Collection<out E>) : Boolean {}
override public fun removeAll(c : java.util.Collection<*>) : Boolean {}
override public fun retainAll(c : java.util.Collection<*>) : Boolean {}
override public fun clear() : Unit {}
}
LibraryClass
abstract public open class AbstractList<erased E> protected () : java.util.AbstractCollection<E>(), java.util.List<E> {
override public fun add(e : E) : Boolean {}
abstract override public fun get(index : Int) : E
override public fun set(index : Int, element : E) : E {}
override public fun add(index : Int, element : E) : Unit {}
override public fun remove(index : Int) : E {}
override public fun indexOf(o : Any?) : Int {}
override public fun lastIndexOf(o : Any?) : Int {}
override public fun clear() : Unit {}
override public fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean {}
override public fun iterator() : Iterator<E> {}
open protected fun removeRange(fromIndex : Int, toIndex : Int) : Unit {}
// open fun indexOf(o : Any?) : Int
// open fun lastIndexOf(o : Any?) : Int
}
LibraryClass
@@ -108,38 +88,28 @@ public trait Set<erased E> : java.util.Collection<E> {
override fun size() : Int
override fun isEmpty() : Boolean
override fun contains(o : Any?) : Boolean
override fun iterator() : Iterator<E>
override fun toArray() : Array<Any?>
override fun toArray<erased T>(a : Array<out T>) : Array<T>
override fun iterator() : java.util.Iterator<E>
// override fun toArray() : Array<Any?>
// override fun toArray<erased T>(a : Array<out T>) : Array<T>
override fun add(e : E) : Boolean
override fun remove(o : Any?) : Boolean
override fun containsAll(c : java.util.Collection<*>) : Boolean
//override fun containsAll(c : java.util.Collection<*>) : Boolean
override fun addAll(c : java.util.Collection<out E>) : Boolean
override fun retainAll(c : java.util.Collection<*>) : Boolean
override fun removeAll(c : java.util.Collection<*>) : Boolean
//override fun retainAll(c : java.util.Collection<*>) : Boolean
//override fun removeAll(c : java.util.Collection<*>) : Boolean
override fun clear() : Unit
}
LibraryClass
abstract public class AbstractSet<erased E> protected () : java.util.AbstractCollection<E>(), java.util.Set<E> {
override public fun removeAll(c : java.util.Collection<*>) : Boolean {}
}
LibraryClass
public open class HashSet<erased E>() : java.util.Set<E> {
override public fun iterator() : Iterator<E> {}
override public fun iterator() : java.util.Iterator<E> {}
override public fun size() : Int {}
override public fun isEmpty() : Boolean {}
override public fun contains(o : Any?) : Boolean {}
override public fun add(e : E) : Boolean {}
override public fun remove(o : Any?) : Boolean {}
override fun toArray() : Array<Any?> {}
override fun toArray<erased T>(a : Array<out T>) : Array<T> {}
override fun containsAll(c : java.util.Collection<*>) : Boolean {}
override fun addAll(c : java.util.Collection<out E>) : Boolean {}
override fun retainAll(c : java.util.Collection<*>) : Boolean {}
override fun removeAll(c : java.util.Collection<*>) : Boolean {}
override fun clear() : Unit {}
override public fun clear() : Unit {}
override fun addAll(c : java.util.Collection<out E>) : Boolean
}
LibraryClass
@@ -148,7 +118,7 @@ public trait Map<erased K, erased V> {
open fun isEmpty() : Boolean
open fun containsKey(key : Any?) : Boolean
open fun containsValue(value : Any?) : Boolean
open fun get(key : Any?) : V
open fun get(key : Any?) : V?
open fun put(key : K, value : V) : V?
open fun remove(key : Any?) : V?
open fun putAll(m : java.util.Map<out K, out V>) : Unit
@@ -157,28 +127,13 @@ public trait Map<erased K, erased V> {
open fun values() : java.util.Collection<V>
}
LibraryClass
abstract public open class AbstractMap<erased K, erased V> protected () : java.util.Map<K, V> {
override public fun size() : Int {}
override public fun isEmpty() : Boolean {}
override public fun containsValue(value : Any?) : Boolean {}
override public fun containsKey(key : Any?) : Boolean {}
override public fun get(key : Any?) : V {}
override public fun put(key : K, value : V) : V? {}
override public fun remove(key : Any?) : V? {}
override public fun putAll(m : java.util.Map<out K, out V>) : Unit {}
override public fun clear() : Unit {}
override public fun keySet() : java.util.Set<K> {}
override public fun values() : java.util.Collection<V> {}
}
LibraryClass
public open class HashMap<erased K, erased V>() : java.util.Map<K, V> {
override public fun size() : Int {}
override public fun isEmpty() : Boolean {}
override public fun get(key : Any?) : V {}
override public fun containsKey(key : Any?) : Boolean {}
override public fun put(key : K, value : V) : V? {}
override public fun put(key : K, value : V) : V {}
override public fun putAll(m : java.util.Map<out K, out V>) : Unit {}
override public fun remove(key : Any?) : V? {}
override public fun clear() : Unit {}
@@ -192,3 +147,4 @@ public class StringBuilder() {
public fun append(obj : Any) : StringBuilder
public fun toString() : String
}
+11
View File
@@ -0,0 +1,11 @@
package js
import java.util.*;
class Json() {
fun set(paramName : String, value : Any?) {}
fun get(paramName : String) : Any? = null
}
fun <K, V> Map<K, V>.toJson() : Json = Json()
fun json(vararg pairs : Tuple2<String, Any?>) = Json()
+9 -5
View File
@@ -2,17 +2,21 @@ package jquery;
import js.annotations.*;
fun JQuery.toString() = ""
NativeClass
class JQuery() {
fun addClass(className : String) : JQuery = this;
fun addClass(f : DomElement.(Int, String)->String) = this;
fun attr(attrName : String) = ""
fun attr(attrName : String, value : String) = this
fun attr(attrName : String) = "";
fun attr(attrName : String, value : String) = this;
fun html() : String = "";
fun html(s : String) = this;
fun html(f : DomElement.(Int, String)->String) = this;
fun hasClass(className : String) = true
fun removeClass(className : String) = this
fun html() : String = ""
fun height() = 0
fun width() = 0
fun click() = this;
+34
View File
@@ -0,0 +1,34 @@
package raphael
import js.annotations.*;
import js.*;
NativeClass
class Element() {
fun attr(name : String, value : String) : Element = this
fun attr(nameToValue : Json) : Element = this
fun mouseover(handler : () -> Unit) = this
fun getTotalLength() : Double = 0.0
//fun mouse
}
NativeClass
class Paper() {
fun path(pathString : String) : Element = Element();
fun path() : Element = Element();
fun ellipse(x : Int, y : Int, rx : Int, ry : Int) : Element = Element();
}
NativeFun("Raphael")
fun Raphael(elementId : String, width : Int, height : Int) : Paper = Paper();
NativeFun("Raphael")
fun Raphael(elementId : String, width : Int, height : Int, initFun : Paper.() -> Unit) : Unit {}
NativeClass
val Raphael = object {
fun getColor() : String = ""
}
@@ -28,7 +28,9 @@ public abstract class Config {
PATH_TO_JS_LIB_SRC + "\\jquery\\common.kt",
PATH_TO_JS_LIB_SRC + "\\core\\javautil.kt",
PATH_TO_JS_LIB_SRC + "\\core\\javalang.kt",
PATH_TO_JS_LIB_SRC + "\\core\\core.kt"
PATH_TO_JS_LIB_SRC + "\\core\\core.kt",
PATH_TO_JS_LIB_SRC + "\\raphael\\raphael.kt",
PATH_TO_JS_LIB_SRC + "\\core\\json.kt"
);
@@ -2,7 +2,6 @@ package org.jetbrains.k2js.test;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import java.util.Arrays;
@@ -38,29 +37,6 @@ public class KotlinLibTest extends TranslationTest {
//TODO: refactor
@Test
public void classObjectHasCreateMethod() throws Exception {
final Map<String, Class<? extends Scriptable>> propertyToType
= new HashMap<String, Class<? extends Scriptable>>();
propertyToType.put("create", Function.class);
runPropertyTypeCheck("Class", propertyToType);
}
@Test
public void namespaceObjectHasCreateMethod() throws Exception {
final Map<String, Class<? extends Scriptable>> propertyToType
= new HashMap<String, Class<? extends Scriptable>>();
propertyToType.put("create", Function.class);
runPropertyTypeCheck("Namespace", propertyToType);
}
@Test
public void traitObjectHasCreateMethod() throws Exception {
final Map<String, Class<? extends Scriptable>> propertyToType
= new HashMap<String, Class<? extends Scriptable>>();
propertyToType.put("create", Function.class);
runPropertyTypeCheck("Trait", propertyToType);
}
@Test
public void createdTraitIsJSObject() throws Exception {
@@ -1,7 +1,5 @@
package org.jetbrains.k2js.test;
import org.junit.Test;
/**
* @author Pavel Talanov
*/
@@ -13,9 +11,9 @@ public final class StdlibTest extends TranslationTest {
return MAIN;
}
//TODO: test!
@Test
public void filter() throws Exception {
// testFooBoxIsTrue("Filter.kt");
}
//TODO: cant do due to big java dependencies
// @Test
// public void filter() throws Exception {
// testFooBoxIsTrue("Filter.kt");
// }
}
@@ -20,7 +20,7 @@ public final class WebDemoExamples2Test extends TranslationTest {
testWithMain("bottles", "");
}
//TODO: a couple of classes not supported
//TODO: a couple of classes not supported and objects
// @Test
// public void life() throws Exception {
// testWithMain("life", "", "2");
@@ -31,4 +31,10 @@ public final class WebDemoExamples2Test extends TranslationTest {
testWithMain("builder", "");
testWithMain("builder", "1", "over9000");
}
//TODO: comparator dependencies
// @Test
// public void maze() throws Exception {
// testWithMain("maze", "");
// }
}
@@ -0,0 +1,8 @@
package foo
import java.util.Collection;
import java.util.Comparator.
fun max<T>(col :Collection<T>, ) {
}
@@ -1,7 +1,7 @@
var A = Class.create();
var B = Class.create(A);
var A = Kotlin.Class.create();
var B = Kotlin.Class.create(A);
var b = new B;
test = function() {
return (isType(b, A) && isType(b, B));
return (Kotlin.isType(b, A) && Kotlin.isType(b, B));
}
@@ -1,22 +1,22 @@
var A = Class.create();
var B = Class.create(A);
var A = Kotlin.Class.create();
var B = Kotlin.Class.create(A);
var b = new B;
var C = Class.create(B);
var C = Kotlin.Class.create(B);
var c = new C;
var E = Class.create(A)
var E = Kotlin.Class.create(A)
var e = new E;
test1 = function() {
b2 = b
return (isType(b, A) && isType(b, B));
return (Kotlin.isType(b, A) && Kotlin.isType(b, B));
}
test2 = function() {
return (isType(c, C) && isType(c, B) && isType(c, A) && (!isType(c, E)));
return (Kotlin.isType(c, C) && Kotlin.isType(c, B) && Kotlin.isType(c, A) && (!Kotlin.isType(c, E)));
}
test3 = function() {
return isType(e, E) && !isType(e, B) && !isType(e, C) && isType(e, A)
return Kotlin.isType(e, E) && !Kotlin.isType(e, B) && !Kotlin.isType(e, C) && Kotlin.isType(e, A)
}
test = function() {
@@ -1,6 +1,6 @@
var A = Class.create();
var B = Class.create(A);
var C = Class.create();
var A = Kotlin.Class.create();
var B = Kotlin.Class.create(A);
var C = Kotlin.Class.create();
var c = new C;
test = function() {
@@ -1,6 +1,6 @@
var A = Class.create();
var A = Kotlin.Class.create();
var a = new A;
test = function() {
return isType(a, A);
return Kotlin.isType(a, A);
}
@@ -1,4 +1,4 @@
foo = Namespace.create({initialize:function(){
foo = Kotlin.Namespace.create({initialize:function(){
}
, box:function(){
return !false;
@@ -1,6 +1,6 @@
{
classes = function(){
var A = Class.create({initialize:function(){
var A = Kotlin.Class.create({initialize:function(){
this.$order = '';
{
this.set_order(this.get_order() + 'A');
@@ -13,14 +13,14 @@
return this.$order;
}
});
var B = Class.create(A, {initialize:function(){
var B = Kotlin.Class.create(A, {initialize:function(){
this.super_init();
{
this.set_order(this.get_order() + 'B');
}
}
});
var C = Class.create(B, {initialize:function(){
var C = Kotlin.Class.create(B, {initialize:function(){
this.super_init();
{
this.set_order(this.get_order() + 'C');
@@ -30,7 +30,7 @@
return {A:A, B:B, C:C};
}
();
foo = Namespace.create(classes, {initialize:function(){
foo = Kotlin.Namespace.create(classes, {initialize:function(){
}
, box:function(){
return (new foo.C).get_order() === 'ABC' && (new foo.B).get_order() === 'AB' && (new foo.A).get_order() === 'A';
@@ -38,8 +38,6 @@
});
}
function test() {
return foo.box()
}
@@ -1,10 +1,10 @@
foo = {};
(function(foo){
foo.Test = Trait.create({addFoo:function(s){
foo.Test = Kotlin.Trait.create({addFoo:function(s){
return s + 'FOO';
}
});
foo.ExtendedTest = Trait.create(foo.Test, {hooray:function(){
foo.ExtendedTest = Kotlin.Trait.create(foo.Test, {hooray:function(){
return 'hooray';
}
});
File diff suppressed because it is too large Load Diff
+28 -29
View File
@@ -1,19 +1,17 @@
package foo
package std
import js.*
import java.util.*
class NoSuchElementException() : Exception() {}
import java.lang.Iterable
/*
Filters given iterator
*/
inline fun <T> Iterator<T>.filter(f: (T)-> Boolean) : Iterator<T> = FilterIterator<T>(this, f)
inline fun <T> java.util.Iterator<T>.filter(f: (T)-> Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, f)
/*
Adds filtered elements in to given container
*/
inline fun <T,U : Collection<in T>> Iterable<T>.filterTo(var container: U, filter: (T)->Boolean) : U {
inline fun <T,U : Collection<in T>> java.lang.Iterable<T>.filterTo(var container: U, filter: (T)->Boolean) : U {
for(element in this) {
if(filter(element))
container.add(element)
@@ -25,37 +23,38 @@ inline fun <T,U : Collection<in T>> Iterable<T>.filterTo(var container: U, filte
Create iterator filtering given java.lang.Iterable
*/
/*
inline fun <T> Iterable<T>.filter(f: (T)->Boolean) : Iterator<T> = (iterator() as Iterator<T>).filter(f)
inline fun <T> java.lang.Iterable<T>.filter(f: (T)->Boolean) : java.util.Iterator<T> = (iterator() as java.util.Iterator<T>).filter(f)
*/
private class FilterIterator<T>(val original: Iterator<T>, val filter: (T)-> Boolean) : Iterator<T> {
private class FilterIterator<T>(val original: java.util.Iterator<T>, val filter: (T)-> Boolean) : java.util.Iterator<T> {
var state = 0
var nextElement: T? = null
override val hasNext: Boolean
get() {
if (state == 1) {
return true;
}
if (state == 2) {
return false;
}
while(original.hasNext) {
val candidate = original.next()
if((filter)(candidate)) {
nextElement = candidate
state = 1;
return true;
}
}
state = 2;
return false;
}
override fun hasNext(): Boolean =
when(state) {
1 -> true // checked and next present
2 -> false // checked and next not present
else -> {
while(original.hasNext()) {
val candidate = original.next()
if((filter)(candidate)) {
nextElement = candidate
state = 1
true
}
}
state = 2
false
}
}
override fun next(): T {
override fun next(): T =
if(state != 1)
throw java.util.NoSuchElementException()
else {
val res = nextElement as T
nextElement = null
state = 0
return res
res
}
}
@@ -0,0 +1,238 @@
/**
* Let's Walk Through a Maze.
*
* Imagine there is a maze whose walls are the big 'O' letters.
* Now, I stand where a big 'I' stands and some cool prize lies
* somewhere marked with a '$' sign. Like this:
*
* OOOOOOOOOOOOOOOOO
* O O
* O$ O O
* OOOOO O
* O O
* O OOOOOOOOOOOOOO
* O O I O
* O O
* OOOOOOOOOOOOOOOOO
*
* 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.*
/**
* This function looks for a path from max.start to maze.end through
* free space (a path does not go through walls). One can move only
* straightly up, down, left or right, no diagonal moves allowed.
*/
fun findPath(maze : Maze) : List<#(Int, Int)>? {
val previous = HashMap<#(Int, Int), #(Int, Int)>
val queue = LinkedList<#(Int, Int)>
val visited = HashSet<#(Int, Int)>
queue.offer(maze.start)
visited.add(maze.start)
while (!queue.isEmpty()) {
val cell = queue.poll()
if (cell == maze.end) break
for (newCell in maze.neighbors(cell._1, cell._2)) {
if (newCell in visited) continue
previous[newCell] = cell
queue.offer(newCell)
visited.add(cell)
}
}
if (previous[maze.end] == null) return null
val path = ArrayList<#(Int, Int)>()
var current = previous[maze.end]
while (current != maze.start) {
path.add(0, current)
current = previous[current]
}
return path
}
/**
* Find neighbors of the (i, j) cell that are not walls
*/
fun Maze.neighbors(i : Int, j : Int) : List<#(Int, Int)> {
val result = ArrayList<#(Int, Int)>
addIfFree(i - 1, j, result)
addIfFree(i, j - 1, result)
addIfFree(i + 1, j, result)
addIfFree(i, j + 1, result)
return result
}
fun Maze.addIfFree(i : Int, j : Int, result : List<#(Int, Int)>) {
if (i !in 0..height-1) return
if (j !in 0..width-1) return
if (walls[i][j]) return
result.add(#(i, j))
}
/**
* A data class that represents a maze
*/
class Maze(
// Number or columns
val width : Int,
// Number of rows
val height : Int,
// true for a wall, false for free space
val walls : Array<out Array<out Boolean>>,
// The starting point (must not be a wall)
val start : #(Int, Int),
// The target point (must not be a wall)
val end : #(Int, Int)
) {
}
/** A few maze examples here */
fun main(args : Array<String>) {
printMaze("I $")
printMaze("I O $")
printMaze("""
O $
O
O
O
O I
""")
printMaze("""
OOOOOOOOOOO
O $ O
OOOOOOO OOO
O O
OOOOO OOOOO
O O
O OOOOOOOOO
O OO
OOOOOO IO
""")
printMaze("""
OOOOOOOOOOOOOOOOO
O O
O$ O O
OOOOO O
O O
O OOOOOOOOOOOOOO
O O I O
O O
OOOOOOOOOOOOOOOOO
""")
}
// UTILITIES
fun printMaze(str : String) {
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("")
}
/**
* 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
* a '$' sign.
*
* Example:
*
* OOOOOOOOOOOOOOOOO
* O O
* O$ O O
* OOOOO O
* O O
* O OOOOOOOOOOOOOO
* O O I O
* O O
* OOOOOOOOOOOOOOOOO
*/
fun makeMaze(s : String) : Maze {
val lines = s.split("\n").sure()
val w = max<String?>(lines.toList(), comparator<String?> {o1, o2 ->
val l1 : Int = o1?.size ?: 0
val l2 = o2?.size ?: 0
l1 - l2
}).sure()
val data = Array<Array<Boolean>>(lines.size) {Array<Boolean>(w.size) {false}}
var start : #(Int, Int)? = null
var end : #(Int, Int)? = null
for (line in lines.indices) {
for (x in lines[line].indices) {
val c = lines[line].sure()[x]
data[line][x] = c == 'O'
when (c) {
'I' -> start = #(line, x)
'$' -> end = #(line, x)
else -> {}
}
}
}
if (start == null) {
throw IllegalArgumentException("No starting point in the maze (should be indicated with 'I')")
}
if (end == null) {
throw IllegalArgumentException("No goal point in the maze (should be indicated with a '$' sign)")
}
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 <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> {
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)
}
fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
for (elem in this)
result.add(elem)
return result
}
fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
@@ -0,0 +1,45 @@
Maze:
I~~$
Result: Path found
Maze:
I O $
Result: No path
Maze:
O $
O ~
O ~
O ~
O ~~~~~~~~~I
Result: Path found
Maze:
OOOOOOOOOOO
O $~~~~~ O
OOOOOOO~OOO
O ~~~ O
OOOOO~OOOOO
O~~~~~ O
O~OOOOOOOOO
O~~~~~~ OO
OOOOOO~~~IO
Result: Path found
Maze:
OOOOOOOOOOOOOOOOO
O ~~~ O
O$~~O~ O
OOOOO~ O
O ~~~~ O
O ~OOOOOOOOOOOOOO
O ~ O I O
O ~~~~~~~~~~~~~ O
OOOOOOOOOOOOOOOOO
Result: Path found