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
/
Copy pathhelpers.rb
116 lines (92 loc) · 2.62 KB
/
helpers.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
helpers do
def set_playername(playername, remember_name)
if params[:playername].empty?
session[:playername] = "Player"
else
session[:playername] = params[:playername]
end
if remember_name == 'on' || remember_name == '1' || remember_name == 'true'
session[:playername_persistent] = true
end
end
def build_deck
cards = []
amount_decks = AMOUNT_DECKS
# For each amount_decks, create the deck using an array of suits and an array of values
amount_decks.times do
['hearts','diamonds','spades','clubs'].each do |suit|
['ace','2','3','4','5','6','7','8','9','10','jack','queen','king'].each do |value|
cards << {suit: suit, value: value}
end
end
end
cards.shuffle!
session[:deck] = cards
end
def setup
session[:player_cards] = []
session[:dealer_cards] = []
2.times { session[:player_cards] << session[:deck].pop }
2.times { session[:dealer_cards] << session[:deck].pop }
session[:dealer_cards].last[:hidden] = true
end
def has_hidden_card(cards)
cards.select {|card| card[:hidden] }.size > 0
end
def cards_value(cards)
total_points = 0
cards.each do |card|
if card[:value].to_i != 0
point = card[:value].to_i
else
point = case card[:value]
when 'ace'; 11
when 'jack', 'queen', 'king'; 10
end
end
total_points += point
end
# Take into account any aces if the total value is higher then 21
cards.select{ |card| card[:value] == 'ace' }.count.times do
total_points -= 10 if total_points > BLACKJACK_AMOUNT
end
total_points
end
def show_all_cards(cards)
cards.each {|card| card[:hidden] = false}
end
def card_filename(card)
base_path = '/images/cards'
if card[:hidden]
return "#{base_path}/cover.jpg"
else
return "#{base_path}/#{card[:suit]}_#{card[:value]}.jpg"
end
end
def card_title(card)
if card[:hidden]
return 'Nobody knows'
else
card_value = card[:value].to_i == 0 ? card[:value] : CARD_VALUE_TO_WORD[card[:value].to_i]
return "#{card_value.capitalize} of #{card[:suit].capitalize}"
end
end
def remove_game
reset_game
session[:player_money] = nil
end
def reset_game
reset_round
session[:state] = nil
session[:player_money] = INITIAL_PLAYER_MONEY
session[:player_bet] = nil
end
def reset_round
session[:player_cards] = []
session[:dealer_cards] = []
session[:deck] = []
end
def play_again
" <a href='/make_bet' class='float-right'>Play again</a>"
end
end