Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Fedorov waterlink

View GitHub Profile
View GameTest.kt
@Test
fun `game rules`() {
// Classic rules
SCISSORS beats PAPER
PAPER beats ROCK
ROCK beats SCISSORS
// Additional modern rules
ROCK beats LIZARD
LIZARD beats SPOCK
View Throw.kt
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
View Throw.kt
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
View Throw.kt
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
}
View Throw.kt
enum class Throw {
SCISSORS, PAPER, ROCK;
fun beats(other: Throw) =
this == SCISSORS && other != ROCK ||
this == PAPER && other != SCISSORS ||
this == ROCK && other != PAPER
}
View Game.kt
if (first.beats(second)) {
return FIRST_PLAYER
}
View Game.kt
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
View ExampleTest.kt
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 {
View HelloWeird.kt
fun main() {
println("Hello World")
}
View ReadFromInput.kt
fun main(args: Array<String>) {
print("What is your name? > ")
val name = readLine()
print("Hello, ")
print(name)
println("! How are things going?")
}