Permalink
Browse files

Refactor how pips and suit occurences are counted

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 0f76ee1 commit dd9b8c2be75df947ff4cfe592f5e4280b2218fa1
Showing with 7 additions and 39 deletions.
  1. +0 −8 lib/array_helper.rb
  2. +7 −11 lib/hand.rb
  3. +0 −8 spec/array_helper_spec.rb
  4. +0 −12 spec/hand_spec.rb
View
@@ -1,12 +1,4 @@
module ArrayHelper
- def value_occurence_count(array)
- grouped_values = array.group_by { |i| i }
- result = {}
- grouped_values.each do |key, value|
- result[key] = value.count
- end
- result
- end
def results_per_occurence_number(array)
grouped_values = array.group_by { |i| i }
View
@@ -52,20 +52,16 @@ def rank
end
end
- def pips_occurence_count
- value_occurence_count(cards.map(&:pips)).values
- end
-
- def suit_occurence_count
- value_occurence_count(cards.map(&:suit)).values
- end
-
private
def pips_per_occurence
results_per_occurence_number(cards.map(&:pips))
end
+ def suits_per_occurence
+ results_per_occurence_number(cards.map(&:suit))
+ end
+
def has_four
if pips_per_occurence[4]
{
@@ -77,7 +73,7 @@ def has_four
end
def has_three
- pips_occurence_count.include?(3)
+ pips_per_occurence[3]
end
def has_two_pairs
@@ -91,15 +87,15 @@ def has_two_pairs
end
def has_two
- pips_occurence_count.include?(2)
+ pips_per_occurence[2]
end
def full_house
has_three && has_two
end
def flush
- suit_occurence_count.include?(5)
+ suits_per_occurence[5]
end
def straight
@@ -13,14 +13,6 @@ class ExampleClass
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, 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)
View
@@ -32,18 +32,6 @@
end
end
- describe "#pips_occurrence_count" do
- it "returns the correct pips occurrence count" do
- expect(hand.pips_occurence_count).to eq ({ 5 => 2, 6 => 1, 7 => 1, 8 => 1 }.values)
- end
- end
-
- describe "#suit_occurrence_count" do
- it "returns the correct suit occurrence count" do
- expect(hand.suit_occurence_count).to eq ({ :heart => 1, :diamonds => 4 }.values)
- end
- end
-
describe "#rank" do
context "pair hand" do
it "ranks the hand correctly" do

0 comments on commit dd9b8c2

Please sign in to comment.