-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.rb
68 lines (55 loc) · 1.04 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
55
56
57
58
59
60
61
62
63
64
65
66
class String
def red
"\033[31m#{self}\033[0m"
end
end
class Card
SUIT_STRINGS = {
:clubs => "♣",
:diamonds => "♦",
:hearts => "♥",
:spades => "♠"
}
VALUES = {
:two => 2,
:three => 3,
:four => 4,
:five => 5,
:six => 6,
:seven => 7,
:eight => 8,
:nine => 9,
:ten => 10,
:jack => :J,
:queen => :Q,
:king => :K,
:ace => :A
}
SUITS = {
:clubs => "♣",
:diamonds => "♦",
:hearts => "♥",
:spades => "♠"
}
def to_s
output = "[#{VALUES[self.value].to_s} #{SUIT_STRINGS[self.suit]}]"
if self.suit == :diamonds || self.suit == :hearts
output.red
else
output
end
end
def self.suits
SUITS.keys
end
def self.values
VALUES.keys
end
attr_reader :suit, :value
def initialize(suit, value)
unless Card.suits.include?(suit) && Card.values.include?(value)
raise "Illegal suit/value"
end
@suit, @value = suit, value
end
end