Permalink
Browse files

Compare two-pair hands

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 9b06f0f commit 6000b59712f54bc76758af38ea2ad04b4a218aff
Showing with 54 additions and 10 deletions.
  1. +24 −6 lib/hand.rb
  2. +30 −4 spec/hand_spec.rb
View
@@ -19,19 +19,37 @@ def <=>(other_hand)
if rank_type == other_hand_rank_type
if rank.fetch(:type) == :full_house
compare_highest(rank, other_hand.rank) ||
compare_filler(rank, other_hand.rank)
compare_filler(rank, other_hand.rank) ||
0
elsif rank.fetch(:type) == :two_pair
compare_pairs(rank, other_hand.rank) ||
compare_cards(rank, other_hand.rank) ||
0
else
compare_value(rank, other_hand.rank) ||
compare_cards(rank, other_hand.rank)
compare_cards(rank, other_hand.rank) ||
0
end
else
POKER_RANKS.index(rank.fetch(:type)) <=> POKER_RANKS.index(other_hand.rank.fetch(:type))
end
end
#TODO: a lot of duplication here!!!
def compare_cards(rank, other_rank)
(rank.fetch(:cards) - other_rank.fetch(:cards)).max <=>
(other_rank.fetch(:cards) - rank.fetch(:cards)).max
cards = rank.fetch(:cards)
other_cards = other_rank.fetch(:cards)
if cards != other_cards
(cards - other_cards).max <=> (other_cards - cards).max
end
end
def compare_pairs(rank, other_rank)
cards = rank.fetch(:pairs)
other_cards = other_rank.fetch(:pairs)
if cards != other_cards
(cards - other_cards).max <=> (other_cards - cards).max
end
end
def compare_value(rank, other_rank)
@@ -117,8 +135,8 @@ def has_two_pairs
if pips_per_occurence[2] && pips_per_occurence[2].size == 2
{
:type => :two_pair,
:pairs => pips_per_occurence[2],
:kicker => pips_per_occurence[1].first
:pairs => pips_per_occurence[2].sort.reverse,
:cards => pips_per_occurence[1]
}
end
end
View
@@ -202,22 +202,48 @@
end
context "two pairs" do
let(:hand_string_array) { ["4h", "4d", "6d", "6h", "9s"] }
let(:hand_string_array) { ["4h", "4d", "7d", "7h", "9s"] }
it "ranks the hand correctly" do
expect(hand.rank.fetch(:type)).to eq :two_pair
end
it "sets the pairs correctly" do
expect(hand.rank.fetch(:pairs)).to eq [4,6]
expect(hand.rank.fetch(:pairs)).to eq [7,4]
end
it "sets the kicker correctly" do
expect(hand.rank.fetch(:kicker)).to eq 9
expect(hand.rank.fetch(:cards).first).to eq 9
end
context "comparison" do
context "with a hand with no identical pairs, best pair lower, same kicker" do
let(:other_hand_string_array) { ["5h", "5d", "6d", "6h", "9s"] }
it "is better" do
expect(hand).to be > other_hand
end
end
context "with a hand with lower high pair, sane lower pair, same kicker" do
let(:other_hand_string_array) { ["4h", "4d", "6d", "6h", "9s"] }
it "is better" do
expect(hand).to be > other_hand
end
end
context "with a hand with high identical pair, one lower pair, same kicker" do
let(:other_hand_string_array) { ["3h", "3d", "7d", "7h", "9s"] }
it "is better" do
expect(hand).to be > other_hand
end
end
context "with a hand with the same two pairs and same pips on kicker" do
let(:other_hand_string_array) { ["4h", "4d", "7d", "7s", "9d"] }
it "is a tie" do
expect(hand).to be == other_hand
end
end
end
end
context "two pairs with low kicker" do
let(:hand_string_array) { ["4h", "4d", "6d", "6h", "2s"] }
it "sets the kicker correctly" do
expect(hand.rank.fetch(:kicker)).to eq 2
expect(hand.rank.fetch(:cards).first).to eq 2
end
end

0 comments on commit 6000b59

Please sign in to comment.