Permalink
Browse files

Compare pairs

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 5f0e5c4 commit 3270ff725e1b336edeb5b44cfd69208e08d11671
Showing with 52 additions and 26 deletions.
  1. +30 −26 lib/hand.rb
  2. +22 −0 spec/hand_spec.rb
View
@@ -14,7 +14,19 @@ def initialize(card_input, hand_parser)
#TODO: this only implements comparison for cards of different types
#future things to handle include differentiating hands with the same type
def <=>(other_hand)
- POKER_RANKS.index(rank.fetch(:type)) <=> POKER_RANKS.index(other_hand.rank.fetch(:type))
+ 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
+ else
+ POKER_RANKS.index(rank.fetch(:type)) <=> POKER_RANKS.index(other_hand.rank.fetch(:type))
+ end
end
POKER_RANKS = [
@@ -29,31 +41,16 @@ def <=>(other_hand)
:straight_flush,
]
- #TODO: refactor this once all test methods returns hashes
def rank
- if straight_flush
- straight_flush
- elsif has_four
- has_four
- elsif full_house
- full_house
- elsif flush
- flush
- elsif straight
- straight
- elsif has_three
- has_three
- elsif has_two_pairs
- has_two_pairs
- elsif has_two
- has_two
- else
- #TODO: possible duplication happening on pips_per_occurence[1].sort.reverse
- {
- :type => :highest,
- :cards => pips_per_occurence[1].sort.reverse
- }
- end
+ straight_flush ||
+ has_four ||
+ full_house ||
+ flush ||
+ straight ||
+ has_three ||
+ has_two_pairs ||
+ has_two ||
+ highest
end
private
@@ -66,6 +63,13 @@ def suits_per_occurence
results_per_occurence_number(cards.map(&:suit))
end
+ def highest
+ {
+ :type => :highest,
+ :cards => pips_per_occurence[1].sort.reverse
+ }
+ end
+
def has_four
if pips_per_occurence[4]
{
@@ -120,7 +124,7 @@ def flush
if suits_per_occurence[5]
{
:type => :flush,
- :cards => cards.map(&:pips).sort.reverse
+ :cards => pips_per_occurence[1].sort.reverse
}
end
end
View
@@ -10,7 +10,9 @@
let(:hand_parser) { HandArrayParser.new(card_parser) }
let(:pair_hand_string_array) { ["5h", "5d", "6d", "7d", "8d"] }
let(:hand_string_array) { pair_hand_string_array }
+ let(:other_hand_string_array) { pair_hand_string_array }
let(:hand) { described_class.new(hand_string_array, hand_parser) }
+ let(:other_hand) { described_class.new(other_hand_string_array, hand_parser) }
describe "hand comparisons" do
it "tells me if the hand is better" do
@@ -32,6 +34,26 @@
it "returns the other cards correctly" do
expect(hand.rank.fetch(:cards)).to eq [8,7,6]
end
+ context "comparison" do
+ context "with a hand with a lower pair" do
+ let(:other_hand_string_array) { ["4h", "4d", "6s", "7s", "8s"] }
+ it "is better" do
+ expect(hand).to be > other_hand
+ end
+ end
+ context "with a hand with the same pair but lower cards" do
+ let(:other_hand_string_array) { ["5s", "5c", "2d", "7s", "8s"] }
+ it "is better" do
+ expect(hand).to be > other_hand
+ end
+ end
+ context "with a hand with the same pair and same pips for other cards" do
+ let(:other_hand_string_array) { ["5s", "5c", "6s", "7s", "8s"] }
+ it "is a tie" do
+ expect(hand).to be == other_hand
+ end
+ end
+ end
end
context "high card" do

0 comments on commit 3270ff7

Please sign in to comment.