Class: Solitaire::Card

Inherits:
Struct
  • Object
show all
Defined in:
lib/quiz1/t/solutions/Moses Hohman/deck.rb,
lib/quiz1/t/solutions/Florian Gross/solitaire.rb

Direct Known Subclasses

JokerCard

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

Class Method Summary collapse

Instance Method Summary collapse

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

#codeObject (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

#faceObject

Returns the value of attribute face

Returns:

  • (Object)

    the current value of face



18
19
20
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 18

def face
  @face
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



18
19
20
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 18

def type
  @type
end

Class Method Details

.deckObject



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_inspectObject



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

#inspectObject 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

Returns:

  • (Boolean)


87
88
89
# File 'lib/quiz1/t/solutions/Moses Hohman/deck.rb', line 87

def is_joker?
	false
end

#letterObject



47
48
49
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 47

def letter
  Letters[(value - 1) % 26]
end

#nameObject



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

Returns:

  • (Boolean)


38
# File 'lib/quiz1/t/solutions/Florian Gross/solitaire.rb', line 38

def special?; type == :special; end