Skip to content

Commit

Permalink
Implement two pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
camillebaldock committed Sep 3, 2014
1 parent f991324 commit b9bd531
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -8,3 +8,8 @@ A “hand” in poker consists of five playing cards drawn from a standard deck.


TBA TBA


## After the video

1) Finish implementing all the poker hands

in a "shameless green" style
7 changes: 7 additions & 0 deletions lib/hand.rb
Expand Up @@ -26,6 +26,8 @@ def rank
{ :type => :flush } { :type => :flush }
elsif has_three elsif has_three
{ :type => :three_of_a_kind } { :type => :three_of_a_kind }
elsif has_two_pairs
{ :type => :two_pair }
elsif has_two elsif has_two
{ :type => :pair } { :type => :pair }
else else
Expand Down Expand Up @@ -59,6 +61,11 @@ def has_three
pip_count.values.include?(3) pip_count.values.include?(3)
end end


def has_two_pairs
grouped_pip_counts = pip_count.values.group_by { |i| i }
grouped_pip_counts[2] && grouped_pip_counts[2].size == 2
end

def has_two def has_two
pip_count.values.include?(2) pip_count.values.include?(2)
end end
Expand Down
7 changes: 7 additions & 0 deletions spec/hand_spec.rb
Expand Up @@ -11,6 +11,9 @@
let(:highest_hand) { let(:highest_hand) {
HandParser.new.parse(["4h", "5d", "6d", "7d", "9d"]) HandParser.new.parse(["4h", "5d", "6d", "7d", "9d"])
} }
let(:two_pair_hand) {
HandParser.new.parse(["4h", "4d", "6d", "6h", "9s"])
}
let(:three_of_a_kind_hand) { let(:three_of_a_kind_hand) {
HandParser.new.parse(["5h", "5d", "5s", "7d", "8d"]) HandParser.new.parse(["5h", "5d", "5s", "7d", "8d"])
} }
Expand Down Expand Up @@ -87,5 +90,9 @@
it "works with a straight with high ace" do it "works with a straight with high ace" do
expect(straight_hand_high_ace.rank).to eq ({:type => :straight}) expect(straight_hand_high_ace.rank).to eq ({:type => :straight})
end end

it "works with a two pair hand" do
expect(two_pair_hand.rank).to eq ({:type => :two_pair})
end
end end
end end

0 comments on commit b9bd531

Please sign in to comment.