Files
game-scoring-scripts/rank.rb
T
2008-08-25 10:04:01 -07:00

71 lines
1.5 KiB
Ruby

#!/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')