-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rank hands without tiebreaker #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
require "./card" | ||
require "./hand_ranker" | ||
|
||
class Hand | ||
def initialize(cards) | ||
@cards = cards | ||
@rank = HandRanker.new(self).rank | ||
end | ||
|
||
attr_reader :rank, :cards | ||
|
||
def to_s | ||
@cards.join("\n") | ||
end | ||
|
||
def <=>(other_hand) | ||
rank <=> other_hand.rank | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
class HandClassifier | ||
def initialize(hand) | ||
@hand = hand | ||
end | ||
|
||
attr_reader :hand, :rank | ||
|
||
def classify | ||
if straight_flush? | ||
:straight_flush | ||
elsif four_of_a_kind? | ||
:four_of_a_kind | ||
elsif full_house? | ||
:full_house | ||
elsif flush? | ||
:flush | ||
elsif straight? | ||
:straight | ||
elsif three_of_a_kind? | ||
:three_of_a_kind | ||
elsif two_pair? | ||
:two_pair | ||
elsif one_pair? | ||
:one_pair | ||
else | ||
:high_card | ||
end | ||
end | ||
|
||
private | ||
|
||
def straight_flush? | ||
straight? && flush? | ||
end | ||
|
||
def four_of_a_kind? | ||
find_pairings?(4) | ||
end | ||
|
||
def full_house? | ||
grouped_hand = find_matches | ||
full_house = grouped_hand.length == 2 && grouped_hand.any? do |group| | ||
group.length == 2 | ||
end | ||
full_house && grouped_hand.any?{ |group| group.length == 3 } | ||
end | ||
|
||
def flush? | ||
hand.cards.map(&:suit).reduce(true, :==) #i apologize for the accidental obscenity of this code | ||
end | ||
|
||
def straight? | ||
values = hand.cards.map(&:value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More than 5 lines 😠 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was definitely the method i had the most trouble with. I feel like there should be some way to use reduce here but I can't figure it out... |
||
values.sort! | ||
increment = values[0] | ||
match_tracker = true | ||
values.each_with_index do |value, index| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try looking into the |
||
match_tracker = match_tracker && index + increment == value | ||
end | ||
match_tracker | ||
end | ||
|
||
def three_of_a_kind? | ||
find_pairings?(3) | ||
end | ||
|
||
def two_pair? | ||
matches = find_pairings(2) | ||
matches.length == 2 | ||
end | ||
|
||
def one_pair? | ||
find_pairings?(2) | ||
end | ||
|
||
def find_matches | ||
hand.cards.group_by { |card| card.value } | ||
end | ||
|
||
def find_pairings?(length) | ||
find_pairings(length).empty? | ||
end | ||
|
||
def find_pairings(length) | ||
find_matches.select { |number, group| group.length == length } | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require "./hand_classifier" | ||
class HandRanker | ||
HAND_RANKS = { | ||
straight_flush: 9, | ||
four_of_a_kind: 8, | ||
full_house: 7, | ||
flush: 6, | ||
straight: 5, | ||
three_of_a_kind: 4, | ||
two_pair: 3, | ||
one_pair: 2, | ||
high_card: 1 | ||
} | ||
|
||
def initialize(hand) | ||
@hand = hand | ||
@rank = rank_hand | ||
end | ||
|
||
attr_reader :hand, :rank | ||
|
||
def <=>(other_hand) | ||
rank <=> other_hand.rank | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just do this always? It will Do The Right Thing in all cases. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my big problem is if rank is equal and i need to do tiebreaking in the future |
||
end | ||
|
||
private | ||
|
||
def rank_hand | ||
HAND_RANKS[HandClassifier.new(hand).classify] | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything here and below should be
private
, I think.