Permalink
Browse files

Compare highest card hands

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 3270ff7 commit 7bad3d18c91411a82f70fe775ee52adb2d259919
Showing with 20 additions and 7 deletions.
  1. +12 −7 lib/hand.rb
  2. +8 −0 spec/hand_spec.rb
View
@@ -17,18 +17,23 @@ 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
- #TODO: this is terrible, wait until other edge cases are out to refactor
- if rank.fetch(:value) == other_hand.rank.fetch(:value)
- (rank.fetch(:cards) - other_hand.rank.fetch(:cards)).max <=>
- (other_hand.rank.fetch(:cards) - rank.fetch(:cards)).max
- else
- rank.fetch(:value) <=> other_hand.rank.fetch(:value)
- end
+ compare_value(rank, other_hand.rank) || compare_cards(rank, other_hand.rank)
else
POKER_RANKS.index(rank.fetch(:type)) <=> POKER_RANKS.index(other_hand.rank.fetch(:type))
end
end
+ def compare_cards(rank, other_rank)
+ (rank.fetch(:cards) - other_rank.fetch(:cards)).max <=>
+ (other_rank.fetch(:cards) - rank.fetch(:cards)).max
+ end
+
+ def compare_value(rank, other_rank)
+ if rank.has_key?(:value) && rank.fetch(:value) != other_rank.fetch(:value)
+ rank.fetch(:value) <=> other_rank.fetch(:value)
+ end
+ end
+
POKER_RANKS = [
:highest,
:pair,
View
@@ -64,6 +64,14 @@
it "returns the other cards correctly" do
expect(hand.rank.fetch(:cards)).to eq [9,7,6,5,4]
end
+ context "comparison" do
+ context "with a hand with lower cards" do
+ let(:other_hand_string_array) { ["4s", "5c", "2d", "3s", "9s"] }
+ it "is better" do
+ expect(hand).to be > other_hand
+ end
+ end
+ end
end
context "three of a kind" do

0 comments on commit 7bad3d1

Please sign in to comment.