Permalink
Browse files

Adding more specs to test hand rankings

  • Loading branch information...
1 parent 13018ea commit b2989f1385bd1f971d8cbd16364498b07cf9bc66 @datapimp committed Apr 1, 2013
@@ -10,7 +10,7 @@ class Card
"s" => "Spades"
}
- Values = %w{2 3 4 5 6 7 8 9 10 J Q K A}
+ Values = %w{2 3 4 5 6 7 8 9 T J Q K A}
InvalidCard = Class.new(Exception)
@@ -12,7 +12,7 @@ class Hand
:three_of_a_kind,
:straight,
:flush,
- :full_of_house,
+ :full_house,
:four_of_a_kind,
:straight_flush
]
@@ -34,6 +34,7 @@ def initialize cards="", options={}
def ranking
return :straight_flush if straight_flush?
return :four_of_a_kind if four_of_a_kind?
+ return :full_house if full_house?
return :flush if flush?
return :straight if straight?
return :three_of_a_kind if three_of_a_kind?
@@ -47,16 +48,22 @@ def ranking
def inner_ranking
case ranking
when :straight, :straight_flush
- [high_card.rank]
+ Array(straight_high_card.rank)
+ when :four_of_a_kind
+ [high_group,kickers]
+ when :full_house
+ high = groups.detect {|v| v[1] == 3 }
+ low = groups.detect {|v| v[1] == 2 }
+ [high[0],low[0]]
when :flush
- [high_card.rank, sum_of_card_values]
- when :one_pair, :three_of_a_kind, :quads
+ [high_card.rank, kickers].compact.flatten
+ when :one_pair, :three_of_a_kind
[high_group, kickers].compact.flatten
- when :two_pair, :full_house
+ when :two_pair
Array([high_group, low_pair, kickers].compact.flatten)
else
[kickers]
- end
+ end.flatten
end
def beaten_by?(other)
@@ -66,8 +73,8 @@ def beaten_by?(other)
return true if other_rank_value > my_rank_value
if other_rank_value == my_rank_value
- inner_ranking.each_with_index do |ranking,index|
- return true if other.inner_ranking[index] > ranking
+ inner_ranking.each_with_index do |rank,index|
+ return true if other.inner_ranking[index] > rank
end
end
@@ -78,6 +85,10 @@ def to_s
cards.map(&:to_s)
end
+ def straight_high_card
+ wheel? ? cards[1] : cards[0]
+ end
+
def high_card
cards.first
end
@@ -104,7 +115,7 @@ def trips
end
def groups
- count_by_rank.to_a.select {|v| v[1] >= 2}
+ count_by_rank.to_a.select {|v| v[1] >= 2}.sort {|a,b| a[1]<=>b[1] }.reverse
end
def high_group
@@ -120,7 +131,7 @@ def high_pair_cards
end
def low_pair
- pairs.map(&:first).sort[1]
+ pairs.map(&:first).sort.reverse[1]
end
def low_pair_cards
@@ -152,20 +163,26 @@ def ranks
end
def one_pair?
- count_by_rank.values.include?(2)
+ values = count_by_rank.values
+ counts = values.select {|v| v == 2}
+ !values.include?(3) && values.include?(2) && counts.length == 1
end
def two_pair?
- counts = count_by_rank.values.select {|v| v == 2}
- counts.length >= 2
+ values = count_by_rank.values
+ counts = values.select {|v| v == 2}
+ !values.include?(3) && counts.length == 2
end
def three_of_a_kind?
- count_by_rank.values.include?(3)
+ values = count_by_rank.values
+ counts = values.select {|v| v == 2}
+ values.include?(3) && !values.include?(2)
end
def straight?
return true if wheel?
+ return false unless ranks.uniq.length == 5
diffs = []
@@ -10,10 +10,18 @@
PokerCalculator::Card.new("Kh")
}
+ let(:ten_of_diamonds) {
+ PokerCalculator::Card.new("Td")
+ }
+
let(:king_of_diamonds) {
PokerCalculator::Card.new("Kd")
}
+ it "should recognize a ten of diamonds" do
+ ten_of_diamonds.should < king_of_hearts
+ end
+
it "should recognize a card of a higher value" do
ace_of_hearts.should > king_of_hearts
end
@@ -2,18 +2,98 @@
describe PokerCalculator::Hand do
describe "Comparing hands" do
+ let(:ace_high) { PokerCalculator::Hand.new("8c 9d 7d 3h As")}
+ let(:king_high) { PokerCalculator::Hand.new("8c 9d 7d 3h Ks")}
let(:eights) { PokerCalculator::Hand.new("8c 8d Ac Kd 2s") }
let(:nines) { PokerCalculator::Hand.new("9c 9d Ac Kd 2s") }
- let(:trips) { PokerCalculator::Hand.new("9c 9d 9s Kd 2s") }
+ let(:trip_nines) { PokerCalculator::Hand.new("9c 9d 9s Kd 2s") }
+ let(:trip_aces) { PokerCalculator::Hand.new("Ac Ad As Kd 2s") }
+ let(:aces_and_deuces) { PokerCalculator::Hand.new("Ac Ad 2c 2d Kc") }
+ let(:aces_and_queens) { PokerCalculator::Hand.new("Ac Ad Qc Qd Kc") }
+ let(:kings_and_queens) { PokerCalculator::Hand.new("Kc Kd Qc Qd Ac") }
+ let(:broadway) { PokerCalculator::Hand.new("Tc Jc Qd Ks Ac") }
+ let(:wheel) { PokerCalculator::Hand.new("Ac 2d 3c 4s 5h") }
+ let(:king_high_flush) { PokerCalculator::Hand.new("Kh Jh 9h 3h 6h") }
+ let(:ace_high_flush) { PokerCalculator::Hand.new("Ah Jh 9h 3h 6h") }
+ let(:kings_full) { PokerCalculator::Hand.new("Kh Kd Kc As Ad")}
+ let(:kings_full_of_queens) { PokerCalculator::Hand.new("Kh Kd Kc Qs Qd")}
+ let(:queens_full) { PokerCalculator::Hand.new("Qh Qd Qc As Ad")}
+ let(:quad_deuces) { PokerCalculator::Hand.new("2c 2d 2s 2h Ah")}
+ let(:quad_aces) { PokerCalculator::Hand.new("Ac Ad As Ah 2h")}
+ let(:straight_flush) { PokerCalculator::Hand.new("4h 5h 6h 7h 8h") }
+ let(:royal_flush) { PokerCalculator::Hand.new("Tc Jc Qc Kc Ac") }
- it "should know the ranking hierarchy" do
- eights.beaten_by?(trips).should be_true
- trips.beaten_by?(eights).should be_false
+ it "should know that a high card is beaten by a higher card" do
+ ace_high.should_not be_beaten_by(king_high)
+ king_high.should be_beaten_by(ace_high)
end
- it "should know if a hand is beaten by another hand of the same rank" do
- eights.beaten_by?(nines).should be_true
- nines.beaten_by?(eights).should be_false
+ it "should know that a high card is beaten by a pair" do
+ ace_high.should be_beaten_by(eights)
+ end
+
+ it "should know that a pair is beaten by a higher pair" do
+ eights.should be_beaten_by(nines)
+ end
+
+ it "should know that kings and queens is beaten by aces and deuces" do
+ kings_and_queens.should be_beaten_by(aces_and_deuces)
+ end
+
+ it "should know that aces and deuces is beaten by aces and queens" do
+ aces_and_deuces.should be_beaten_by(aces_and_queens)
+ end
+
+ it "should know that two pair is beaten by trips" do
+ aces_and_deuces.should be_beaten_by(trip_nines)
+ end
+
+ it "should know that trips is beaten by higher trips" do
+ trip_nines.should be_beaten_by(trip_aces)
+ trip_aces.should_not be_beaten_by(trip_nines)
+ end
+
+ it "should know that trips is beaten by a straight" do
+ trip_nines.should be_beaten_by(broadway)
+ broadway.should_not be_beaten_by(trip_nines)
+ end
+
+ it "should know that a straight is beaten by a higher straight" do
+ wheel.should be_beaten_by(broadway)
+ broadway.should_not be_beaten_by(wheel)
+ broadway.should_not be_beaten_by(broadway)
+ end
+
+ it "should know that a king high flush is beaten by an ace high flush" do
+ king_high_flush.should be_beaten_by(ace_high_flush)
+ end
+
+ it "should know that a flush is beaten by a full house" do
+ ace_high_flush.should be_beaten_by(kings_full)
+ end
+
+ it "should know that a full house is beaten by a bigger full house" do
+ queens_full.should be_beaten_by(kings_full)
+ kings_full.should_not be_beaten_by(queens_full)
+ kings_full_of_queens.should be_beaten_by(kings_full)
+ end
+
+ it "should know that full houses are beaten by quads" do
+ quad_deuces.should_not be_beaten_by(kings_full)
+ kings_full.should be_beaten_by(quad_deuces)
+ end
+
+ it "should know a bad beat when it sees one!" do
+ quad_deuces.should be_beaten_by(quad_aces)
+ end
+
+ it "should know quads is beaten by a straight flush" do
+ straight_flush.should_not be_beaten_by(quad_aces)
+ quad_aces.should be_beaten_by(straight_flush)
+ end
+
+ it "should know a royal flush beats all" do
+ straight_flush.should be_beaten_by(royal_flush)
end
end
@@ -77,6 +157,9 @@
it "should recognize a full house" do
hand = PokerCalculator::Hand.new("8s 8c 8d Qh Qd")
+ hand.should_not be_one_pair
+ hand.should_not be_two_pair
+ hand.should_not be_trips
hand.should be_full_house
end
end

0 comments on commit b2989f1

Please sign in to comment.