-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.rb
More file actions
53 lines (42 loc) · 1.31 KB
/
Card.rb
File metadata and controls
53 lines (42 loc) · 1.31 KB
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
class Card
# the [] with => in it is a shortcut for an array of hashes
# you can get the string value of a symbol via the .to_s, like :red.to_s yields "red"
SUITS = { :hearts => :red,
:diamonds => :red,
:clubs => :black,
:spades => :black }
RANKS = { '9' => 9,
'T' => 10,
'J' => 11,
'Q' => 12,
'K' => 13,
'A' => 14 }
def initialize(rank, suit)
@card = rank.to_s.upcase + suit.to_s[0,1].downcase
end
def to_s
@card
end
def suit
SUITS.keys.select {|s| s.to_s.downcase.start_with?(@card[1]) }.first
end
def color
SUITS.select {|s,v| s.to_s.downcase.start_with?(@card[1]) }.values.first
end
def rank
@card[0]
end
def order(trumpSuit)
# order + 6 if it is trump + 4 if it is a bower + 1 if it is the right bower
RANKS[self.rank] +
(self.is_trump?(trumpSuit) ? 6 : 0) +
((self.rank == 'J' and self.is_trump?(trumpSuit)) ? 4 : 0) +
((self.rank == 'J' and self.suit == trumpSuit and self.color == SUITS[trumpSuit]) ? 1 : 0)
end
def is_trump?(trumpSuit)
(self.suit == trumpSuit) or (self.rank == 'J' and self.color == SUITS[trumpSuit])
end
def beats?(othercard, trumpSuit)
self.order(trumpSuit) > othercard.order(trumpSuit)
end
end