View FetcherTwo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("FetcherTwo") { | |
lateinit var asyncQueue: AsyncQueue | |
lateinit var fetcher: FetcherTwo | |
val client = Client() | |
beforeEachTest { | |
asyncQueue = AsyncQueue() | |
fetcher = FetcherTwo(asyncQueue, client) | |
} |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val firstWins = when (first to second) { | |
SCISSORS to PAPER, | |
PAPER to ROCK, | |
ROCK to SCISSORS -> true | |
else -> false | |
} |
View GameRules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val firstWins = rules(first, second) { | |
SCISSORS beats PAPER || | |
PAPER beats ROCK || | |
ROCK beats SCISSORS | |
} | |
if (firstWins) { | |
return FIRST_PLAYER | |
} |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
View Game.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (first == SCISSORS && second != ROCK || | |
first == PAPER && second != SCISSORS || | |
first == ROCK && second != PAPER) { | |
return FIRST_PLAYER | |
} |
View Throw.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Throw { | |
val winsAgainst: List<Throw> | |
fun beats(other: Throw) = winsAgainst.contains(other) | |
object SCISSORS : Throw { | |
override val winsAgainst = listOf(PAPER, LIZARD) | |
} | |
object PAPER : Throw { | |
override val winsAgainst = listOf(ROCK, SPOCK) |
NewerOlder