Permalink
Browse files

Implement hand as Comparable

  • Loading branch information...
camillebaldock committed Sep 7, 2014
1 parent 1da5e47 commit 0a58fe90f1adeeee83bc21bbab3e38c7ad035798
Showing with 14 additions and 4 deletions.
  1. +12 −2 lib/hand.rb
  2. +2 −2 spec/hand_spec.rb
View
@@ -2,6 +2,7 @@
class Hand
include ArrayHelper
include Comparable
attr_reader :cards
@@ -12,13 +13,22 @@ def initialize(card_input, hand_parser)
end
end
def better_than?(other_hand)
POKER_RANKS.index(rank.fetch(:type)) > POKER_RANKS.index(other_hand.rank.fetch(:type))
#TODO: this only implements comparison for cards of different types
#future things to handle include differentiating hands with the same type
def <=>(other_hand)
POKER_RANKS.index(rank.fetch(:type)) <=> POKER_RANKS.index(other_hand.rank.fetch(:type))
end
POKER_RANKS = [
:highest,
:pair,
:two_pair,
:three_of_a_kind,
:straight,
:flush,
:full_house,
:four_of_a_kind,
:straight_flush,
]
def rank
View
@@ -19,12 +19,12 @@
let(:straight_hand_high_ace_string_array) { ["10h", "jh", "qh", "kh", "ad"] }
let(:straight_flush_hand_string_array) { ["5h", "6h", "7h", "8h", "9h"] }
describe "#better_than" do
describe "hand comparisons" do
it "tells me if the hand is better" do
four_of_a_kind_hand = described_class.new(four_of_a_kind_hand_string_array, hand_parser)
full_house_hand = described_class.new(full_house_hand_string_array, hand_parser)
expect(four_of_a_kind_hand.better_than?(full_house_hand)).to be true
expect(four_of_a_kind_hand).to be > full_house_hand
end
end

0 comments on commit 0a58fe9

Please sign in to comment.