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 | |
} |
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) |
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
@Test | |
fun `game rules`() { | |
// Classic rules | |
SCISSORS beats PAPER | |
PAPER beats ROCK | |
ROCK beats SCISSORS | |
// Additional modern rules | |
ROCK beats LIZARD | |
LIZARD beats SPOCK |
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: Throw | |
fun beats(other: Throw) = other == winsAgainst | |
object SCISSORS : Throw { | |
override val winsAgainst = PAPER | |
} | |
object PAPER : Throw { | |
override val winsAgainst = ROCK |
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
object SCISSORS : Throw { | |
override fun beats(other: Throw) = other == PAPER | |
} | |
object PAPER : Throw { | |
override fun beats(other: Throw) = other == ROCK | |
} | |
object ROCK : Throw { | |
override fun beats(other: Throw) = other == SCISSORS |
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 { | |
fun beats(other: Throw): Boolean | |
object SCISSORS : Throw { | |
override fun beats(other: Throw) = other != ROCK | |
} | |
object PAPER : Throw { | |
override fun beats(other: Throw) = other != SCISSORS | |
} |
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
enum class Throw { | |
SCISSORS, PAPER, ROCK; | |
fun beats(other: Throw) = | |
this == SCISSORS && other != ROCK || | |
this == PAPER && other != SCISSORS || | |
this == ROCK && other != PAPER | |
} |
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.beats(second)) { | |
return FIRST_PLAYER | |
} |
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 | |
} | |
if (first == SCISSORS && second != ROCK || | |
first == PAPER && second != SCISSORS || | |
first == ROCK && second != PAPER) { | |
return FIRST_PLAYER | |
} | |
return SECOND_PLAYER |
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
package myapp | |
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | |
import com.fasterxml.jackson.module.kotlin.readValue | |
import myapp.ExamplesTest.ExampleResponse.Kitten | |
import org.assertj.core.api.Assertions.assertThat | |
import org.intellij.lang.annotations.Language | |
import org.junit.Test | |
class ExamplesTest { |