Permalink
Browse files

Compare full house hands

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent eedd1a1 commit 9b06f0fb2e23a3597f71716f330a5598cdb9af5b
Showing with 39 additions and 1 deletion.
  1. +19 −1 lib/hand.rb
  2. +20 −0 spec/hand_spec.rb
View
@@ -17,7 +17,13 @@ def <=>(other_hand)
rank_type = POKER_RANKS.index(rank.fetch(:type))
other_hand_rank_type = POKER_RANKS.index(other_hand.rank.fetch(:type))
if rank_type == other_hand_rank_type
- compare_value(rank, other_hand.rank) || compare_cards(rank, other_hand.rank)
+ if rank.fetch(:type) == :full_house
+ compare_highest(rank, other_hand.rank) ||
+ compare_filler(rank, other_hand.rank)
+ else
+ compare_value(rank, other_hand.rank) ||
+ compare_cards(rank, other_hand.rank)
+ end
else
POKER_RANKS.index(rank.fetch(:type)) <=> POKER_RANKS.index(other_hand.rank.fetch(:type))
end
@@ -34,6 +40,18 @@ def compare_value(rank, other_rank)
end
end
+ def compare_filler(rank, other_rank)
+ if rank.has_key?(:full_of)
+ rank.fetch(:full_of) <=> other_rank.fetch(:full_of)
+ end
+ end
+
+ def compare_highest(rank, other_rank)
+ if rank.has_key?(:highest) && rank.fetch(:highest) != other_rank.fetch(:highest)
+ rank.fetch(:highest) <=> other_rank.fetch(:highest)
+ end
+ end
+
POKER_RANKS = [
:highest,
:pair,
View
@@ -139,6 +139,26 @@
it "returns the full_of value correctly" do
expect(hand.rank.fetch(:full_of)).to eq 6
end
+ context "comparison" do
+ context "with a hand with a smaller highest" do
+ let(:other_hand_string_array) { ["4h", "4d", "4s", "6c", "6h"] }
+ it "is better" do
+ expect(hand).to be > other_hand
+ end
+ end
+ context "with a hand with the same highest but lower filler" do
+ let(:other_hand_string_array) { ["5h", "5d", "5s", "4c", "4h"] }
+ it "is better" do
+ expect(hand).to be > other_hand
+ end
+ end
+ context "with a hand with the same highest and same filler" do
+ let(:other_hand_string_array) { ["5h", "5d", "5s", "6s", "6d"] }
+ it "is a tie" do
+ expect(hand).to be == other_hand
+ end
+ end
+ end
end
context "flush hand" do

0 comments on commit 9b06f0f

Please sign in to comment.