-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
249 lines (247 loc) · 5.75 KB
/
game.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
require './dealer.rb'
require './player.rb'
require './cards_and_deck.rb'
class Game
def initialize
get_player
@cards = Deck.new
@dealer = Dealer.new
@player_score = 0
end
#gets the players name
def get_player
@player = Player.new("")
puts "Please enter your name...."
@player.name = gets.chomp.to_s
end
#deals hands
def deal_cards
puts " "
puts "Dealing cards..."
puts " "
2.times do
@dealer.hand << @cards.deck.shift
end
2.times do
@player.hand << @cards.deck.shift
end
end
#declares one card of the dealers hand
def announce_dealer_hand
d_hand = @dealer.hand.first
puts "The dealer is showing"
puts "#{d_hand.rank} #{d_hand.suit}"
puts " "
end
#declares full dealer hand
def announce_full_dealer_hand
puts "The dealer is showing"
@dealer.hand.each do |card|
puts"#{card.rank} #{card.suit}"
end
end
#declares full player hand
def announce_player_hand
puts "You have been dealt:"
@player.hand.each do |card|
puts "#{card.rank} #{card.suit}"
end
end
#gets a players score, possible refactor, do I really need two methods for this?
def get_initial_player_score
faces = %w(J Q K A)
ranks = %W(2 3 4 5 6 7 8 9 10)
@player.hand.each do |card|
if ranks.include?(card.rank)
score = card.rank.to_i
@player_score += score
elsif faces.include?(card.rank)
if card.rank == "A"
is_ace(card.rank)
else
@player_score += 10
end
end
end
puts " "
puts "Score: #{@player_score}"
puts " "
end
#adjust the players score after hit
def adjust_player_score
faces = %w(J Q K A)
ranks = %W(2 3 4 5 6 7 8 9 10)
card = @player.hand.last
if ranks.include?(card.rank)
score = card.rank.to_i
@player_score += score
elsif faces.include?(card.rank)
if card.rank == "A"
is_ace(card.rank)
else
@player_score += 10
end
end
puts " "
puts "Score: #{@player_score}"
puts " "
end
#gets the score, possible refactor, do I really need two methods for this?
def get_dealer_score
@dealer_score = 0
faces = %w(J Q K A)
ranks = %W(2 3 4 5 6 7 8 9 10)
@dealer.hand.each do |card|
if ranks.include?(card.rank)
score = card.rank.to_i
@dealer_score += score
elsif faces.include?(card.rank)
if card.rank == "A"
@dealer_score += 11
else
@dealer_score += 10
end
end
end
puts " "
puts "Score: #{@dealer_score}"
puts " "
end
#shift cards from deck to a players hand
def player_hit
@player.hand << @cards.deck.shift
end
#shift cards from the deck to the dealers hand
def dealer_hit
@dealer.hand << @cards.deck.shift
end
#method for player action, possible refactor? It's a bit ugly
def stick_or_twist
puts "Stick(s) or twist(t)?"
choice = gets.chomp.to_s.downcase
unless choice == "s" || choice == "t"
puts "Stick(s) or twist(t)?"
choice = gets.chomp.to_s.downcase
end
if choice == "s"
puts "Score: #{@player_score}"
return "s"
else
player_hit
announce_player_hand
adjust_player_score
end
end
#aces high or low in opening hand
def is_ace(card_rank)
puts "Make this ace high(h) or low(l)?"
choice = gets.chomp.to_s.downcase
unless choice == "h" || choice == "l"
puts "Make this ace high(h) or low(l)?"
choice = gets.chomp.to_s.downcase
end
if choice == "l"
@player_score += 1
else
@player_score += 11
end
end
#loop for player turn
def player_turn
puts "You have £#{@player.bankroll}"
while @player_score < 22
if stick_or_twist == "s"
break
end
end
end
#sets player wager. Sets itself to zero every time it is called
def player_bet
puts "You have £#{@player.bankroll}"
puts "Dealer has £#{@dealer.bankroll}"
@player_wager = 0
until @player_wager > 0 && @player_wager <= @player.bankroll
puts "Place your bets please"
@player_wager = gets.chomp.to_i
end
puts " "
puts "#{@player.name} bets £#{@player_wager}"
@player.bankroll -= @player_wager
end
#dealer turn, checks if player is bust before acting
def dealer_turn
if @player_score > 21
puts "You are bust!"
else
announce_full_dealer_hand
get_dealer_score
while @dealer_score < 17
puts "Dealer hits."
dealer_hit
announce_full_dealer_hand
get_dealer_score
end
if @dealer_score > 21
puts "Dealer is bust!"
else
puts "Dealer stands."
end
end
end
#determin winner and adjust bankrolls accordingly
def result
if @player_score >21
puts "Dealer wins"
@dealer.bankroll += @player_wager
elsif @player_score < 21 && @dealer_score > 21
puts "Player wins"
@player.bankroll += @player_wager * 2
@dealer.bankroll -= @player_wager
elsif (@dealer_score > @player_score) && (@dealer_score <22) && (@player_score && @dealer_score <22)
puts "Dealer wins"
@dealer.bankroll += @player_wager
elsif (@player_score > @dealer_score) && (@player_score < 22) && (@dealer_score && @player_score < 22)
puts "Player wins!"
@player.bankroll += @player_wager * 2
@dealer.bankroll -= @player_wager
elsif
(@player_score == @dealer_score) && (@player_score < 22) && (@dealer_score && @player_score < 22)
puts "Hand is a draw"
@player.bankroll += @player_wager
end
end
#reset the deck
def reset
@player_score = 0
@player.hand.each do |card|
@cards.deck << card
end
@player.hand.replace([])
@dealer.hand.each do |card|
@cards.deck << card
end
@dealer.hand.replace([])
end
#main game loop
def play
until @player.bankroll == 0 || @dealer.bankroll <= 0
@cards.shuffle_cards
player_bet
deal_cards
announce_dealer_hand
announce_player_hand
get_initial_player_score
player_turn
dealer_turn
result
reset
end
if @dealer.bankroll <= 0
puts "CONGRATULATIONS!! You have won the game!"
else
puts "Better luck next time, the dealer took all your cash!"
end
end
end
a = Game.new
a.play