fun main() {
val game = Game()
var gameIsNotEnded = true
while (gameIsNotEnded) {
print("Enter a direction: N/S/E/W:")
gameIsNotEnded = game.makeMove(readLine())
}
}
enum class Directions {
NORTH, SOUTH, EAST, WEST, START, END
}
class Game {
var path = mutableListOf(Directions.START)
val north = {path.add(Directions.NORTH)}
val south = {path.add(Directions.SOUTH)}
val east = {path.add(Directions.EAST)}
val west = {path.add(Directions.WEST)}
val end = {path.add(Directions.END); println("Game Over");false}
fun makeMove(moveDir: String?): Boolean {
return when (moveDir) {
"N" -> move(north) // This passes the 'north' lambda into the move function
"S" -> move(south)
"E" -> move(east)
"W" -> move(west)
else -> move(end)
}
}
}
fun move(where: () -> Boolean): Boolean { // Here we take a lambda as an argument
return where.invoke() // The lambda will be invoked when called
}