-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
184 lines (146 loc) · 4.11 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
require 'rubygems'
require 'sinatra'
set :sessions, true
BLACKJACK_AMOUNT = 21
DEALER_MIN_HIT = 17
helpers do
def calculate_total(cards) # cards is [["H", "3"], ["D", "J"], ... ]
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
#correct for Aces
arr.select{|element| element == "A"}.count.times do
break if total <= BLACKJACK_AMOUNT
total -= 10
end
total
end
def card_image(card) # ['H', '4']
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
@success = "<strong>#{session[:player_name]} wins!</strong> #{msg}"
end
def loser!(msg)
@play_again = true
@show_hit_or_stay_buttons = false
@error = "<strong>#{session[:player_name]} loses.</strong> #{msg}"
end
def tie!(msg)
@play_again = true
@show_hit_or_stay_buttons = false
@success = "<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
erb :new_player
end
post '/new_player' do
if params[:player_name].empty?
@error = "Name is required"
halt erb(:new_player)
end
session[:player_name] = params[:player_name]
redirect '/game'
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! # [ ['H', '9'], ['C', 'K'] ... ]
# 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!("#{session[:player_name]} hit blackjack.")
elsif player_total > BLACKJACK_AMOUNT
loser!("It looks like #{session[:player_name]} busted at #{player_total}.")
end
erb :game
end
post '/game/player/stay' do
@success = "#{session[:player_name]} has chosen 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
# decision tree
dealer_total = calculate_total(session[:dealer_cards])
if dealer_total == BLACKJACK_AMOUNT
loser!("Dealer hit blackjack.")
elsif dealer_total > BLACKJACK_AMOUNT
winner!("Dealer busted at #{dealer_total}.")
elsif dealer_total >= DEALER_MIN_HIT #17, 18, 19, 20
# dealer stays
redirect '/game/compare'
else
# dealer hits
@show_dealer_hit_button = true
end
erb :game
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 #{session[:player_name]} and the dealer stayed at #{player_total}.")
end
erb :game
end
get '/game_over' do
erb :game_over
end