Skip to content

Instantly share code, notes, and snippets.

Oleksii Fedorov waterlink

Block or report user

Report or block waterlink

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View FetcherTwo.kt
describe("FetcherTwo") {
lateinit var asyncQueue: AsyncQueue
lateinit var fetcher: FetcherTwo
val client = Client()
beforeEachTest {
asyncQueue = AsyncQueue()
fetcher = FetcherTwo(asyncQueue, client)
}
View Game.kt
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
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
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
val firstWins = when (first to second) {
SCISSORS to PAPER,
PAPER to ROCK,
ROCK to SCISSORS -> true
else -> false
}
View GameRules.kt
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
val firstWins = rules(first, second) {
SCISSORS beats PAPER ||
PAPER beats ROCK ||
ROCK beats SCISSORS
}
if (firstWins) {
return FIRST_PLAYER
}
View Game.kt
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
if (first == SCISSORS && second != ROCK ||
first == PAPER && second != SCISSORS ||
first == ROCK && second != PAPER) {
return FIRST_PLAYER
}
View Throw.kt
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)
You can’t perform that action at this time.