This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
157 lines (122 loc) · 4.16 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
require 'rubygems'
require 'sinatra'
require 'pry'
require 'json'
require './helpers'
set :sessions, true
set :session_secret, 'Codefish.org\'s secret'
CARD_VALUE_TO_WORD = { 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten" }
BLACKJACK_AMOUNT = 21
DEALER_HIT_AMOUNT = 17
INITIAL_PLAYER_MONEY = 100
AMOUNT_DECKS = 1
get '/' do
remove_game
erb :index
end
get '/player' do
erb :'player/info'
end
get '/start' do
reset_game
if session[:playername_persistent]
redirect '/make_bet'
end
erb :'preparation/playername'
end
post '/playername' do
if params[:playername].empty?
@alert_error = "Please enter a valid name. I'm sure you have one :-)"
halt erb :'preparation/playername'
end
set_playername(params[:playername], params[:remember_name])
redirect '/make_bet'
end
get '/make_bet' do
reset_round
if session[:player_money] && session[:player_money] > 0
erb :'preparation/make_bet'
else
erb :'game/game_over'
end
end
post '/make_bet' do
if params['bet_amount'].empty? || params['bet_amount'].to_i <= 0
@alert_error = "Illegal amount entered. Please enter a valid number."
erb :'preparation/make_bet'
elsif params['bet_amount'].to_i > session[:player_money]
@alert_error = "I'm sorry, you don't have this much money to bet. Enter a lower amount."
erb :'preparation/make_bet'
else
session[:player_bet] = params['bet_amount'].to_i
session[:player_money] -= session[:player_bet]
redirect '/game'
end
end
get '/game' do
if session[:player_cards].empty?
build_deck
setup
session[:state] = :player
end
erb :'game/display'
end
post '/ajax/game' do
# Check for blackjacks
if session[:dealer_cards].size == 2 && cards_value(session[:dealer_cards]) == BLACKJACK_AMOUNT
show_all_cards(session[:dealer_cards])
session[:state] = :player_lost
@alert_error = "Dealer has backjack, you've lost!!" + play_again
elsif session[:player_cards].size == 2 && cards_value(session[:player_cards]) == BLACKJACK_AMOUNT
session[:state] = :player_win
@alert_success = "You've blackjack, you win!!" + play_again
end
if params['action'].downcase == 'hit'
session[:player_cards] << session[:deck].pop
if cards_value(session[:player_cards]) > BLACKJACK_AMOUNT
session[:state] = :player_lost
@alert_error = "Game over! You've busted!" + play_again
end
elsif params['action'].downcase == 'stay'
session[:state] = :dealer
# session[:dealer_cards].each {|card| card[:hidden] = false}
show_all_cards(session[:dealer_cards])
elsif params['action'].downcase == 'dealer'
if cards_value(session[:dealer_cards]) < DEALER_HIT_AMOUNT
session[:dealer_cards] << session[:deck].pop
elsif cards_value(session[:player_cards]) > cards_value(session[:dealer_cards])
session[:state] = :player_win
@alert_success = "You win!" + play_again
elsif cards_value(session[:player_cards]) < cards_value(session[:dealer_cards])
session[:state] = :player_lost
@alert_error = "Too bad, dealer wins!" + play_again
else cards_value(session[:player_cards]) == cards_value(session[:dealer_cards])
session[:state] = :tie
@alert_info = "Game ends in a tie! " + play_again
end
if session[:state] == :dealer && cards_value(session[:dealer_cards]) > BLACKJACK_AMOUNT
session[:state] = :player_win
@alert_success = "Dealer busted! You win!" + play_again
end
end
if session[:player_bet] && [:player_win, :player_lost, :tie].include?(session[:state])
if session[:state] == :player_win
session[:player_money] += (2*session[:player_bet])
elsif session[:state] == :tie
session[:player_money] += session[:player_bet]
end
session[:player_bet] = nil
end
content_type :json
json_data = {:state => session[:state]}
json_data['error'] = @alert_error if @alert_error
json_data['info'] = @alert_info if @alert_info
json_data['success'] = @alert_success if @alert_success
json_data.to_json
end
get '/ajax/dealer_cards' do
erb :'ajax/dealer_cards', :layout => false
end
get '/ajax/player_cards' do
erb :'ajax/player_cards', :layout => false
end