I was initially trying to figure out how to use “break” to quit the while loop, but I wasn’t able to figure it out. Turns out, the solution is still simple, just changing the value of a boolean variable upon which the while loop runs:
object Game {
var whileIsTrue = true
fun play() {
while (whileIsTrue) {
print("> Enter your command: ")
println(GameInput(readLine()).processCommand())
}
}
private class GameInput(arg: String?) {
private val input = arg ?: ""
val command = input.split(" ")[0]
val argument = input.split(" ").getOrElse(1, { "" })
fun processCommand() = when (command.toLowerCase()){
"move" -> move(argument)
"quit", "exit" -> {whileIsTrue = false; "Quitting..."}
else -> commandNotFound()
}
private fun commandNotFound() = "I'm not quite sure what you're trying to do!"
}
}