Implement determineOutcome
This commit is contained in:
parent
9a4f8f6043
commit
8aa7d991d2
|
@ -4,11 +4,17 @@ fun main() {
|
||||||
println("Hello, world")
|
println("Hello, world")
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A possible move in a game of rock-paper-scissors. */
|
/**
|
||||||
enum class Move {
|
* A possible move in a game of rock-paper-scissors.
|
||||||
ROCK,
|
*
|
||||||
PAPER,
|
* [strongAgainst] is a function for lazy evaluation because otherwise we can’t access SCISSORS before it’s been defined.
|
||||||
SCISSORS
|
* 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
|
// purposely not calling this Result to avoid confusion with kotlin.Result and similar
|
||||||
|
@ -19,4 +25,8 @@ enum class Outcome {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Determine the [Outcome] of [first] vs [second] from the perspective of [first]. */
|
/** Determine the [Outcome] of [first] vs [second] from the perspective of [first]. */
|
||||||
fun determineOutcome(first: Move, second: Move): Outcome = TODO()
|
fun determineOutcome(first: Move, second: Move): Outcome = when {
|
||||||
|
first.strongAgainst() == second -> Outcome.WIN
|
||||||
|
first == second -> Outcome.DRAW
|
||||||
|
else -> Outcome.LOSS
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user