Terse, but almost reasonable version:
class Euchre
OTHER_COLOR = {'c' => 's', 's' => 'c', 'h' => 'd', 'd' => 'h'}
attr_reader :trump_suit
def initialize(suit_name)
@trump_suit = suit_name
@trump = @trump_suit[0,1].downcase
end
def <<(card)
(@hand ||= []) << card
end
def hand
suits = @hand.map {|card| card[1,1]}.uniq
i = suits.index(@trump) and suits.push(suits.slice!(i))
suits[-3],suits[-2] = suits[-2],suits[-3] if suits.length > 2 and
OTHER_COLOR[suits[-1]] == suits[-2]
@hand.sort_by do |x|
rank, suit = x.split('')
if rank == 'J' and @trump == suit : 50
elsif rank == 'J' and OTHER_COLOR[suit] == @trump : 40
else '9TJQKA'.index(rank) + suits.index(suit)*10
end
end.reverse
end
end
euchre = Euchre.new(gets.strip)
5.times { euchre << gets.strip }
puts euchre.trump_suit, euchre.hand
Golfed, but weighing in a good 80 characters more than Ryan's (but,
hey, it's my first golf outing in Ruby.) Like his, it should be one
line.
a=[];6.times{a<<gets.strip};puts t=a.shift;o=Hash[*%w{C S S C H D D
H}];t=t[0,1];s=a.map{|c|c[1,1].upcase}.uniq;i=s.index(t)and
s.push(s.slice!(i));s.length>2&&o[s[-1]]==s[-2]&&(s[-3],s[-2]=s[-2],s[-3]);puts
a.sort_by{|c|r,z=c.upcase.split('');r=='J'&&t==z&&50||r=='J'&&o[z]==t&&40||'9TJQKA'.index(r)+s.index(z)*9}.reverse