Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Last active March 22, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckyruby/f0b137fd63507411095e719934781ee6 to your computer and use it in GitHub Desktop.
Save luckyruby/f0b137fd63507411095e719934781ee6 to your computer and use it in GitHub Desktop.
class RankedHand
attr_reader :category, :cards, :score, :description
RANKINGS = { royal_flush: "19", straight_flush: "18", four_of_a_kind: "17",
full_house: "16", flush: "15", straight: "14", three_of_a_kind: "13",
two_pair: "12", pair: "11", high_card: "10"
} #for scoring purposes, it's necessary that these ranking values be 2 chars long
DESCRIPTIONS = { royal_flush: "Royal Flush", straight_flush: "Straight Flush",
four_of_a_kind: "Four of a Kind", full_house: "Full House",
flush: "Flush", straight: "Straight", three_of_a_kind: "Three of a Kind",
two_pair: "Two Pair", pair: "Pair", high_card: "High Card"
}
def initialize(hand, category)
@cards = hand
@category = category
@score = generate_score(category)
@description = DESCRIPTIONS[category]
end
private
#Generated by concatenating the hand ranking with tiebreak values to form a 12 digit integer for determining the highest hand score amongst players.
def generate_score(category) #assumes cards are already sorted by values descending
tiebreak = case category
when :royal_flush
[] #no tiebreak necessary
when :straight_flush, :straight, :four_of_a_kind
[
cards[4].value_string
]
when :full_house
[
cards[0].value_string, #trips
cards[3].value_string #pair
]
when :three_of_a_kind
[
cards[0].value_string, #trips
cards[3].value_string, #high card
cards[4].value_string #high card
]
when :two_pair
[
cards[0].value_string, #top pair
cards[2].value_string, #bottom pair
cards[4].value_string #high card
]
when :pair
[
cards[0].value_string, #pair
cards[2].value_string, #high card
cards[3].value_string, #high card
cards[4].value_string #high card
]
else
[
cards[0].value_string, #high card
cards[1].value_string, #high card
cards[2].value_string, #high card
cards[3].value_string, #high card
cards[4].value_string #high card
]
end
(RANKINGS[category] + tiebreak.join).ljust(12,"0").to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment