-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOP_BlackJack_Solution.rb
277 lines (210 loc) · 5.05 KB
/
OOP_BlackJack_Solution.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Exact the primary nouns
class Card
attr_accessor :suit, :face_value
def initialize(suit, face_value)
@suit = suit
@face_value = face_value
end
def pretty_output
"The #{face_value} of #{find_suit}"
end
def card
pretty_output
end
def to_s
pretty_output
end
def find_suit
case suit
when "H" then "Hearts"
when "D" then "Diamonds"
when "C" then "Clubs"
when "S" then "Spades"
end
end
end
class Deck
attr_accessor :cards
def initialize
@cards = []
['H', 'D', 'C', 'S'].each do |suit|
['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'].each do |value|
@cards << Card.new(suit, value)
@cards = @cards.shuffle
end
end
end
def deal_one
cards.pop
end
end
require 'pry'
module Hand
def show_hand
puts "--- #{name}'s Hand ---"
cards.each do |card|
puts "=> #{card}"
end
puts "=> Total: #{total}"
end
def total
face_values = cards.map{|card| card.face_value}
total = 0
face_values.each do |val|
if val == "Ace"
total += 11
else
total += (val.to_i == 0 ? 10 : val.to_i)
end
end
#correct for aces
face_values.select{|val| val == "Ace"}.count.times do
break if total <= BlackJack::BLACKJACK_AMOUNT
total -= 10
end
total
end
def is_busted?
total > BlackJack::BLACKJACK_AMOUNT
end
def add_card(new_card)
cards << new_card
end
end
class Player
include Hand
attr_accessor :name, :cards
def initialize(name)
@name = name
@cards = []
end
end
class Dealer
include Hand
attr_accessor :cards, :name
def initialize
@name = "Dealer"
@cards = []
end
def show_flop
puts "---- Dealer's Hand -----"
puts "=> First card is hidden."
puts "=> Second card is #{cards[1]}"
end
end
class BlackJack
attr_accessor :player, :deck, :dealer
BLACKJACK_AMOUNT = 21
DEALER_HIT_MIN = 17
def initialize
@deck = Deck.new
@player = Player.new("Player1")
@dealer = Dealer.new
end
def set_player_name
puts "What's your name?"
player.name = gets.chomp
end
#back and forth dealing for player and dealer
def deal_cards
player.add_card(deck.deal_one)
dealer.add_card(deck.deal_one)
player.add_card(deck.deal_one)
dealer.add_card(deck.deal_one)
end
def show_flop
player.show_hand
dealer.show_flop
end
# checking for score busting
def blackjack_or_bust?(player_or_dealer)
if player_or_dealer.total == BLACKJACK_AMOUNT
if player_or_dealer.is_a?(Dealer)
puts "Sorry, dealer hit BlackJack. #{player.name} loses."
play_again?
else
puts "Congrats. #{player.name} hit BlackJack. #{player.name} wins!"
play_again?
end
elsif player_or_dealer.is_busted?
if player_or_dealer.is_a?(Player)
puts "Sorry, #{player.name} busted. #{player.name} loses."
play_again?
else
puts "Congrats, dealer busted. #{player.name} wins!"
play_again?
end
end
end
def play_again?
puts ""
puts "Would you like to play again? 1)Yes 2)No"
if gets.chomp == "1"
puts "Starting new game..."
deck = Deck.new
player.cards = []
dealer.cards = []
start
else
puts "Goodbye!"
exit
end
end
# define the player's turn and logic
def player_turn
puts "It's #{player.name}'s turn."
blackjack_or_bust?(player)
while !player.is_busted?
puts "What do you want to do? 1) Hit 2) Stay"
response = gets.chomp
if !["1", "2"].include?(response)
puts "Error, you must enter 1 or 2."
next
end
if response == "2"
puts "#{player.name} choses to stay."
break
end
new_card = deck.deal_one
puts "Dealing card to #{player.name}: #{new_card}"
player.add_card(new_card)
puts "#{player.name}'s total is now: #{player.total}"
blackjack_or_bust?(player)
end
end
def dealer_turn
puts "Dealer's Turn."
puts ""
puts "Dealer has: #{dealer.cards[0]} and #{dealer.cards[1]}."
blackjack_or_bust?(dealer)
while dealer.total < DEALER_HIT_MIN
new_card = deck.deal_one
puts "Dealing card to dealer: #{new_card}"
dealer.add_card(new_card)
puts "Dealer total is now: #{dealer.total}"
end
blackjack_or_bust?(dealer)
puts "Dealer stays with: #{dealer.total}."
who_won?
end
def who_won?
if player.total > dealer.total
puts "Congrats. #{player.name}'s #{player.total} beats the dealer's #{dealer.total}. #{player.name} wins!"
elsif player.total < dealer.total
puts "Sorry. The dealer's #{dealer.total} beats #{player.name}'s #{player.total}. #{player.name} loses."
else
puts "It's a push. House wins. Sorry."
end
play_again?
end
# running this method will start the game off. It executes all other methods.
def start
set_player_name
deal_cards
show_flop
player_turn
dealer_turn
end
end
game = BlackJack.new
game.start