-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
207 lines (166 loc) · 4.63 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
require 'rubygems'
require 'sinatra'
require 'pry'
set :sessions, true
helpers do
def calculate_score(hand)
values = hand.map{ |card| card[0]}
score = 0
values.each do |value|
if value == 'Ace'
score += 11
else
score += (value.to_i == 0 ? 10 : value.to_i)
end
end
# Correct for Aces
values.select{|val| val == 'Ace'}.count.times do
break if score <= 21
score -= 10
end
score
end
def is_busted?(hand)
calculate_score(hand) > 21
end
def is_blackjack?(hand)
calculate_score(hand) == 21
end
def end_game_variables
@player_turn = false
@dealer_turn = false
@game_over = true
end
def check_player_score
if is_busted?(session[:player_cards])
@msg_fail = "Sorry #{session[:player_name]}, you busted. You lose :("
session[:player_purse] -= session[:bet]
end_game_variables
elsif is_blackjack?(session[:player_cards])
@msg_success = "Congrats #{session[:player_name]}, you got blackjack! You win :)"
session[:player_purse] += session[:bet]
end_game_variables
end
end
def check_dealer_score
if is_busted?(session[:dealer_cards]) # score > 21
@msg_success = "Dealer busted! You win :)"
session[:player_purse] += session[:bet]
end_game_variables
elsif is_blackjack?(session[:dealer_cards]) # score == 21
@msg_fail = 'Dealer hit blackjack. You lose :('
session[:player_purse] -= session[:bet]
end_game_variables
elsif calculate_score(session[:dealer_cards]) < 17
@dealer_turn = true
else # 17 <= score < 21
compare_scores(session[:dealer_cards], session[:player_cards])
end
end
def compare_scores(dealer_hand, player_hand)
end_game_variables
dealer_score = calculate_score(dealer_hand)
player_score = calculate_score(player_hand)
if dealer_score > player_score
@msg_fail = "Sorry, you lose. Dealer's total of #{dealer_score} beat your score of #{player_score}."
session[:player_purse] -= session[:bet]
elsif dealer_score < player_score
@msg_success = "Congrats, you win! Your total of #{player_score} beat the dealer's score of #{dealer_score}."
session[:player_purse] += session[:bet]
else
@msg_success = "It's a tie."
end
end
def card_image(card)
if card == 'cover'
img_name = "<img src='/images/cards/cover.jpg' />"
else
img_name = "<img src='/images/cards/#{card[1].downcase}_#{card[0].downcase}.jpg' />"
end
img_name
end
end
before do
@player_turn = true
@dealer_turn = false
@game_over = false
end
get '/' do
redirect '/new_player'
end
get '/new_player' do
erb :new_player
end
post '/new_player' do
if params[:player_name].empty?
@error = 'Must enter a name'
halt erb :new_player
end
session[:player_name] = params[:player_name]
#give player $
session[:player_purse] = 500
redirect '/blackjack/place_bet'
end
get '/home' do
erb :home
end
get '/done' do
erb :done
end
get '/blackjack/place_bet' do
if session[:player_purse] < 1
redirect '/you_lose'
else
erb :place_bet
end
end
post '/blackjack/place_bet' do
if params[:bet].to_i > session[:player_purse] || params[:bet].to_i <= 0
@error = "Must enter a valid amount, between $1 and $#{session[:player_purse]}"
halt erb :place_bet
end
session[:bet] = params[:bet].to_i
redirect '/blackjack/new'
end
get '/blackjack/new' do
#setup deck
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
suits = ['Spades', 'Diamonds', 'Hearts', 'Clubs']
session[:deck] = values.product(suits).shuffle!
#setup hands
session[:player_cards] = []
session[:dealer_cards] = []
#deal cards
2.times do
session[:player_cards] << session[:deck].pop
session[:dealer_cards] << session[:deck].pop
end
redirect '/blackjack' # Keeps user from getting new cards at a screen refresh
end
get '/blackjack' do
check_player_score
erb :blackjack
end
post '/blackjack/player_hit' do
session[:player_cards] << session[:deck].pop
check_player_score
erb :blackjack, layout: false
end
post '/blackjack/player_stay' do
@player_turn = false
check_dealer_score # if dealer's score is between 17 and 21, will also compare scores
erb :blackjack, layout: false
end
post '/blackjack/dealer_hit' do
session[:dealer_cards] << session[:deck].pop
@player_turn = false
check_dealer_score # if dealer's score is between 17 and 21, will also compare scores
erb :blackjack, layout: false
end
get '/you_lose' do
erb :you_lose
end
post '/refill_purse' do
session[:player_purse] = 500
redirect '/blackjack/place_bet'
end