get rid of 'new'

This commit is contained in:
Dmitry Jemerov
2011-06-15 17:26:33 +02:00
parent c10c19b848
commit 46208ad14f
3 changed files with 23 additions and 23 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ fun main(args: Array<String>) {
}
fun bottlesOfBeer(count: Int): String {
val result = new StringBuilder()
val result = StringBuilder()
result.append(count)
result.append(if (count > 1) " bottles of beer" else " bottle of beer")
return result.toString() ?: ""
+11 -11
View File
@@ -24,17 +24,17 @@ class Evaluator(val expr: StringBuilder, val numbers: ArrayList<Int>) {
in '0'..'9' => {
val n = c - '0'
val index = numbers.indexOf(n)
if (index < 0) throw new Exception("You used incorrect number: " + n)
if (index < 0) throw Exception("You used incorrect number: " + n)
numbers.remove(index) // gotcha: conflict between remove(Object) and remove(int)
return n
}
'(' => {
val result = evaluate()
if (expr.takeFirst() != ')') throw new Exception(") expected")
if (expr.takeFirst() != ')') throw Exception(") expected")
return result
}
'\0' => throw new Exception("Syntax error: Character expected")
else => throw new Exception("Syntax error: Unrecognized character " + c)
'\0' => throw Exception("Syntax error: Character expected")
else => throw Exception("Syntax error: Unrecognized character " + c)
}
}
@@ -58,16 +58,16 @@ class Evaluator(val expr: StringBuilder, val numbers: ArrayList<Int>) {
fun evaluateAll(): Int {
val result = evaluate()
if (expr.length() > 0) throw new Exception("unexpected text: " + expr)
if (expr.length() > 0) throw Exception("unexpected text: " + expr)
return result
}
}
fun main(args: Array<String>) {
System.out?.println("24 game")
val numbers = new ArrayList<Int>(4)
val rnd = new Random();
val prompt = new StringBuilder()
val numbers = ArrayList<Int>(4)
val rnd = Random();
val prompt = StringBuilder()
for(val i in 0..3) {
val n = rnd.nextInt(9) + 1
numbers.add(n)
@@ -76,10 +76,10 @@ fun main(args: Array<String>) {
}
System.out?.println("Your numbers: " + prompt)
System.out?.println("Enter your expression:")
val reader = new BufferedReader(new InputStreamReader(System.`in`))
val expr = new StringBuilder(reader.readLine())
val reader = BufferedReader(InputStreamReader(System.`in`))
val expr = StringBuilder(reader.readLine())
try {
val result = new Evaluator(expr, numbers).evaluateAll()
val result = Evaluator(expr, numbers).evaluateAll()
if (result != 24)
System.out?.println("Sorry, that's " + result)
else
+11 -11
View File
@@ -11,7 +11,7 @@ class World() {
public val world: World get() = this@World
}
public val items = new ArrayList<Item>
public val items = ArrayList<Item>()
class Item(val name: String, var room: Room) {
{
@@ -29,11 +29,11 @@ class World() {
}
}
public val atEndOfRoad = new AtEndOfRoad()
public val atHillInRoad = new AtHillInRoad()
public val insideBuilding = new InsideBuilding()
public val atEndOfRoad = AtEndOfRoad()
public val atHillInRoad = AtHillInRoad()
public val insideBuilding = InsideBuilding()
public val brassLantern = new BrassLantern()
public val brassLantern = BrassLantern()
class AtEndOfRoad(): Room("At End of Road") {
public val west: Room? get() = atHillInRoad
@@ -92,17 +92,17 @@ class EastCommand(): MoveCommand {
}
fun parse(cmd: String): Command? {
if (cmd == "quit") return new QuitCommand()
if (cmd == "west") return new WestCommand()
if (cmd == "east") return new EastCommand()
if (cmd == "quit") return QuitCommand()
if (cmd == "west") return WestCommand()
if (cmd == "east") return EastCommand()
return null
}
fun main(args: Array<String>) {
val w = new World()
val p = new Player(w.startRoom())
val w = World()
val p = Player(w.startRoom())
System.out?.println(p.room.name)
val reader = new BufferedReader(new InputStreamReader(System.`in`))
val reader = BufferedReader(InputStreamReader(System.`in`))
while(true) {
System.out?.print("> ")
val cmd = reader.readLine() as String