Permalink
Browse files

Implement highest for straight hands

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 2cdd02c commit a38336afc42ebaf5f62b6701d6a9de4e70051a1f
Showing with 39 additions and 14 deletions.
  1. +9 −5 lib/hand.rb
  2. +30 −9 spec/hand_spec.rb
View
@@ -39,7 +39,7 @@ def rank
elsif flush
{ :type => :flush }
elsif straight
{ :type => :straight }
straight
elsif has_three
{ :type => :three_of_a_kind }
elsif has_two_pairs
@@ -89,10 +89,10 @@ def flush
def straight
card_values = cards.map(&:pips)
aces_as_ones = aces_as_ones(card_values)
if aces_as_ones != card_values
consecutive_cards?(aces_as_ones) || consecutive_cards?(card_values)
else
consecutive_cards?(card_values)
if aces_as_ones != card_values && consecutive_cards?(aces_as_ones)
{ :type => :straight, :highest => 5 }
elsif consecutive_cards?(card_values)
{ :type => :straight, :highest => high_card }
end
end
@@ -116,4 +116,8 @@ def aces_as_ones(card_values)
result
end
def high_card
cards.map(&:pips).max
end
end
View
@@ -82,22 +82,43 @@
expect(flush_hand.rank).to eq ({:type => :flush})
end
it "ranks a straight hand correctly" do
straight_hand = described_class.new(straight_hand_string_array, hand_parser)
context "straight hand" do
it "ranks the hand correctly" do
straight_hand = described_class.new(straight_hand_string_array, hand_parser)
expect(straight_hand.rank).to eq ({:type => :straight})
expect(straight_hand.rank.fetch(:type)).to eq :straight
end
it "sets the highest hand correctly" do
straight_hand = described_class.new(straight_hand_string_array, hand_parser)
expect(straight_hand.rank.fetch(:highest)).to eq 9
end
end
it "ranks a straight with low ace hand correctly" do
straight_hand_low_ace = described_class.new(straight_hand_low_ace_string_array, hand_parser)
context "straight with low ace hand" do
it "ranks the hand correctly" do
straight_hand_low_ace = described_class.new(straight_hand_low_ace_string_array, hand_parser)
expect(straight_hand_low_ace.rank.fetch(:type)).to eq :straight
end
it "sets the highest hand correctly" do
straight_hand_low_ace = described_class.new(straight_hand_low_ace_string_array, hand_parser)
expect(straight_hand_low_ace.rank).to eq ({:type => :straight})
expect(straight_hand_low_ace.rank.fetch(:highest)).to eq 5
end
end
it "ranks a straight with high ace hand correctly" do
straight_hand_high_ace = described_class.new(straight_hand_high_ace_string_array, hand_parser)
context "straight with high ace hand" do
it "ranks the hand correctly" do
straight_hand_high_ace = described_class.new(straight_hand_high_ace_string_array, hand_parser)
expect(straight_hand_high_ace.rank.fetch(:type)).to eq :straight
end
it "sets the highest hand correctly" do
straight_hand_high_ace = described_class.new(straight_hand_high_ace_string_array, hand_parser)
expect(straight_hand_high_ace.rank).to eq ({:type => :straight})
expect(straight_hand_high_ace.rank.fetch(:highest)).to eq 14
end
end
it "ranks a two pair hand correctly" do

0 comments on commit a38336a

Please sign in to comment.