-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.rb
54 lines (44 loc) · 1.02 KB
/
card.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Card
SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
# All Cards have a Card ID to identifier them within the suit
# - IDs must be unique
# - Cards 1..10 have ID 1..10 respectively
# - Others IDs are defined below
ACE_ID = 1
JACK_ID = 11
QUEEN_ID = 12
KING_ID = 13
PICTURE_CARD_DESCRIPTION = { ACE_ID => 'Ace', JACK_ID => 'Jack', QUEEN_ID => 'Queen', KING_ID => 'King' }
attr_accessor :suit, :id
def initialize(suit, id)
@suit = suit
@id = id
end
def value
if id == ACE_ID
value = 11
elsif id >= JACK_ID
value = 10
else
# All other IDs (2..10) reflect the card value
value = id
end
end
def description
if PICTURE_CARD_DESCRIPTION.key?(id)
PICTURE_CARD_DESCRIPTION[id]
else
id.to_s
end
end
def to_s
if PICTURE_CARD_DESCRIPTION.key?(id)
"#{PICTURE_CARD_DESCRIPTION[id]} of #{suit}"
else
"#{id} of #{suit}"
end
end
def set_low_ace
@id = ACE_LOW_ID if id == ACE_HIGH_ID
end
end