Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Here is a complete idiomatic Scala hand classifier for all hands (handles 5-high straights):

case class Card(rank: Int, suit: Int) { override def toString = s"${"23456789TJQKA" rank}${"♣♠♦♥" suit}" }

object HandType extends Enumeration {
  val HighCard, OnePair, TwoPair, ThreeOfAKind, Straight, Flush, FullHouse, FourOfAKind, StraightFlush = Value
}

case class Hand(hand: Set[Card]) {
  val (handType, sorted) = {
    def rankMatches(card: Card) = hand count (_.rank == card.rank)
    val groups = hand groupBy rankMatches mapValues {_.toList.sorted}

    val isFlush = (hand groupBy {_.suit}).size == 1
    val isWheel = "A2345" forall {r => hand exists (_.rank == Card.ranks.indexOf(r))}   // A,2,3,4,5 straight
    val isStraight = groups.size == 1 && (hand.max.rank - hand.min.rank) == 4 || isWheel
    val (isThreeOfAKind, isOnePair) = (groups contains 3, groups contains 2)

    val handType = if (isStraight && isFlush)     HandType.StraightFlush
      else if (groups contains 4)                 HandType.FourOfAKind
      else if (isThreeOfAKind && isOnePair)       HandType.FullHouse
      else if (isFlush)                           HandType.Flush
      else if (isStraight)                        HandType.Straight
      else if (isThreeOfAKind)                    HandType.ThreeOfAKind
      else if (isOnePair && groups(2).size == 4)  HandType.TwoPair
      else if (isOnePair)                         HandType.OnePair
      else                                        HandType.HighCard

    val kickers = ((1 until 5) flatMap groups.get).flatten.reverse
    require(hand.size == 5 && kickers.size == 5)
    (handType, if (isWheel) (kickers takeRight 4) :+ kickers.head else kickers)
  }
}

object Hand {
  import scala.math.Ordering.Implicits._
  implicit val rankOrdering = Ordering by {hand: Hand => (hand.handType, hand.sorted)}
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: shopify get values from settings_data.json 
Javascript :: js how to display value in html binding 
Javascript :: firebase is there a way to rename a document 
Javascript :: variables are used to store data values. 
Javascript :: how to disable gravity for an object in matter.js 
Javascript :: angular input .valueChanges.subscribe value 
Javascript :: Create Compond from route to js function communication 
Javascript :: local storage textarea 
Javascript :: persian calender for react-native 
Javascript :: asynchronous file read 
Javascript :: online js to typescript converter 
Javascript :: objection eager loading 
Javascript :: mongoose return full object after inserting data to db 
Javascript :: momentjs isomonth 
Javascript :: how to check if .each first element 
Javascript :: get position/offset of element relative to a parent container 
Javascript :: show more vs editor shortcut key 
Javascript :: table antd dosen t update 
Javascript :: limit frontend request 
Javascript :: html5 web component 
Javascript :: what is the opposite of lazy initialization 
Javascript :: Old Syntax of Router Switch 
Javascript :: imleç 
Javascript :: convert todays date to json datetime 
Javascript :: Function As Parameter In Self Invoking Function 
Javascript :: redwood toaster 
Javascript :: Check If Key Exists For Object 
Javascript :: jquery check screen width 
Javascript :: Backbone Models In Collection Is Added Here 
Javascript :: Mirror Inverse Program in javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =