-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwenty_one.rb
149 lines (125 loc) · 2.7 KB
/
twenty_one.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
# frozen_string_literal: true
require 'sinatra'
require 'sinatra/reloader' if development?
require 'tilt/erubis'
require_relative 'lib/game.rb'
configure do
enable :sessions
set :session_secret, development? ? 'secret' : SecureRandom.hex(64)
end
helpers do
def each_dealer_card_image_path
@dealer.hand.each_with_index do |card, idx|
path = if !end_of_game?
"/images/#{idx.zero? ? 'cards/' + card.to_s : 'back'}.png"
else
"/images/cards/#{card}.png"
end
yield path
end
end
def each_player_card_image_path
@player.hand.each do |card|
yield "/images/cards/#{card}.png"
end
end
def end_of_game?
@game.game_over
end
end
before do
pass if request.path_info == '/'
@game = session[:game]
@dealer = @game.dealer
@player = @game.player
@player_score = @player.score
@dealer_score = @dealer.score
@player_hand_value = @player.total(Game::MAX_LIMIT)
@dealer_hand_value = @dealer.total(Game::MAX_LIMIT)
end
def initialize_game
session[:game] = Game.new
end
def handle_bust
if @game.player_bust?
flash('Oops! You busted! Dealer wins!', :danger)
@dealer.increment_score
elsif @game.dealer_bust?
flash('Woo! The dealer busted. You win!', :success)
@player.increment_score
end
end
def handle_winner
case @game.winner
when :player
flash("You won! Show that dealer who's boss!", :success)
@player.increment_score
when :dealer
flash('The dealer won this round. But I believe in you!', :danger)
@dealer.increment_score
when :neither
flash('It was a tie. Better luck next time!')
end
end
def handle_end_of_round
if @game.player_bust? || @game.dealer_bust?
handle_bust
else
handle_winner
end
@game.game_over = true
end
def hit
if end_of_game?
flash("It's the end of the round. You can't hit anymore!", :danger)
else
@game.hit(@player)
handle_end_of_round if @game.player_bust?
end
end
def stay
if end_of_game?
flash("It's the end of the round. Move on to the next round!", :danger)
else
@game.dealer_turn
handle_end_of_round
end
end
def next_round
@game.reset_game
end
def reset
flash('Game has been reset!', :success)
@game.total_reset
end
def flash(message, type = :neutral)
session[:flash] = { message: message, type: type }
end
get '/' do
flash('Hi there!', :success)
initialize_game
erb :index
end
get '/play' do
erb :play
end
post '/hit' do
hit
redirect '/play'
end
post '/stay' do
stay
redirect '/play'
end
post '/next_round' do
next_round
redirect '/play'
end
get '/reset' do
flash('You cannot undo this action!', :danger)
erb :reset
end
post '/reset' do
reset
redirect '/play'
end