-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack2.rb
More file actions
232 lines (193 loc) · 4.36 KB
/
Copy pathblackjack2.rb
File metadata and controls
232 lines (193 loc) · 4.36 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# blackjack2.rb
class Card
RANKS = [*2..10, :Jack, :Queen, :King, :Ace]
SUITS = [:Hearts, :Spades, :Diamonds, :Clubs]
attr_reader :rank, :suit, :value
def initialize rank, suit = :Clubs
if RANKS.include?(rank) && SUITS.include?(suit)
@rank = rank
@suit = suit
@value = rank_value
else
raise ArgumentError, "invalud :rank or :suit"
end
end
def to_s
"#{@rank} of #{@suit}"
end
def rank_value
case self.rank
when :Ace
11
when :King
10
when :Queen
10
when :Jack
10
else
self.rank
end
end
private :rank_value
end
class Deck
attr_reader :number_of_decks, :percent_reserved
attr_reader :in_reserve, :count
def initialize number_of_decks = 1, percent_reserved = 20.0
@number_of_decks = number_of_decks
@percent_reserved = percent_reserved
@cards = []
Card::SUITS.product( Card::RANKS ) { | suit, rank | @cards << Card.new(rank, suit) }
@cards = @cards * @number_of_decks
@number_of_reserve_cards = (@cards.count.to_f * @percent_reserved / 100).to_i
end
def print
puts "#{@cards.count}-card deck"
@cards.each do | card |
puts " #{card}"
end
end
def shuffle
@cards.shuffle!
end
def cut
@cards.rotate! @cards.count / 2
end
def draw
@cards.shift
end
def count
@cards.count
end
def in_reserve?
@cards.count < @number_of_reserve_cards
end
end
class Hand
attr_reader :count, :value
def initialize hand = nil
if hand.nil?
@hand = []
@value = nil
else
@hand = [hand]
@value = hand_value
end
end
def result
case self.value
when 21
self.count == 2 && :blackjack || 21
when 12..20
value
when 0..11
raise "error, #{self.value} is an illegal hand"
else
:bust
end
end
def add_card card
@hand += [card]
@value = hand_value
end
def count
@hand.count
end
def card position
@hand[position]
end
def contains? rank
@hand.each do | card |
if rank == card.rank
return true
end
end
false
end
def hand_value
ace_count = 0
value = 0
@hand.each do | card |
value += card.value
if card.rank == :Ace
ace_count += 1
end
end
while value > 21 && ace_count > 0
value -= 10
ace_count -= 1
end
value
end
private :hand_value
end
class Blackjack
attr_reader :total, :wins, :pushes, :losses
def initialize number_of_decks = 6, percent_reserved = 25.0
@number_of_decks = number_of_decks
@percent_reserved = percent_reserved
@total = 0
@wins = 0
@pushes = 0
@losses = 0
end
def play
@cards = Deck.new @number_of_decks, @percent_reserved
@cards.shuffle
@cards.cut
while !@cards.in_reserve? do
player = Hand.new @cards.draw
dealer = Hand.new @cards.draw
player.add_card(@cards.draw)
dealer.add_card(@cards.draw)
while player_strategy(player, dealer.card(1)) == :hit do
player.add_card(@cards.draw)
end
while dealer_strategy(dealer) == :hit do
dealer.add_card(@cards.draw)
end
if (dealer.result == :blackjack && player.result != :blackjack) || player.result == :bust
@losses += 1
elsif player.result == :blackjack || dealer.result == :bust
@wins += 1
elsif dealer.result > player.result
@losses += 1
elsif player.result > dealer.result
@wins += 1
else
@pushes += 1
end
@total += 1
end
end
def player_strategy hand, dealer_up_card
strategy = :stand
if hand.count == 2 && hand.contains?(:Ace)
if hand.value >= 19
strategy = :stand
elsif hand.value == 18 && [2, 7, 8].include?(dealer_up_card.value)
strategy = :stand
else
strategy = :hit
end
else
if hand.value <= 11 || (hand.value == 12 && dealer_up_card.value.between?(2, 3))
strategy = :hit
elsif hand.value >= 17 || dealer_up_card.value.between?(2, 6)
strategy = :stand
elsif hand.value < (dealer_up_card.value + 10)
strategy = :hit
end
end
strategy
end
def dealer_strategy hand
hand.value >= 17 && :stand || :hit
end
def hand_builder *ranks
hand = Hand.new
ranks.each { |rank| hand.add_card( Card.new(rank)) }
hand
end
end