by JeanPierre · 12y ago · 0 comments
Ordering for free is valuable, I guess, but it sort of depends on the situation. Sometimes face cards are worth 10, other times they are worth 11, 12, 13. If you use val King = 10;, then it suddenly is impossible to distinguish between face cards and tens.
If you didn't want the ordering (or the compiler warning when your blackjack implementation omits queens), you could use Scala's symbols, which are more or less the same as Clojure's keywords:
scala> 'king
res0: Symbol = 'king case class Rank private (val rank : Int) extends AnyVal {
override def toString() = {
rank match {
case 1 => "A"
case 11 => "J"
case 12 => "Q"
case 13 => "K"
case _ => rank.toString()
}
}
}
object Rank {
val Ace = Rank(1)
val Two = Rank(2)
// ...
val King = Rank(13)
} card"Heart:Ace"
or card"$suite:Ace" if you want to reference a variable.