scoring is functional!

This commit is contained in:
swaits committed 2008-08-28 18:52:35 -07:00
1 parent 781bb1aa5a
commit 6ad0b7e9d2
4 files changed
+169 -74

No files matched your search

+50 -56
View File
@@ -1,70 +1,64 @@
#!/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 'hand'
require 'combinations'
require 'card'
def score(hand_string, verbose=false)
# split into an array of two character strings (each representing a card)
cards = hand_string.scan(/../)
def score5(hand,verbose=false)
# reject illegal hands (hands with duplicate cards)
return nil if hand.sort.uniq.size < 5
return 0
end
# a place to store all of our scores
best_hands = nil
best_score = -1
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
# create and score all permutations of two 5 card hands
cards.combinations(5).each do |c|
other = cards - c
hand_scores[ [c.sort,other.sort].sort ] = nil
# create each hand from two 10 character strings
hand_a = Hand.new(c.join)
hand_b = Hand.new((cards-c).join)
score = hand_a.score + hand_b.score
# save the hand if it's the best
best_hands,best_score = [hand_a,hand_b],score if score > best_score
end
puts hand_scores.inspect
puts hand_scores.size
# now create and score the 10 card hand
ten_card_hand = Hand.new(cards.join)
best_hands,best_score = [ten_card_hand],ten_card_hand.score if ten_card_hand.score > best_score
# 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?
# DEBUG
if verbose then
best_hands.each do |h|
puts "#{h.score} points for a #{h.score_name}"
puts h
puts
end
puts "#{best_score} TOTAL POINTS"
end
# score the 10 card hand
hand_scores[ cards.sort ] = score10(cards)
# return score
best_score
end
#score('tsjsqsksastcjcqckcac')
score('tsjsqsksastcjcqckcac',true)
puts
puts
score('3d8c6s7d8h9s9dkdqd5d',true)
puts
puts
score('2s3s4s5s6s7s8s9stsjs',true)
puts
puts
score('2c3s4c5d6h7s8d9sthjs',true)
puts
puts
score('as3s4s5s6s7s8s9stsjs',true)
puts
puts
score('asadacah6s7s8s9stsjs',true)
puts
puts
score('asadackskd7s8s9stsjs',true)
puts
puts