-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.rb
81 lines (53 loc) · 1.35 KB
/
testing.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
70
71
72
73
74
75
76
77
78
require "pry"
def say(msg)
puts "--> #{msg}"
end
def show_cards(hand)
hand.map { |pair| "#{pair[1]}#{pair[0]}" }.join(", ")
end
def say(msg)
puts "--> #{msg}"
end
def hit(hand)
hand << deck.pop
end
def calculate_total(hand)
array_of_faces = hand.map {|pair| pair[1]}
total = 0
array_of_faces.each do |face|
if face == "A"
total += 11
# capture J, Q, K
elsif face.to_i == 0
total += 10
else
total += face.to_i
end
end
# correct for aces
array_of_faces.count{ |face| face == "A"}.times do
total -= 10 if total > 21
end
total
end
say "Welcome to blackjack. What's your name?"
players_name = gets.chomp
say "Great, nice to meet you #{players_name}. Let's get started."
say "I'll shuffle the cards....."
suits = %w(♣ ♥ ♠ ♦)
cards = %w(2 3 4 5 6 7 8 9 10 J Q K A)
deck = suits.product(cards)
deck = deck.shuffle!
#Dealing
players_hand = []
dealers_hand =[]
players_hand << deck.pop
dealers_hand << deck.pop
players_hand << deck.pop
dealers_hand << deck.pop
#First round
players_total = calculate_total(players_hand)
dealers_total = calculate_total(dealers_hand)
say "#{show_cards(players_hand)}"
# say "Dealer's cards: \n#{show_cards(dealers_hand)} Dealer has a total of: #{dealers_total}."
# say "Your cards: \n#{show_cards(players_hand)} You have a total of: #{players_total}."