1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
object Poker {
  def whatHand_?(_cs:String) = {
    val rank = Map((0 to 12).map(i => "A23456789TJQK"(i) -> (i+1)).toArray:_*)
    val cs = (0.until(_cs.size, 2)).map(_cs.substring).
               map{s=>(s(0), rank(s(1)))}.toList.sort(_._2<_._2)
    val royalSt_? = cs match{
      case List((_,1),(_,10),(_,11),(_,12),(_,13)) => true
      case _ => false
    }
    val flush_? = cs.forall(cs(0)._1 == _._1)
    val st_? = (1 to 9).map(i=>cs(0)._2==i && cs(4)._2==i+4).exists(true==) || royalSt_?
    val p = (((List(List[(char,int)]())) /: List.make(2, cs)){
              for(i <-_; j <-_) yield j::i
            }.filter(c=>c(0)._2 == c(1)._2).size - 5)/2

    (royalSt_?, flush_?, st_?, p) match {
      case (true, true, _, _) => "Royal flush"
      case (_, true, true,_)  => "Straight flush"
      case (_, true, _ ,_)  => "Flush"
      case (_, _, true ,_)  => "Straight"
      case (_,_,_, 6) => "Four of a kind"
      case (_,_,_, 4) => "Full house"
      case (_,_,_, 3) => "Three of a kind"
      case (_,_,_, 2) => "Two pairs"
      case (_,_,_, 1) => "One pair"
      case _ => "No pair"
    }
  }
}