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