-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.rb
More file actions
188 lines (160 loc) · 3.93 KB
/
Copy pathblackjack.rb
File metadata and controls
188 lines (160 loc) · 3.93 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# blackjack.rb
SUITS = ['Hearts', 'Spades', 'Diamonds', 'Clubs']
RANKS = [*2..10, 'Jack', 'Queen', 'King', 'Ace']
CARDS = []
SUITS.product( RANKS ) { | suit, rank | CARDS << [rank, suit] }
def print_cards cards
puts "#{cards.count}-card deck"
cards.each do | rank, suit |
puts " #{rank} of #{suit}"
end
end
def shuffle_cards cards
cards.shuffle!
end
def cut_cards cards
cards.rotate! cards.count / 2
end
def cards_contain cards, card
cards.each do | rank, suit |
if rank == card
return true
end
end
false
end
def print_card card
rank, suit = card
puts "#{rank} of #{suit}"
end
def draw_card cards
cards.shift
end
def value_of_card card
rank, suit = card
case rank
when 'Ace'
11
when 'King'
10
when 'Queen'
10
when 'Jack'
10
else
rank.to_i
end
end
def value_of_hand hand
ace_count = 0
value = 0
hand.each do | card |
rank, suit = card
value += value_of_card card
if rank == 'Ace'
ace_count += 1
end
end
while value > 21 && ace_count > 0
value -= 10
ace_count -= 1
end
value
end
def player_strategy hand, dealer_up_card
strategy = :stand
hand_value = value_of_hand hand
card_value = value_of_card dealer_up_card
if hand.count == 2 && cards_contain(hand, 'Ace')
if hand_value >= 19
strategy = :stand
elsif hand_value == 18 && [2, 7, 8].include?(card_value)
strategy = :stand
else
strategy = :hit
end
else
if hand_value <= 11 || (hand_value == 12 && card_value.between?(2, 3))
strategy = :hit
elsif hand_value >= 17 || card_value.between?(2, 6)
strategy = :stand
elsif hand_value < (card_value + 10)
strategy = :hit
end
end
strategy
end
def dealer_strategy hand
v = value_of_hand hand
v >= 17 && :stand || :hit
end
def results_of_hand hand
v = value_of_hand hand
case v
when 21
hand.count == 2 && :blackjack || 21
when 12..20
v
when 0..11
raise "error, illegal hand"
else
:bust
end
end
def load_shoe number_of_decks
shoe = []
for i in 1..number_of_decks
shoe += CARDS
end
shoe
end
def game_simulation number_of_decks = 6, percent_reserved = 25.0
cards = load_shoe number_of_decks
number_of_reserve_cards = (cards.count.to_f * percent_reserved / 100).to_i
# puts "Playing blackjack with #{number_of_decks} decks (#{cards.count} cards)" +
# " and #{percent_reserved}% (#{number_of_reserve_cards} cards) in reserve"
dealer_wins = 0
player_wins = 0
pushes = 0
cards = shuffle_cards cards
cards = cut_cards cards
while cards.count > number_of_reserve_cards
player = []
dealer = []
for i in 1..2
card = draw_card cards
player += [card]
card = draw_card cards
dealer += [card]
end
while player_strategy(player, dealer[1]) == :hit
card = draw_card cards
player += [card]
end
while dealer_strategy(dealer) == :hit
card = draw_card cards
dealer += [card]
end
player_result = results_of_hand(player)
dealer_result = results_of_hand(dealer)
if (dealer_result == :blackjack && player_result != :blackjack) || player_result == :bust
# puts "Dealer wins with #{dealer_result}"
dealer_wins += 1
elsif player_result == :blackjack || dealer_result == :bust
# puts "Player wins with #{player_result}"
player_wins += 1
elsif dealer_result > player_result
# puts "Dealer wins with #{dealer_result}"
dealer_wins += 1
elsif player_result > dealer_result
# puts "Player wins with #{player_result}"
player_wins += 1
else
# puts "Push with #{dealer_result}"
pushes += 1
end
# print_cards player
# print_cards dealer
end
return dealer_wins, player_wins, pushes
end