rock-paper-scissors/src/main/kotlin/rps/Game.kt

32 lines
912 B
Kotlin
Raw Normal View History

package rps
fun main() {
println("Hello, world")
}
2020-10-22 19:32:38 +02:00
/**
* A possible move in a game of rock-paper-scissors.
*
* [strongAgainst] is a function for lazy evaluation because otherwise we cant access SCISSORS before its been defined.
* If the game needs to be expanded (e.g. the somewhat common rock, paper, scissors, salamander, spock),
* this argument could be a List<Move> instead.
*/
enum class Move(val strongAgainst: () -> Move) {
ROCK({ SCISSORS }),
PAPER({ ROCK }),
SCISSORS({ PAPER })
}
// purposely not calling this Result to avoid confusion with kotlin.Result and similar
enum class Outcome {
WIN,
DRAW,
LOSS
}
/** Determine the [Outcome] of [first] vs [second] from the perspective of [first]. */
2020-10-22 19:32:38 +02:00
fun determineOutcome(first: Move, second: Move): Outcome = when {
first.strongAgainst() == second -> Outcome.WIN
first == second -> Outcome.DRAW
else -> Outcome.LOSS
}