Permalink
Browse files

Added tie for royals

  • Loading branch information...
1 parent 19898d7 commit f964e0512658a9f4368209c1deedf12c579e8acb @dblack committed May 16, 2012
Showing with 29 additions and 16 deletions.
  1. +21 −16 lib/hand.rb
  2. +8 −0 playing_cards_test.rb
View
37 lib/hand.rb
@@ -23,7 +23,7 @@ class Hand
TIE_BREAKERS = {
"high card" => high_card_proc,
"pair" => proc do |hand1, hand2|
- comp = hand1.multiple_cards(2).first <=> hand2.multiple_cards(2).first
+ comp = hand1.compare_multiple_card(hand2, 2)
comp.zero? ? hand1.compare_kickers(hand2) : comp
end,
"two pair" => proc do |hand1, hand2|
@@ -35,29 +35,18 @@ class Hand
"straight" => high_card_proc,
"flush" => high_card_proc,
"three of a kind" => proc do |hand1, hand2|
- hand1.multiple_cards(3).first <=> hand2.multiple_cards(3).first
+ hand1.compare_multiple_card(hand2, 3)
end,
"full house" => proc do |hand1, hand2|
- hand1.multiple_cards(3).first <=> hand2.multiple_cards(3).first
+ hand1.compare_multiple_card(hand2, 3)
end,
"four of a kind" => proc do |hand1, hand2|
- hand1.multiple_cards(4).first <=> hand2.multiple_cards(4).first
+ hand1.compare_multiple_card(hand2, 4)
end,
"straight flush" => high_card_proc,
+ "royal straight flush" => proc { 0 }
}
- def kickers
- multiple_cards(1).sort
- end
-
- def compare_kickers(other)
- kickers.each.with_index do |card, i|
- comp = card <=> other.kickers[i]
- return comp unless comp.zero?
- end
- return 0
- end
-
def initialize(cards)
@cards = cards
end
@@ -71,6 +60,22 @@ def multiple_cards(n)
end
end
+ def compare_multiple_card(other, n)
+ self.multiple_cards(n).first <=> other.multiple_cards(n).first
+ end
+
+ def kickers
+ multiple_cards(1).sort
+ end
+
+ def compare_kickers(other)
+ kickers.each.with_index do |card, i|
+ comp = card <=> other.kickers[i]
+ return comp unless comp.zero?
+ end
+ return 0
+ end
+
def hand_name
HANDS_IN_ORDER.each do |hand|
send("is_#{hand.tr(' ', '_')}?") and return hand
View
8 playing_cards_test.rb
@@ -273,6 +273,14 @@ def test_ties_straight_flush
assert(hand1.ties?(hand1))
end
+ def test_ties_royal_straight_flush
+ hand1 = hand_from("10", "clubs", "J", "clubs", "Q", "clubs",
+ "K", "clubs", "A", "clubs")
+ hand2 = hand_from("10", "hearts", "J", "hearts", "Q", "hearts",
+ "K", "hearts", "A", "hearts")
+ assert(hand1.ties?(hand2))
+ end
+
def hand_from(*specs)
PlayingCards::Hand.new(specs.each_slice(2).map {|r,s| PlayingCards::Card.new(r,s)})
end

0 comments on commit f964e05

Please sign in to comment.