RB
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
# ♥️Challenge♥️ Shuffling a new deck of cards
# Assume a new deck of cards is organized as follows:
# ♠️1, ♠️2, ..., ♠️10, ♠️J, ♠️Q, ♠️K, ♦️1, ♦️2, ..., ♦️10, ♦️J, ♦️Q, ♦️K, ♣️1, ♣️2, ..., ♣️10, ♣️J, ♣️Q, ♣️K, ♥️1, ♥️2, ..., ♥️10, ♥️J, ♥️Q, ♥️K
# Write a program to do the following:
# 1. Generate a new deck of cards and print it nicely!
# 2. Shuffle and print it three times!
# For explanations see the following answer:
def initialize_deck
values = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K" ]
suits = [ "Hearts", "Spades", "Clubs", "Diamonds" ]
deck = suits.product(values)
(0..deck.size-1).each {|x| deck[x] = deck[x].join(' ')}
return deck
end
def show(deck)
(0..12).each {|x|
print "| #{deck[x].center(12)} | #{deck[x+13].center(12)} | #{deck[x+26].center(12)} | #{deck[x+39].center(12)} |\n"
}
end
def in_riffle_shuffle(deck)
first_pile = deck.slice(0, 26)
second_pile = deck.slice(26, 51)
deck.clear
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run