-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
204 lines (164 loc) · 4.69 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
require 'rubygems'
require 'sinatra'
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'secret'
BLACKJACK_AMOUNT = 21
DEALER_MIN_HIT = 17
INITIAL_POT_AMOUNT = 5000
helpers do
def calculate_total(cards)
arr = cards.map{|element| element[1]}
total = 0
arr.each do |a|
if a == "A"
total += 11
else
total += a.to_i == 0 ? 10 : a.to_i
end
end
arr.select{|element| element == "A"}.count.times do
break if total <= BLACKJACK_AMOUNT
total -= 10
end
total
end
def card_image(card) #['C', '2']
suit = case card[0]
when 'H' then 'hearts'
when 'D' then 'diamonds'
when 'C' then 'clubs'
when 'S' then 'spades'
end
value = card[1]
if ['J', 'Q', 'K', 'A'].include?(value)
value = case card[1]
when 'J' then 'jack'
when 'Q' then 'queen'
when 'K' then 'king'
when 'A' then 'ace'
end
end
"<img src='/images/cards/#{suit}_#{value}.jpg' class='card_image'>"
end
def winner!(msg)
@play_again = true
@show_hit_or_stay_buttons = false
session[:player_pot] = session[:player_pot] + session[:player_bet]
@winner = "<strong>#{session[:player_name]} wins!</strong> #{msg}"
end
def loser!(msg)
@play_again = true
@show_hit_or_stay_buttons = false
session[:player_pot] = session[:player_pot] - session[:player_bet]
@loser = "<strong>#{session[:player_name]} loses</strong> #{msg}"
end
def tie!(msg)
@play_again = true
@show_hit_or_stay_buttons = false
@winner = "#{msg}"
end
end
before do
@show_hit_or_stay_buttons = true
end
get '/' do
if session[:player_name]
redirect '/game'
else
redirect '/new_player'
end
end
get '/new_player' do
session[:player_pot] = INITIAL_POT_AMOUNT
erb :new_player
end
post '/new_player' do
if params[:player_name].empty?
@error = "Please enter your name"
halt erb(:new_player)
end
session[:player_name] = params[:player_name]
redirect '/bet'
end
get '/bet' do
session[:player_bet] = nil
erb :bet
end
post '/bet' do
if params[:bet_amount].nil? || params[:bet_amount].to_i == 0
@error = "Must make a bet"
halt erb(:bet)
elsif params[:bet_amount].to_i > session[:player_pot]
@error = "Bet amount cannot be greater than $#{session[:player_pot]}"
halt erb(:bet)
else
session[:player_bet] = params[:bet_amount].to_i
redirect '/game'
end
end
get '/game' do
session[:turn] = session[:player_name]
#create a deck and put it in session
suits = ['H', 'D', 'C', 'S']
values = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
session[:deck] = suits.product(values).shuffle!
#deal cards
session[:dealer_cards] = []
session[:player_cards] = []
session[:dealer_cards] << session[:deck].pop
session[:player_cards] << session[:deck].pop
session[:dealer_cards] << session[:deck].pop
session[:player_cards] << session[:deck].pop
erb :game
end
post '/game/player/hit' do
session[:player_cards] << session[:deck].pop
player_total = calculate_total(session[:player_cards])
if player_total == BLACKJACK_AMOUNT
winner!("BLACKJACK!!")
elsif player_total > BLACKJACK_AMOUNT
loser!("Sorry, looks like #{session[:player_name]} busted at #{player_total}")
end
erb :game, layout: false
end
post '/game/player/stay' do
@success = "#{session[:player_name]} has elected to stay"
@show_hit_or_stay_buttons = false
redirect '/game/dealer'
end
get '/game/dealer' do
session[:turn] = "dealer"
@show_hit_or_stay_buttons = false
dealer_total = calculate_total(session[:dealer_cards])
if dealer_total == BLACKJACK_AMOUNT
loser!("Sorry #{session[:player_name]}, the dealer hit BLACKJACK")
elsif dealer_total > BLACKJACK_AMOUNT
winner!("Dealer busted at #{dealer_total}, #{session[:player_name]} WINS!!")
elsif dealer_total >= DEALER_MIN_HIT
redirect '/game/compare'
else
@show_dealer_hit_button = true
end
erb :game, layout: false
end
post '/game/dealer/hit' do
session[:dealer_cards] << session[:deck].pop
redirect '/game/dealer'
end
get '/game/compare' do
@show_hit_or_stay_buttons = false
player_total = calculate_total(session[:player_cards])
dealer_total = calculate_total(session[:dealer_cards])
if player_total < dealer_total
loser!("#{session[:player_name]} stayed at #{player_total} and the dealer stayed at #{dealer_total}")
elsif player_total > dealer_total
winner!("#{session[:player_name]} stayed at #{player_total} and the dealer stayed at #{dealer_total}")
else
tie!("Both players at at: #{player_total}. It's a push")
end
erb :game, layout: false
end
get '/game_over' do
erb :game_over
end