dererXII.scalashogi_2.13.5.0.2.source-code.Mode.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalashogi_2.13 Show documentation
Show all versions of scalashogi_2.13 Show documentation
Shogi API written in scala. Immutable and free of side effects.
package shogi
sealed abstract class Mode(val id: Int) {
lazy val name = toString.toLowerCase
def casual = this == Mode.Casual
def rated = this == Mode.Rated
def fold[A](c: => A, r: => A): A = if (this.casual) c else r
}
object Mode {
case object Casual extends Mode(0)
case object Rated extends Mode(1)
val all = List[Mode](Casual, Rated)
val byId: Map[Int, Mode] = all map { v =>
(v.id, v)
} toMap
def apply(id: Int): Option[Mode] = byId get id
def apply(rated: Boolean): Mode = if (rated) Rated else Casual
val default: Mode = Casual
def orDefault(id: Int): Mode = apply(id) | default
}