Class: Cribbage::Card
- Inherits:
-
Object
- Object
- Cribbage::Card
- Defined in:
- lib/cribbage.rb
Overview
AH 5C 6D 7S 8C QD 9C
Constant Summary collapse
- INDEX =
%w(A 2 3 4 5 6 7 8 9 10 J Q K)
- SUITS =
{'H' => '♥', 'D' => '♦', 'C' => '♣', 'S' => '♠'}
Instance Attribute Summary collapse
-
#card_str ⇒ Object
Returns the value of attribute card_str.
-
#face_value ⇒ Object
Returns the value of attribute face_value.
-
#index ⇒ Object
index in a hand.
-
#suit ⇒ Object
Returns the value of attribute suit.
-
#value ⇒ Object
Str format: “5h” = five of hearts “AD” = ace of diamonds “10c” = ten of clubs.
Instance Method Summary collapse
- #compute_value(char) ⇒ Object
-
#initialize(str) ⇒ Card
constructor
A new instance of Card.
- #to_s ⇒ Object
Constructor Details
#initialize(str) ⇒ Card
Returns a new instance of Card.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cribbage.rb', line 28 def initialize(str) raise "String cannot be blank!" if str.nil? raise "Must be a string!" unless str.is_a?(String) raise "Invalid length" if (str.size < 2 or str.size > 3) @value = self.compute_value(str.chop) @face_value = str.chop.upcase raise "Invalid face value" unless INDEX.include?(@face_value) @index = INDEX.index(@face_value) @suit = str[-1].upcase @card_str = str end |
Instance Attribute Details
#card_str ⇒ Object
Returns the value of attribute card_str.
22 23 24 |
# File 'lib/cribbage.rb', line 22 def card_str @card_str end |
#face_value ⇒ Object
Returns the value of attribute face_value.
20 21 22 |
# File 'lib/cribbage.rb', line 20 def face_value @face_value end |
#index ⇒ Object
index in a hand. Ie A = 0,
23 24 25 |
# File 'lib/cribbage.rb', line 23 def index @index end |
#suit ⇒ Object
Returns the value of attribute suit.
21 22 23 |
# File 'lib/cribbage.rb', line 21 def suit @suit end |
#value ⇒ Object
Str format:
"5h" = five of hearts
"AD" = ace of diamonds
"10c" = ten of clubs
19 20 21 |
# File 'lib/cribbage.rb', line 19 def value @value end |
Instance Method Details
#compute_value(char) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/cribbage.rb', line 45 def compute_value(char) return 1 if char.upcase=='A' return 10 if %w(J Q K).include?(char.upcase) if char.to_i > 0 and char.to_i <= 10 return char.to_i end raise "Could not compute value of #{char}" end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/cribbage.rb', line 41 def to_s "[#{@face_value}#{SUITS[@suit]}]" end |