-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
237 lines (210 loc) · 5.33 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
require 'rubygems'
require 'sinatra'
require 'pry'
set :sessions, true
helpers do
def hit
session[:play_deck].pop
end
def init_game
deck =
['KH', 'QH', 'JH', 'AH', '2H', '3H','4H',
'5H', '6H', '7H', '8H', '9H', '10H',
'KC', 'QC', 'JC', 'AC', '2C', '3C','4C',
'5C', '6C', '7C', '8C', '9C', '10C',
'KD', 'QD', 'JD', 'AD', '2D', '3D','4D',
'5D', '6D', '7D', '8D', '9D', '10D',
'KS', 'QS', 'JS', 'AS', '2S', '3S','4S',
'5S', '6C', '7S', '8S', '9S', '10S']
session[:play_deck] = []
deck.each {|x| session[:play_deck] << x} # Initialize playing deck
session[:play_deck].shuffle!
end
def init_deal
session[:player_turn] = true
session[:dealer_total] = 0
session[:player_total] = 0
session[:dealer_hand] = []
session[:player_hand] = []
2.times do
session[:player_hand] << hit
session[:dealer_hand] << hit
end
end
def calc(hand)
total = 0
ace = hand.select {|x| x[0] == 'A'}
# Calculation of hand without aces.
if ace == []
hand.each do |x|
x = x[0..-2].to_i
total += x == 0 ? 10 : x
end
total
else
# Calculation of hand with ace(s)
# Step 1. Calculate cards without the aces
hand.reject{|x| ace.include?(x)}.each do |x|
x = x[0..-2].to_i
total += x == 0 ? 10 : x
end
# Step 2. Add the value of ace(s) to the current total
nr_ace = ace.length
case nr_ace
when 1 then total += total <= 10 ? 11 : 1
when 2 then total += total <= 9 ? 12 : 2
when 3 then total += total <= 8 ? 13 : 3
else total += total <= 7 ? 14 : 4
end
total
end
end
def refresh
session[:player_turn] = true
session[:gameover] = false
session[:player_hand] = []
session[:dealer_hand] = []
session[:dealer_total] = 0
session[:player_total] = 0
session[:play_deck] = []
session[:wallet] = 500
session[:bet] = 0
end
def conv_to_img(card)
suits = {H: 'hearts', C: 'clubs', D: 'diamonds', S: 'spades'}
royals = {K: 'king', Q: 'queen', J: 'jack', A: 'ace'}
card_val = card[0..-2]
card_suit = card[-1]
card_suit = suits[card_suit.to_sym]
if card_val.to_i == 0
royal = royals[card_val.to_sym]
"<img src='/images/cards/#{card_suit}_#{royal}.jpg' class='thumbnail' style='width: 160px; height 234px'/>"
else
"<img src='/images/cards/#{card_suit}_#{card_val}.jpg' class='thumbnail' style='width: 160px; height 234px'/>"
end
end
end
before do
end
get '/' do
if session[:username]
session[:wallet] = 500
erb :index
else
redirect '/new_player'
end
end
get '/new_player' do
erb :new_player
end
post '/new_player' do
if params[:username].empty?
@error = "Please enter your name."
halt erb :new_player
else
session[:wallet] = 500
session[:username] = params[:username]
redirect '/bet'
end
end
get '/bet' do
if session[:wallet] == 0
erb :replay
else
erb :bet
end
end
post '/bet' do
if params[:bet].to_i > session[:wallet]
@error = "You do not have enough chips. Try a different amount"
halt erb :bet
elsif params[:bet].empty? or params[:bet].to_i == 0
@error = "Please bet an amount greater than 0."
halt erb :bet
else
session[:bet] = params[:bet].to_i
session[:gameover] = false
redirect '/game'
end
end
get '/game' do
if session[:play_deck] == nil or session[:play_deck] == []
init_game
init_deal
else
init_deal
end
session[:dealer_total] = calc(session[:dealer_hand])
session[:player_total] = calc(session[:player_hand])
redirect '/game/player'
end
get '/game/player' do
if session[:player_total] == 21
@success = "Blackjack! Congratulations, you win!"
session[:wallet] += session[:bet]
session[:gameover] = true
erb :game
elsif session[:player_total] > 21
@error = "You bust! Dealer wins!"
session[:wallet] -= session[:bet]
session[:gameover] = true
erb :game
else
erb :game
end
end
post '/game/hit' do
if session[:player_turn]
session[:player_hand] << hit
session[:player_total] = calc(session[:player_hand])
redirect '/game/player'
else
if session[:dealer_total] < 17
session[:dealer_hand] << hit
session[:dealer_total] = calc(session[:dealer_hand])
redirect '/game/dealer'
else
redirect '/game/compare'
end
end
end
post '/game/player/stay' do
session[:player_turn] = false
redirect '/game/dealer'
end
get '/game/dealer' do
if session[:dealer_total] == 21
@error = "Blackjack! Dealer wins!"
session[:wallet] -= session[:bet]
session[:gameover] = true
erb :game
elsif session[:dealer_total] > 21
@success = "Dealer bust! You win!"
session[:wallet] += session[:bet]
session[:gameover] = true
erb :game
else
erb :game
end
end
get '/game/compare' do
if session[:dealer_total] >= session[:player_total]
@error = "Dealer wins with a hand of #{session[:dealer_total]}"
session[:wallet] -= session[:bet]
session[:gameover] = true
erb :game
else
@success = "Congratulations! You win with a total of #{session[:player_total]}"
session[:wallet] += session[:bet]
session[:gameover] = true
erb :game
end
end
post '/replay' do
refresh
redirect '/'
end
post '/end' do
refresh
erb :end
end