-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOP_BlackJack.rb
236 lines (168 loc) · 4.88 KB
/
OOP_BlackJack.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
# STEPS TO CREATING AN OOP APPLICATION ###
# 1. Create the requirements and specifications of the blackjack game.
# 2. Extract the nouns and turn them into classes.
# 3. Extract the verbs and turn them into instance methods.
# 4. Group instance methods into classes
# 5. Create a 'Game Engine' that makes the objects interact with one another
# -------------------------------------------------------------- #
# Step 1: Game Specifications #
# - Ask for the players name
# - The deck is shuffled and cards are dealt in a back& forth manner
# - - Dealer has 1 face-up and 1 face-down card. Players cards are both face-up
# - Player hits or stays until they reach 21, break 21, or choose to stay
# - Dealer has a set of rules to follow for their hit & stay procedures
# - After dealer has completed turn, the hands are evaluated to see who wins
# - After completion, player is asked if they want to play again
# -------------------------------------------------------------- #
# Step 2. Extract Major Nouns #
# Player
# Dealer
# Cards
# Deck
# The Game of Blacjack (game engine)
# -------------------------------------------------------------- #
# Step 3. Extract Major Verbs #
# Deal
# Shuffle
# Evaluate Score
# Hit or Stay
# Bust
# -------------------------------------------------------------- #
# Step 4. Group Methods into Classes #
###------START MODULES--------####
# This module is to be included in any class where a score needs to be evaluated
module Hand
attr_accessor :hand, :score
def evaluate_score(cards)
#an array of objects [<obj12345 @suit = 'Spade' @value = 'Ace'>, <ob12345 @suit = 'Club', @value = 'King'>]
self.score = 0
cards.each do |x|
if x.value == "Ace"
self.score += 11
elsif x.value.to_i == 0 # J, Q, K
self.score += 10
else
self.score += x.value.to_i
end
end
#correct for Aces
cards.select{|e| e.value == "Ace"}.count.times do
self.score -= 10 if self.score > 21
end
self.score
end
def add_card(card)
self.hand << card
end
end
###-----END MODULES---------###
class Player
attr_accessor :name
include Hand
def initialize(name)
super()
@name = name
@score =
@hand = []
end
end
class Dealer
attr_reader :score, :hand
include Hand
def initialize
super()
@name = "Mr. Dealer"
@hand = []
@score = 0
end
private
#FIGURE OUT DEALER TURN
# def dealer_turn #takes current count, returns True if dealer hits and False if dealer stays
# if self.score == 21
# puts "Dealer got BlackJack and wins."
# exit
# elsif self.score < 16
# puts "Dealer has less than 16 and must hit."
# self.hand=
# elsif self.score >= 16
# puts "Dealer has over 17 and must stay."
# end
# end
end
class Card
attr_accessor :name, :value, :suit
def initialize(value, suit)
@name = "#{value} of #{suit}"
@value = value
@suit = suit
end
end
class Deck
attr_accessor :cards
def initialize
@cards = []
[2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace'].each do |value|
['Diamonds', 'Hearts', 'Spades', 'Clubs'].each do |suit|
@cards << Card.new(value, suit)
@cards = @cards.shuffle
end #ends innder DO
end #ends outer DO
end
def deal
self.cards.pop #returns 1 card
end
end
# -------------------------------------------------------------- #
# Step 5. Create 'Game Engine' Class called BlackJack
class BlackJack
attr_accessor :player, :deck, :dealer
include Hand
def initialize
puts "Welcome. Let's get started. What's your name?"
name = gets.chomp
@player = Player.new("#{name}")
@dealer = Dealer.new()
@deck = Deck.new()
end
def play_game
puts "Welcome, #{self.player.name}. Here is your first card."
card1 = self.deck.deal
self.player.add_card(card1)
puts "You got a a #{card1.name}"
card2 = self.deck.deal
self.dealer.add_card(card2)
puts "Dealer got a #{card2.name}"
card3 = self.deck.deal
self.player.add_card(card3)
puts "You got a a #{card3.name}"
card4 = self.deck.deal
self.dealer.add_card(card4)
puts "Dealer's last card is face down."
player_total = self.player.evaluate_score(self.player.hand)
puts "Your total is #{player_total}"
next_move(self.player.hand)
end
def player_hit
card = self.deck.deal
self.player.add_card(card)
puts "You got a #{card.name}"
puts "Your total is now #{self.player.evaluate_score(self.player.hand)}"
end
def player_stay
end
def next_move(cards)
if self.evaluate_score(cards) > 21
puts "You busted. Sorry."
elsif self.evaluate_score(cards) == 21
puts "You hit BlackJack!"
else
puts "Your score is less than 21. Do you want to hit or stay?"
response = gets.chomp.capitalize
if response == "Hit"
self.player_hit
else
self.player_stay
end
end
end
end