-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.rb
More file actions
52 lines (42 loc) · 969 Bytes
/
deck.rb
File metadata and controls
52 lines (42 loc) · 969 Bytes
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
require_relative 'card'
#Deck of cards is an array of 52 cards... requiring suits, ranks, deck
class Deck
SUITS = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
attr_reader :deck, :suits, :ranks
def initialize(suits, ranks)
@deck = []
@suits = suits
@ranks = ranks
create_deck
end
def shuffle
@deck.shuffle!
end
def deal_card
@deck.pop
end
def replace_with(new_deck)
@suits = []
@ranks = []
@deck = new_deck
new_deck.each do |card|
add_suit_and_rank(card)
end
self
end
private
def create_deck
suits.each do |suit|
ranks.each do |rank|
@deck.push(Card.new(suit, rank))
end
end
end
def add_suit_and_rank(card)
suit = card.suit
rank = card.rank
@suits.push suit unless @suits.include? suit
@ranks.push rank unless @ranks.include? rank
end
end