How to resolve the algorithm Poker hand analyser step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Poker hand analyser step by step in the Scala programming language

Table of Contents

Problem Statement

Create a program to parse a single five card poker hand and rank it according to this list of poker hands.

A poker hand is specified as a space separated list of five playing cards. Each input card has two characters indicating face and suit.

Faces are:    a, 2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, k Suits are:    h (hearts),   d (diamonds),   c (clubs),   and   s (spades),   or alternatively,   the unicode card-suit characters:    ♥ ♦ ♣ ♠

Duplicate cards are illegal. The program should analyze a single hand and produce one of the following outputs:

The programs output for the above examples should be displayed here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Poker hand analyser step by step in the Scala programming language

Source code in the scala programming language

val faces = "23456789TJQKA"
val suits = "CHSD"
sealed trait Card
object Joker extends Card
case class RealCard(face: Int, suit: Char) extends Card
val allRealCards = for {
  face <- 0 until faces.size
  suit <- suits
} yield RealCard(face, suit)

def parseCard(str: String): Card = {
  if (str == "joker") {
    Joker
  } else {
    RealCard(faces.indexOf(str(0)), str(1))
  }
}

def parseHand(str: String): List[Card] = {
  str.split(" ").map(parseCard).toList
}

trait HandType {
  def name: String
  def check(hand: List[RealCard]): Boolean
}

case class And(x: HandType, y: HandType, name: String) extends HandType {
  def check(hand: List[RealCard]) = x.check(hand) && y.check(hand)
}

object Straight extends HandType {
  val name = "straight"
  def check(hand: List[RealCard]): Boolean = {
    val faces = hand.map(_.face).toSet
    faces.size == 5 && (faces.min == faces.max - 4 || faces == Set(0, 1, 2, 3, 12))
  }
}

object Flush extends HandType {
  val name = "flush"
  def check(hand: List[RealCard]): Boolean = {
    hand.map(_.suit).toSet.size == 1
  }
}

case class NOfAKind(n: Int, name: String = "", nOccur: Int = 1) extends HandType {
  def check(hand: List[RealCard]): Boolean = {
    hand.groupBy(_.face).values.count(_.size == n) >= nOccur
  }
}

val allHandTypes = List(
  NOfAKind(5, "five-of-a-kind"),
  And(Straight, Flush, "straight-flush"),
  NOfAKind(4, "four-of-a-kind"),
  And(NOfAKind(3), NOfAKind(2), "full-house"),
  Flush,
  Straight,
  NOfAKind(3, "three-of-a-kind"),
  NOfAKind(2, "two-pair", 2),
  NOfAKind(2, "one-pair")
)

def possibleRealHands(hand: List[Card]): List[List[RealCard]] = {
  val realCards = hand.collect { case r: RealCard => r }
  val nJokers = hand.count(_ == Joker)
  allRealCards.toList.combinations(nJokers).map(_ ++ realCards).toList
}

def analyzeHand(hand: List[Card]): String = {
  val possibleHands = possibleRealHands(hand)
  allHandTypes.find(t => possibleHands.exists(t.check)).map(_.name).getOrElse("high-card")
}


val testHands = List(
  "2H 2D 2S KS QD",
  "2H 5H 7D 8S 9D",
  "AH 2D 3S 4S 5S",
  "2H 3H 2D 3S 3D",
  "2H 7H 2D 3S 3D",
  "2H 7H 7D 7S 7C",
  "TH JH QH KH AH",
  "4H 4C KC 5D TC",
  "QC TC 7C 6C 4C",
  "QC TC 7C 7C TD",
  "2H 2D 2S KS joker",
  "2H 5H 7D 8S joker",
  "AH 2D 3S 4S joker",
  "2H 3H 2D 3S joker",
  "2H 7H 2D 3S joker",
  "2H 7H 7D joker joker",
  "TH JH QH joker joker",
  "4H 4C KC joker joker",
  "QC TC 7C joker joker",
  "QC TC 7H joker joker"
)

for (hand <- testHands) {
  println(s"$hand -> ${analyzeHand(parseHand(hand))}")
}


  

You may also check:How to resolve the algorithm Longest string challenge step by step in the AWK programming language
You may also check:How to resolve the algorithm Go Fish step by step in the Python programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Ada programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Clojure programming language