Permalink
Browse files

Rename and reorganise hand-name methods for consistency

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 3a7b0be commit f47a817a645ff551d37912f2711a600481d1aa2b
Showing with 64 additions and 64 deletions.
  1. +64 −64 lib/hand.rb
View
@@ -39,66 +39,64 @@ def <=>(other_hand)
def rank
straight_flush ||
- has_four ||
+ four_of_a_kind ||
full_house ||
flush ||
straight ||
- has_three ||
- has_two_pairs ||
- has_two ||
+ three_of_a_kind ||
+ two_pair ||
+ pair ||
highest
end
private
- def pips_per_occurence
- result = results_per_occurence_number(cards.map(&:pips))
- result.each do |nb_occurence, pips|
- result[nb_occurence] = pips.sort.reverse
+ def straight_flush
+ if flush && straight
+ straight.merge(:type => :straight_flush)
end
- result
end
- def suits_per_occurence
- results_per_occurence_number(cards.map(&:suit))
- end
-
- def compare_card_arrays(key, rank, other_rank)
- if rank.has_key?(key)
- cards = rank.fetch(key)
- other_cards = other_rank.fetch(key)
- if cards != other_cards
- (cards - other_cards).max <=> (other_cards - cards).max
- end
+ def four_of_a_kind
+ if pips_per_occurence[4]
+ {
+ :type => :four_of_a_kind,
+ :value => pips_per_occurence[4].first,
+ :cards => pips_per_occurence[1]
+ }
end
end
- def compare_value(key, rank, other_rank)
- if rank.has_key?(key)
- if rank.fetch(key) != other_rank.fetch(key)
- rank.fetch(key) <=> other_rank.fetch(key)
- end
+ def full_house
+ if pips_per_occurence[3] && pips_per_occurence[2]
+ {
+ :type => :full_house,
+ :value => pips_per_occurence[3].first,
+ :full_of => pips_per_occurence[2].first
+ }
end
end
- def highest
- {
- :type => :highest,
- :cards => pips_per_occurence[1]
- }
+ def flush
+ if suits_per_occurence[5]
+ {
+ :type => :flush,
+ :cards => pips_per_occurence[1]
+ }
+ end
end
- def has_four
- if pips_per_occurence[4]
- {
- :type => :four_of_a_kind,
- :value => pips_per_occurence[4].first,
- :cards => pips_per_occurence[1]
- }
+ def straight
+ card_values = cards.map(&:pips)
+ aces_as_ones = aces_as_ones(card_values)
+ if aces_as_ones != card_values && consecutive_cards?(aces_as_ones)
+ { :type => :straight, :value => 5 }
+ elsif consecutive_cards?(card_values)
+ { :type => :straight, :value => high_card }
end
end
- def has_three
+ def three_of_a_kind
if pips_per_occurence[3]
{
:type => :three_of_a_kind,
@@ -108,7 +106,7 @@ def has_three
end
end
- def has_two_pairs
+ def two_pair
if pips_per_occurence[2] && pips_per_occurence[2].size == 2
{
:type => :two_pair,
@@ -118,7 +116,7 @@ def has_two_pairs
end
end
- def has_two
+ def pair
if pips_per_occurence[2]
{
:type => :pair,
@@ -128,38 +126,40 @@ def has_two
end
end
- def full_house
- if pips_per_occurence[3] && pips_per_occurence[2]
- {
- :type => :full_house,
- :value => pips_per_occurence[3].first,
- :full_of => pips_per_occurence[2].first
- }
- end
+ def highest
+ {
+ :type => :highest,
+ :cards => pips_per_occurence[1]
+ }
end
- def flush
- if suits_per_occurence[5]
- {
- :type => :flush,
- :cards => pips_per_occurence[1]
- }
+ def pips_per_occurence
+ result = results_per_occurence_number(cards.map(&:pips))
+ result.each do |nb_occurence, pips|
+ result[nb_occurence] = pips.sort.reverse
end
+ result
end
- def straight
- card_values = cards.map(&:pips)
- aces_as_ones = aces_as_ones(card_values)
- if aces_as_ones != card_values && consecutive_cards?(aces_as_ones)
- { :type => :straight, :value => 5 }
- elsif consecutive_cards?(card_values)
- { :type => :straight, :value => high_card }
+ def suits_per_occurence
+ results_per_occurence_number(cards.map(&:suit))
+ end
+
+ def compare_card_arrays(key, rank, other_rank)
+ if rank.has_key?(key)
+ cards = rank.fetch(key)
+ other_cards = other_rank.fetch(key)
+ if cards != other_cards
+ (cards - other_cards).max <=> (other_cards - cards).max
+ end
end
end
- def straight_flush
- if flush && straight
- straight.merge(:type => :straight_flush)
+ def compare_value(key, rank, other_rank)
+ if rank.has_key?(key)
+ if rank.fetch(key) != other_rank.fetch(key)
+ rank.fetch(key) <=> other_rank.fetch(key)
+ end
end
end

0 comments on commit f47a817

Please sign in to comment.