Skip to content
Browse files

hand comparisons WIP

  • Loading branch information...
1 parent 1834f1f commit 13018ea4b397a69344c12af195ec3a0b457b77c0 @datapimp committed Mar 29, 2013
Showing with 114 additions and 2 deletions.
  1. +97 −1 lib/poker-calculator/hand.rb
  2. +17 −1 spec/poker-calculator/hand_spec.rb
View
98 lib/poker-calculator/hand.rb
@@ -1,6 +1,21 @@
+require 'rubygems'
+require 'pry'
+
module PokerCalculator
class Hand
attr_accessor :cards
+
+ Rankings = [
+ :high_card,
+ :one_pair,
+ :two_pair,
+ :three_of_a_kind,
+ :straight,
+ :flush,
+ :full_of_house,
+ :four_of_a_kind,
+ :straight_flush
+ ]
def initialize cards="", options={}
if cards.is_a?(Hash)
@@ -26,6 +41,38 @@ def ranking
return :one_pair if one_pair?
return :high_card
end
+
+ # pairs are beaten by higher pairs
+ # trips are beaten by higher trips, etc
+ def inner_ranking
+ case ranking
+ when :straight, :straight_flush
+ [high_card.rank]
+ when :flush
+ [high_card.rank, sum_of_card_values]
+ when :one_pair, :three_of_a_kind, :quads
+ [high_group, kickers].compact.flatten
+ when :two_pair, :full_house
+ Array([high_group, low_pair, kickers].compact.flatten)
+ else
+ [kickers]
+ end
+ end
+
+ def beaten_by?(other)
+ other_rank_value = Rankings.index(other.ranking)
+ my_rank_value = Rankings.index(self.ranking)
+
+ 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
+ end
+ end
+
+ false
+ end
def to_s
cards.map(&:to_s)
@@ -35,6 +82,55 @@ def high_card
cards.first
end
+ def high_kicker
+ kickers.max
+ end
+
+ def kicker_cards
+ cards.select {|card| kickers.include?(card.rank) }
+ end
+
+ def kickers
+ grouped = groups.collect(&:first)
+ cards.reject {|card| grouped.include?(card.rank) }.collect(&:rank)
+ end
+
+ def pairs
+ count_by_rank.to_a.select {|v| v[1] == 2}
+ end
+
+ def trips
+ count_by_rank.to_a.select {|v| v[1] == 3}
+ end
+
+ def groups
+ count_by_rank.to_a.select {|v| v[1] >= 2}
+ end
+
+ def high_group
+ groups.map(&:first).max
+ end
+
+ def high_pair
+ pairs.map(&:first).max
+ end
+
+ def high_pair_cards
+ cards.select {|card| card.rank == high_pair }
+ end
+
+ def low_pair
+ pairs.map(&:first).sort[1]
+ end
+
+ def low_pair_cards
+ cards.select {|card| card.rank == low_pair }
+ end
+
+ def sum_of_card_values
+ cards.inject(0) {|memo,card| memo += card.rank }
+ end
+
def count_by_suit
@by_suit ||= cards.inject({}) do |memo,card|
memo[card.suit] ||= 0
@@ -110,4 +206,4 @@ def wheel?
end
end
-end
+end
View
18 spec/poker-calculator/hand_spec.rb
@@ -1,6 +1,22 @@
require "spec_helper"
describe PokerCalculator::Hand do
+ describe "Comparing hands" do
+ 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") }
+
+ it "should know the ranking hierarchy" do
+ eights.beaten_by?(trips).should be_true
+ trips.beaten_by?(eights).should be_false
+ 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
+ end
+ end
+
describe "Identifying hands" do
it "should automatically sort the cards by rank in descending order" do
hand = PokerCalculator::Hand.new("2c 8c 5c 3d Ah")
@@ -64,4 +80,4 @@
hand.should be_full_house
end
end
-end
+end

0 comments on commit 13018ea

Please sign in to comment.
Something went wrong with that request. Please try again.