-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
256 lines (209 loc) · 6.46 KB
/
main.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
require 'rubygems'
require 'sinatra'
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'your_secret'
helpers do
def calculate_total(cards)
# [['H', '3'], ['S', 'Q'], ... ]
arr = cards.map {|i| i[1] }
total = 0
arr.each do |value|
if value == "A"
total += 11
elsif value.to_i == 0
total += 10
else
total += value.to_i
end
end
arr.count("A").times do
total -= 10 if total > 21
end
total
end
def display_card(card)
suit = case card[0]
when 'H' then 'hearts'
when 'D' then 'diamonds'
when 'S' then 'spades'
when 'C' then 'clubs'
end
value = case card[1]
when 'A' then 'ace'
when 'J' then 'jack'
when 'K' then 'king'
when 'Q' then 'queen'
else value = card[1]
end
return "<img src='/images/cards/#{suit}_#{value}.jpg'>"
end
def check_name(name)
if /[0-9\W]/.match(name)
@error = "Name must be letters only."
erb :new_player
elsif name.empty?
@error = "Name cannot be blank."
erb :new_player
else
session[:player_name] = name
redirect '/bet'
end
end
def check_bet(amount)
if /^\-/.match(amount)
@error = "Bet must be greater than 0."
elsif /[a-zA-Z\W]/.match(amount)
@error = "Enter numbers only."
elsif amount.to_i > session[:bank_roll]
@error = "Bet must be less than or equal to #{session[:bank_roll]}."
else
session[:bet_amount] = amount
redirect '/game'
end
end
def initial_hand_check(dealer_cards, player_cards)
if calculate_total(dealer_cards) == 21 && calculate_total(player_cards) == 21
@show_hit_or_stand_buttons = false
@tie = "You and the dealer have 21. It's a tie! <a href='/bet'>Play again?</a>"
elsif calculate_total(player_cards) == 21
@show_hit_or_stand_buttons = false
@winner = "You have blackjack! You win! <a href='/bet'>Play again?</a>"
session[:bank_roll] = session[:bank_roll] + (session[:bet_amount].to_i * 1.50)
elsif calculate_total(dealer_cards) == 21
@show_hit_or_stand_buttons = false
@loser = "Dealer has blackjack. You lose! <a href='/bet'>Play again?</a>"
session[:bank_roll] -= session[:bet_amount].to_i
else
@info = "Would you like to Hit or Stand, #{session[:player_name]}?"
end
end
def check_player_hand(player_cards)
if calculate_total(player_cards) > 21
@show_hit_or_stand_buttons = false
@loser = "Oops! You busted! <a href='/bet'>Play again?</a>"
session[:bank_roll] -= session[:bet_amount].to_i
erb :game, layout: false
elsif calculate_total(player_cards) == 21
@show_hit_or_stand_buttons = false
@tie = "You have 21. Dealer's turn to draw."
redirect "/game/dealer"
halt erb(:game)
else
@info = "Would you like to Hit or Stand, #{session[:player_name]}?"
end
end
def check_dealer_hand(dealer_cards)
if calculate_total(dealer_cards) > 21
@winner = "Dealer busted. You win! <a href='/bet'>Play again?</a>"
session[:bank_roll] += session[:bet_amount].to_i
elsif calculate_total(dealer_cards) >= 17
redirect "/game/compare"
else
redirect "/game/dealer"
end
end
def final_hand_check(dealer_cards, player_cards)
if calculate_total(dealer_cards) > calculate_total(player_cards)
@loser = "The dealer has #{calculate_total(dealer_cards)}. You have #{calculate_total(player_cards)}. You lose. <a href='/bet'>Play again?</a>"
session[:bank_roll] -= session[:bet_amount].to_i
elsif calculate_total(dealer_cards) < calculate_total(player_cards)
@winner = "The dealer has #{calculate_total(dealer_cards)}. You have #{calculate_total(player_cards)}. You win! <a href='/bet'>Play again?</a>"
session[:bank_roll] += session[:bet_amount].to_i
else
@tie = "The dealer has #{calculate_total(dealer_cards)}. You have #{calculate_total(player_cards)}. It's a tie. <a href='/bet'>Play again?</a>"
end
end
def bank_check(amount)
if amount == 0
@error = "You are out of money <a href='/new_player'>Play again?</a>"
end
end
end
before do
@show_hit_or_stand_buttons = true
end
get '/' do
if session[:player_name]
redirect "/game"
else
redirect "/new_player"
end
end
get "/new_player" do
session.clear
erb :new_player
end
post '/set_name' do
check_name(params[:player_name])
end
get '/bet' do
@show_bet_form = true
if session[:bet_amount] != nil
session[:bank_roll]
if session[:bank_roll] == 0
@error = "You are out of money <a href='/new_player'>Start Over?</a>"
@show_bet_form = false
end
else
session[:bank_roll] = 500
end
erb :bet
end
post '/set_bet' do
@show_bet_form = true
check_bet(params[:bet_amount])
erb :bet
end
get '/game' do
#create a deck
suits = ['H', 'S', 'C', 'D']
values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
session[:deck] = suits.product(values).shuffle!
# prettify cards
session[:player_cards] = []
session[:dealer_cards] = []
session[:player_cards] << session[:deck].pop
session[:dealer_cards] << session[:deck].pop
session[:player_cards] << session[:deck].pop
session[:dealer_cards] << session[:deck].pop
# check of initial deal
initial_hand_check(session[:dealer_cards],session[:player_cards])
erb :game
end
post "/game/player/hit" do
session[:player_cards] << session[:deck].pop
check_player_hand(session[:player_cards])
erb :game, layout: false
end
post "/game/player/stand" do
redirect "/game/dealer"
end
get "/game/dealer" do
@info = "You are standing with #{calculate_total(session[:player_cards])}."
@show_hit_or_stand_buttons = false
@show_dealers_cards = true
if calculate_total(session[:dealer_cards]) <= 16
@show_dealer_button = true
else
redirect "/game/compare"
end
erb :game, layout: false
end
post "/game/dealer/hit" do
@show_dealers_cards = true
@show_hit_or_stand_buttons = false
session[:dealer_cards] << session[:deck].pop
@dealer_draw = true
check_dealer_hand(session[:dealer_cards])
erb :game, layout: false
end
get "/game/compare" do
@show_hit_or_stand_buttons = false
@show_dealers_cards = true
if session[:dealer_cards].size > 2
@dealer_draw = true
end
final_hand_check(session[:dealer_cards], session[:player_cards])
erb :game, layout: false
end