[QUIZ SOLUTION] Euchre Hands (#55)

D

Daniel Sheppard

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

class Card
=20 attr_reader :suite, :value
=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] (e-mail address 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

cards =3D ARGF.readlines
puts EuchreHand.new(cards.shift, cards.map {|s| Card.new(s)})




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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top