Class: Solitaire::Card
- Defined in:
- lib/quiz1/t/solutions/Moses Hohman/deck.rb,
lib/quiz1/t/solutions/Florian Gross/solitaire.rb
Direct Known Subclasses
Constant Summary collapse
- ACE =
1
- JACK =
11
- QUEEN =
12
- KING =
13
- Faces =
[:ace, :two, :three, :four, :five, :six, :seven, :eight, :nine, :ten, :jack, :queen, :king]
- Types =
[:clubs, :diamonds, :hearts, :spades, :special]
- SpecialFaces =
[:joker_a, :joker_b]
Instance Attribute Summary collapse
-
#code ⇒ Object
(also: #value)
readonly
Returns the value of attribute code.
-
#face ⇒ Object
Returns the value of attribute face.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #compact_inspect ⇒ Object
-
#initialize(suit, value) ⇒ Card
constructor
A new instance of Card.
- #inspect ⇒ Object (also: #to_s)
- #is_joker? ⇒ Boolean
- #letter ⇒ Object
- #name ⇒ Object
- #special? ⇒ Boolean
Constructor Details
#initialize(suit, value) ⇒ Card
Returns a new instance of Card.
63 64 65 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 63 def initialize(suit, value) @code = suit.value + value end |
Instance Attribute Details
#code ⇒ Object (readonly) Also known as: value
Returns the value of attribute code.
66 67 68 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 66 def code @code end |
#face ⇒ Object
Returns the value of attribute face
18 19 20 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 18 def face @face end |
#type ⇒ Object
Returns the value of attribute type
18 19 20 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 18 def type @type end |
Class Method Details
.deck ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 24 def self.deck Types.map do |type| if type == :special SpecialFaces.map do |face| new(face, type) end else Faces.map do |face| new(face, type) end end end.flatten end |
.joker(char) ⇒ Object
83 84 85 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 83 def Card.joker(char) JokerCard.new(char.chr) end |
.parse(code) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 69 def Card.parse(code) if (1..52).member?(code) Card.new(Suit.by_value(code), code.offset_mod(13)) elsif (53..54).member?(code) Card.joker(code+12) elsif (65..66).member?(code) Card.joker(code) elsif code =~ /\A[AB]\Z/ Card.joker(code[0]) else raise "Illegal class or value for parameter value, #{code.class} #{code.inspect}" end end |
Instance Method Details
#<=>(other) ⇒ Object
95 96 97 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 95 def <=>(other) code<=>other.code end |
#==(other) ⇒ Object
91 92 93 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 91 def ==(other) code==other.code end |
#compact_inspect ⇒ Object
61 62 63 64 65 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 61 def compact_inspect if face == :joker_a then "A" elsif face == :joker_b then "B" else value end end |
#inspect ⇒ Object Also known as: to_s
67 68 69 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 67 def inspect "#<#{self.class} #{name} (#{letter}/#{value})>" end |
#is_joker? ⇒ Boolean
87 88 89 |
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 87 def is_joker? false end |
#letter ⇒ Object
47 48 49 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 47 def letter Letters[(value - 1) % 26] end |
#name ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 51 def name if face == :joker_a then "JokerA" elsif face == :joker_b then "JokerB" else face_str = face.to_s.capitalize.gsub(/_(\w)/) { $1.upcase } type_str = type.to_s.capitalize face_str + " of " + type_str end end |
#special? ⇒ Boolean
38 |
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 38 def special?; type == :special; end |