-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
executable file
·191 lines (166 loc) · 4.79 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
require 'rubygems'
require 'sinatra'
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'your_secret'
BLACKJACK = 21
DEALER_MIN = 17
INITIAL_POT = 500
helpers do
def calculate_total(card_values)
hand = card_values.map {|card| card[1]}
hand_total = 0
hand.each do |value|
if value == "A"
hand_total += 11
elsif value.to_i == 0
hand_total += 10
else
hand_total += value.to_i
end
end
hand.select{|value| value == "A"}.count.times do
hand_total -= 10 if hand_total > BLACKJACK
end
hand_total
end
def card_image(card)
suit = case card[0]
when 'Heart' then 'hearts'
when 'Diamond' then 'diamonds'
when 'Club' then 'clubs'
when 'Spade' 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 = "<strong>It's a tie!</strong> #{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
erb :new_player
end
post '/new_player' do
if params[:player_name].empty?
@error = "Player name is required."
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 = "You must place a bet."
halt erb(:bet)
elsif params[:bet_amount].to_i > session[:player_pot]
@error = "You bet is greater than the amount you have. ($#{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]
suits = ['Heart', 'Club', 'Diamond', 'Spade']
card_values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
session[:deck] = suits.product(card_values).shuffle!
session[:player_hand] = []
session[:dealer_hand] = []
session[:player_hand] << session[:deck].pop
session[:dealer_hand] << session[:deck].pop
session[:player_hand] << session[:deck].pop
session[:dealer_hand] << session[:deck].pop
player_total = calculate_total(session[:player_hand])
if player_total == BLACKJACK
winner!("#{session[:player_name]} hit blackjack.")
end
erb :game
end
post '/game/player/hit' do
session[:player_hand] << session[:deck].pop
player_total = calculate_total(session[:player_hand])
if player_total == BLACKJACK
winner!("#{session[:player_name]} hit blackjack.")
elsif player_total > BLACKJACK
loser!("#{session[:player_name]} busted at #{player_total}.")
end
erb :game, layout: false
end
post '/game/player/stay' do
@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_hand])
if dealer_total == BLACKJACK
loser!("Dealer hit blackjack.")
elsif dealer_total > BLACKJACK
winner!("Dealer busted at #{dealer_total}.")
elsif dealer_total >= DEALER_MIN
redirect '/game/compare_hands'
else
@show_dealer_hit_button = true
end
erb :game, layout: false
end
post '/game/dealer/hit' do
session[:dealer_hand] << session[:deck].pop
redirect '/game/dealer'
end
get '/game/compare_hands' do
@show_hit_or_stay_buttons = false
player_total = calculate_total(session[:player_hand])
dealer_total = calculate_total(session[:dealer_hand])
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 #{session[:player_name]} and the dealer stayed at #{player_total}.")
end
erb :game, layout: false
end
get '/game_over' do
erb :game_over
end