I hereby claim:
- I am waterlink on github.
- I am alexfedorov_ride (https://keybase.io/alexfedorov_ride) on keybase.
- I have a public key ASAHCQiy_SEsEIuW_DBrDmasFqDpQtefNhvglsyP-FiHpAo
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
describe("FetcherTwo") { | |
lateinit var asyncQueue: AsyncQueue | |
lateinit var fetcher: FetcherTwo | |
val client = Client() | |
beforeEachTest { | |
asyncQueue = AsyncQueue() | |
fetcher = FetcherTwo(asyncQueue, client) | |
} |
fun play(first: Throw, second: Throw): Winner { | |
if (first == second) { | |
return TIE | |
} | |
return when (first vs second) { | |
// classic rules | |
SCISSORS beats PAPER, | |
PAPER beats ROCK, | |
ROCK beats SCISSORS -> FIRST_PLAYER |
return when (first to second) { | |
// classic rules | |
SCISSORS to PAPER, | |
PAPER to ROCK, | |
ROCK to SCISSORS -> FIRST_PLAYER | |
// additional modern rules | |
ROCK to LIZARD, | |
LIZARD to SPOCK, | |
SPOCK to SCISSORS, |
fun play(first: Throw, second: Throw): Winner { | |
if (first == second) { | |
return TIE | |
} | |
return when (first to second) { | |
SCISSORS to PAPER, | |
PAPER to ROCK, | |
ROCK to SCISSORS -> FIRST_PLAYER |
val firstWins = when (first to second) { | |
SCISSORS to PAPER, | |
PAPER to ROCK, | |
ROCK to SCISSORS -> true | |
else -> false | |
} |
private fun rules(first: Throw, | |
second: Throw, | |
block: GameRules.() -> Boolean) = | |
GameRules(first, second).run(block) | |
class GameRules(private val first: Throw, | |
private val second: Throw) { | |
infix fun Throw.beats(other: Throw) = | |
first == this && second == other | |
} |
val firstWins = rules(first, second) { | |
SCISSORS beats PAPER || | |
PAPER beats ROCK || | |
ROCK beats SCISSORS | |
} | |
if (firstWins) { | |
return FIRST_PLAYER | |
} |
val scissorsBeatPaper = first == SCISSORS && second == PAPER | |
val paperBeatsRock = first == PAPER && second == ROCK | |
val rockBeatsScissors = first == ROCK && second == SCISSORS | |
if (scissorsBeatPaper || paperBeatsRock || rockBeatsScissors) { | |
return FIRST_PLAYER | |
} |
if (first == SCISSORS && second != ROCK || | |
first == PAPER && second != SCISSORS || | |
first == ROCK && second != PAPER) { | |
return FIRST_PLAYER | |
} |