-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
206 lines (166 loc) · 4.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
require 'rubygems'
require 'sinatra'
require 'pry'
set :sessions, true
BLACKJACK_AMOUNT = 21
DEALER_MIN_HIT = 17
INITIAL_BET_AMOUNT = 500
helpers do
def get_total(hand)
total = 0
arr = hand.map{|element| element[1]}
arr.each do |value|
if value == 'ace'
total < 11 ? total += 11 : total += 1
else
total += (value.to_i == 0 ? 10 : value)
end
end
total
end
def deal_card(hand, deck)
hand << deck.shift
end
def initial_deal(hand, deck)
2.times do
deal_card(hand, deck)
end
end
def card_image(card)
"<img src='/images/cards/#{card[0]}_#{card[1]}.svg' class='card'>"
end
def hide_play_again
if session[:player_money] == 0
return true
end
end
def winner!(msg)
@show_buttons = false
@reveal = true
@message = "#{session[:player_name]} wins! #{msg}"
session[:player_score] += 1
session[:player_money] += session[:bet_amount]
end
def loser!(msg)
@show_buttons = false
@reveal = true
@message = "#{session[:player_name]} loses. #{msg}"
session[:dealer_score] += 1
session[:player_money] -= session[:bet_amount]
hide_play_again
end
def tie!(msg)
@show_buttons = false
@reveal = true
@message = "Nobody wins - it's a draw. #{msg}"
hide_play_again
end
end
before do
@show_buttons = true
end
get '/' do
if session[:player_name]
redirect '/game'
else
redirect '/get_name'
end
end
get '/get_name' do
erb :get_name
end
post '/get_name' do
if params[:player_name].empty?
@problem = true
halt erb :get_name
end
@legal_chars = []
"a".upto("z") {|c| @legal_chars << c}
params[:player_name].each_char do |c|
if !@legal_chars.include? c.downcase
@problem = true
halt erb :get_name
end
end
session[:player_name] = params[:player_name].capitalize
session[:player_score] = 0
session[:dealer_score] = 0
session[:player_money] = INITIAL_BET_AMOUNT
redirect '/bet'
end
get '/bet' do
session[:bet_amount] = nil
erb :bet_amount
end
post '/bet' do
if params[:bet_amount].empty? || params[:bet_amount].to_i == 0
@problem = true
halt erb :bet_amount
elsif params[:bet_amount].to_i > session[:player_money]
@problem = true
halt erb :bet_amount
else
session[:bet_amount] = params[:bet_amount].to_i
redirect '/game'
end
end
get '/game' do
@suits = ['hearts', 'clubs', 'diamonds', 'spades']
@cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace']
@player_turn = 'initial'
session[:deck] = @suits.product(@cards).shuffle!
session[:player_hand] = []
session[:dealer_hand] = []
initial_deal(session[:player_hand], session[:deck])
initial_deal(session[:dealer_hand], session[:deck])
player_total = get_total(session[:player_hand])
dealer_total = get_total(session[:dealer_hand])
if player_total == BLACKJACK_AMOUNT
winner!("You've got Blackjack!")
elsif dealer_total == BLACKJACK_AMOUNT
loser!("Sorry, but the dealer wins with Blackjack.")
elsif dealer_total == BLACKJACK_AMOUNT && player_total == BLACKJACK_AMOUNT
tie!("The dealer also had #{dealer_total}.")
end
erb :game
end
post '/game/player/hit' do
deal_card(session[:player_hand], session[:deck])
player_total = get_total(session[:player_hand])
@player_turn = 'hit'
if player_total == BLACKJACK_AMOUNT
winner!("You've got Blackjack!")
elsif player_total > BLACKJACK_AMOUNT
loser!("Sorry, looks like you busted.")
end
erb :game, layout: false
end
post '/game/player/stay' do
redirect '/game/dealer'
end
get '/game/dealer' do
@player_turn = 'stay'
player_total = get_total(session[:player_hand])
while get_total(session[:dealer_hand]) < DEALER_MIN_HIT
deal_card(session[:dealer_hand], session[:deck])
# @delay still reveals dealer's first card but sets a delay
# if not set reveal class automatically set in template
@delay = true
end
dealer_total = get_total(session[:dealer_hand])
if dealer_total == BLACKJACK_AMOUNT
loser!("Sorry, but the dealer wins with Blackjack.")
elsif dealer_total > BLACKJACK_AMOUNT
winner!("The dealer busted with #{dealer_total}!")
else
#compare scores
if dealer_total > player_total
loser!("Sorry, the dealer wins with #{dealer_total}.")
elsif player_total > dealer_total
winner!("The dealer loses with #{dealer_total}!")
elsif player_total == dealer_total
tie!("The dealer also had #{dealer_total}.")
end
end
erb :game
end