initial commit
This commit is contained in:
commit
781bb1aa5a
2 files changed
+143
No files matched your search
@@ -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
|
||||||
|
|
||||||
@@ -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')
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user