Velocity Reviews > Ruby > [QUIZ SOLUTION] Euchre Hands (#55)

# [QUIZ SOLUTION] Euchre Hands (#55)

Daniel Sheppard
Guest
Posts: n/a

 11-21-2005
#
# My Solution - unless a mail agent mangles it=20
# (happens with great frequency)
#

class Card
=20 def initialize(string)
=20 @value =3D string[0..-2].upcase
=20 @suite =3D string[-1..-1].downcase
=20 end
=20 def to_i
=20 case @value
=20 when 'J': 11
=20 when 'Q': 12
=20 when 'K': 13
=20 when 'A': 1
=20 else
=20 @value.to_i
=20 end
=20 end
=20 def to_s
=20 @value + @suite
=20 end
end
class EuchreHand
=20 def initialize(trump, cards)
=20 @trump_string =3D trump
=20 @trump =3D trump[0..0].downcase
=20 @cards =3D cards
=20 end
=20 def to_s
=20 ([@trump_string] http://www.velocityreviews.com/forums/(E-Mail Removed)_by {|c| sort_value(c)}).join("\n"=
)
=20 end
=20 private
=20 #there are 6 sections of the sort: jack1, jack2, trump, opp1, same,=

opp2
=20 def sort_value(card)
=20 return [0] if card.value =3D=3D 'J' && card.suite =3D=3D @trump=

=20 same, opposite =3D [['h','d'],['c','s']].partition {|x|
x.include?(@trump)}
=20 same[0].delete(@trump)
=20 same =3D same[0][0]
=20 opposite =3D opposite[0]
=20 return [1] if card.value =3D=3D 'J' && card.suite
=20 suites =3D [@trump, opposite[0], same, opposite[1]]
=20 return [2+suites.index(card.suite),-card.to_i]
=20 end
end

puts EuchreHand.new(cards.shift, cards.map {|s| Card.new(s)})

################################################## #######################=
############
This email has been scanned by MailMarshal, an email content filter.
################################################## #######################=
############