-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdeck.rb
51 lines (43 loc) · 1.04 KB
/
deck.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
# encoding: utf-8
class Deck < Array
attr_accessor :cards
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize
super
SUITS.each do |suit|
ace = Card[:value => 11, :suit => suit, :card => "ace"]
push(ace)
(2..10).each do |n|
card = Card[:value => n, :suit => suit, :card => n]
push(card)
end
["jack", "queen", "king"].each do |n|
card = Card[:value => 10, :suit => suit, :card => n]
push(card)
end
end
self
end
class Card < Hash
SYMBOLS = {
:clubs => "♣",
:diamonds => "♦",
:hearts => "♥",
:spades => "♠"
}
def soft_ace?
self[:card] == 'ace' && self[:value] == 11
end
def to_s
suit = SYMBOLS[self[:suit]]
if self[:card].kind_of?(String)
"#{self[:card][0,1].upcase}#{suit} "
else
"#{self[:card]}#{suit} "
end
end
def inspect
to_s
end
end
end