From 781bb1aa5ac6691a2c5a9aa8f2afd52d3a490730 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 25 Aug 2008 10:04:01 -0700 Subject: [PATCH] initial commit --- card.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rank.rb | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 card.rb create mode 100644 rank.rb diff --git a/card.rb b/card.rb new file mode 100644 index 0000000..dbe0c3e --- /dev/null +++ b/card.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +class Card + + attr_reader :rank, :suit + + def initialize(str) + r = str[0,1].downcase + s = str[1,1].downcase + @suit = case s + when 's' then :spades + when 'h' then :hearts + when 'd' then :diamonds + when 'c' then :clubs + end + @rank = case r + when '2'..'9' then r.to_i + when 't' then 10 + when 'j' then 11 + when 'q' then 12 + when 'k' then 13 + when 'a' then 14 + end + end + + def to_s + rank_s + " of " + suit_s + end + + def rank_s + case @rank + when 2 then "two" + when 3 then "three" + when 4 then "four" + when 5 then "five" + when 6 then "six" + when 7 then "seven" + when 8 then "eight" + when 9 then "nine" + when 10 then "ten" + when 11 then "jack" + when 12 then "queen" + when 13 then "king" + when 14 then "ace" + end + end + + def suit_s + @suit.to_s + end + + def ==(other) + @rank == other.rank && @suit == other.suit + end + + def <=>(other) + if @rank != other.rank + @rank <=> other.rank + else + # order is Spades, Hearts, Diamonds, Clubs (conveniently reverse alphabetical) + other.suit_s <=> suit_s + end + end + + def eql?(other) + self == other + end + + def hash + to_s.hash + end +end + diff --git a/rank.rb b/rank.rb new file mode 100644 index 0000000..2adb684 --- /dev/null +++ b/rank.rb @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +class Array + def combinations n + result = Array.new + if n > 0 + (0 .. self.length - n).each do |i| + combs = self[i + 1 .. -1].combinations(n - 1) + combs = [[]] if combs.empty? + combs.collect {|comb| [self[i]] + comb}.each {|x| result << x} + end + end + return result + end +end + +require 'card' + +def score5(hand,verbose=false) + # reject illegal hands (hands with duplicate cards) + return nil if hand.sort.uniq.size < 5 + return 0 +end + +def score10(hand,verbose=false) + # reject illegal hands (hands with duplicate cards) + return nil if hand.sort.uniq.size < 10 + + # count number of suits + suits = Hash.new(0) + ranks = Hash.new(0) + hand.each { |c| ranks[c[0]] += 1 } + hand.each { |c| suits[c[1]] += 1 } + + puts ranks.inspect + puts suits.inspect + + return 0 +end + + +def score(hand_string) + # split into Cards + cards = hand_string.scan(/../).collect { |s| Card.new(s) }.sort + puts cards.inspect + + # create all permutations of two 5 card hands + hand_scores = Hash.new + cards.combinations(5).each do |c| + other = cards - c + hand_scores[ [c.sort,other.sort].sort ] = nil + end + + puts hand_scores.inspect + puts hand_scores.size + + # now score each set of two 5 card hands + hand_scores.each do |k,v| + score_a = score5(k[0]) + score_b = score5(k[1]) + hand_scores[k] = score_a + score_b unless score_a.nil? || score_b.nil? + end + + # score the 10 card hand + hand_scores[ cards.sort ] = score10(cards) +end + +#score('tsjsqsksastcjcqckcac') + +