-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.rb
69 lines (58 loc) · 1.16 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
self.rank = rank
self.suit = suit
end
def card_name
return "#{self.rank} of #{self.suit}"
end
def output_card
print card_name
end
def self.random_card
Card.new(rand(10), :spades)
end
end
class Deck
def initialize()
@cards = []
suits = ["spades", "heart", "diamond", "club"]
ranks = ["ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "jack", "queen", "king"]
for suit in suits
for rank in ranks
@cards << Card.new(rank, suit)
end
end
end
def shuffle
@cards.shuffle!
end
def deal
if @cards.empty?
nil
else
card = @cards[0]
@cards.shift(1)
return card
end
end
def output()
count = 0
for item in @cards
count = count + 1
puts "#{count}: #{item.card_name}"
end
end
end
puts "***** get a deck"
deck = Deck.new
puts "***** shuffle"
deck.shuffle
puts "***** output deck"
deck.output
puts "***** deal"
top_card = deck.deal
puts "delt card = #{top_card.card_name}"
puts "***** output deck"
deck.output