Permalink
Browse files

Implement pairs and kicker for two pairs hand

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent b702c97 commit e4e2b09940076997da93753ba95cc46af815e2e8
Showing with 47 additions and 6 deletions.
  1. +13 −0 lib/array_helper.rb
  2. +10 −3 lib/hand.rb
  3. +10 −3 spec/array_helper_spec.rb
  4. +14 −0 spec/hand_spec.rb
View
@@ -8,6 +8,19 @@ def value_occurence_count(array)
result
end
def results_per_occurence_number(array)
grouped_values = array.group_by { |i| i }
result = {}
grouped_values.each do |key, value|
if result[value.count]
result[value.count] << key
else
result[value.count] = [key]
end
end
result
end
def array_consecutive_integers?(array)
array.sort!
difference_always_1 = true
View
@@ -29,6 +29,7 @@ def <=>(other_hand)
:straight_flush,
]
#TODO: refactor this once all test methods returns hashes
def rank
if straight_flush
{ :type => :straight_flush }
@@ -43,7 +44,7 @@ def rank
elsif has_three
{ :type => :three_of_a_kind }
elsif has_two_pairs
{ :type => :two_pair }
has_two_pairs
elsif has_two
{ :type => :pair }
else
@@ -70,8 +71,14 @@ def has_three
end
def has_two_pairs
grouped_pip_counts = pips_occurence_count.group_by { |i| i }
grouped_pip_counts[2] && grouped_pip_counts[2].size == 2
results_per_occurence = results_per_occurence_number(cards.map(&:pips))
if results_per_occurence[2] && results_per_occurence[2].size == 2
{
:type => :two_pair,
:pairs => results_per_occurence[2],
:kicker => results_per_occurence[1].first
}
end
end
def has_two
View
@@ -11,13 +11,21 @@ class ExampleClass
@example_class.extend(ArrayHelper)
end
let(:sample_array) { [:a, 1, "b", "B", 1] }
let(:sample_array) { [:a, 1, "b", "B", 1, 3, 3] }
describe "#value_occurence_count" do
it "gives the number of occurences of each value in the array" do
result = @example_class.value_occurence_count(sample_array)
expect(result).to eq({ :a => 1, 1 => 2, "b" => 1, "B" => 1 })
expect(result).to eq({ :a => 1, 1 => 2, "b" => 1, "B" => 1, 3 => 2 })
end
end
describe "#results_per_occurence_number" do
it "gives the number of occurences of each value in the array" do
result = @example_class.results_per_occurence_number(sample_array)
expect(result).to eq({ 2 => [1, 3], 1 => [:a, "b", "B"] })
end
end
@@ -62,6 +70,5 @@ class ExampleClass
expect(result).to be true
end
end
end
View
@@ -11,6 +11,7 @@
let(:pair_hand_string_array) { ["5h", "5d", "6d", "7d", "8d"] }
let(:highest_hand_string_array) { ["4h", "5d", "6d", "7d", "9d"] }
let(:two_pair_hand_string_array) { ["4h", "4d", "6d", "6h", "9s"] }
let(:two_pair_low_kicker_hand_string_array) { ["4h", "4d", "6d", "6h", "2s"] }
let(:three_of_a_kind_hand_string_array) { ["5h", "5d", "5s", "7d", "8d"] }
let(:four_of_a_kind_hand_string_array) { ["5h", "5d", "5s", "5c", "8d"] }
let(:full_house_hand_string_array) { ["5h", "5d", "5s", "6c", "6h"] }
@@ -120,6 +121,19 @@
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]
end
it "sets the kicker correctly" do
expect(hand.rank.fetch(:kicker)).to eq 9
end
end
context "two pairs with low kicker" do
let(:hand_string_array) { two_pair_low_kicker_hand_string_array }
it "sets the kicker correctly" do
expect(hand.rank.fetch(:kicker)).to eq 2
end
end
context "straight flush" do

0 comments on commit e4e2b09

Please sign in to comment.