-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
225 lines (190 loc) · 4.69 KB
/
game.rb
File metadata and controls
225 lines (190 loc) · 4.69 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# frozen_string_literal: true
require './deck'
require './player'
require './dealer'
class Game
DEALER_NAME = %w[Dima Roma Petr Andrey].freeze
BLACKJACK = 21
MAX_CARDS = 3
attr_reader :player, :dealer, :deck
def initialize
@deck = Deck.new
@dealer = Dealer.new(DEALER_NAME.sample)
create_player
end
def start
loop
main_menu
action = gets.to_i
case action
when 1
shuffle_deck
loop do
start_round
break unless continue?
end
puts 'You left the game'
when 2
print '.......'
when 3
puts 'You left the game'
exit
end
end
def start_round
deal_two_card
bet = players_bet
show_balance_player
show_cards_players
skip_round = false
loop do
break if max_cards_dealer? && max_cards_player?
menu_action_user
action = gets.to_i
begin
case action
when 1
deal_card_player
puts "You took the card #{player.cards.last}"
show_card_player
puts 'Move passed to the dealer'
dealer_move
skip_round = true
when 2
raise ArgumentError, 'Have already skipped a turn or drawn a card' if skip_round == true
puts 'You missed a move'
skip_round = true
puts 'Move passed to the dealer'
dealer_move
when 3
skip_round = false
break
end
rescue ArgumentError => e
show_exception(e)
end
end
winner_gets_money(bet)
show_winner
end
private
def players_bet
if player.deposit.deposit.zero? || dealer.deposit.deposit.zero?
raise ArgumentError,
'One of the players has no balance'
end
player.bet + dealer.bet
end
def balance_refund(bet_amount)
player.get_money(bet_amount / 2)
dealer.get_money(bet_amount / 2)
end
def draw?
player.sum_points == dealer.sum_points && player.sum_points <= BLACKJACK
end
def winner_gets_money(bet_amount)
if player_win?
player.get_money(bet_amount)
elsif dealer_win?
dealer.get_money(bet_amount)
elsif draw?
balance_refund(bet_amount)
end
end
def show_balance_player
puts "Your balance: #{player.deposit.deposit}"
end
def dealer_move
puts "Dealer #{dealer.name} thinks"
sleep 2
if dealer.sum_points < 17
deal_card_dealer
puts "Dealer #{dealer.name} took the card"
else
puts "Dealer #{dealer.name} skips a turn"
end
end
def max_cards_player?
player.cards.count == MAX_CARDS
end
def max_cards_dealer?
dealer.cards.count == MAX_CARDS
end
def player_win?
player.sum_points == BLACKJACK || dealer.sum_points < player.sum_points && player.sum_points < BLACKJACK || dealer.sum_points > BLACKJACK
end
def dealer_win?
dealer.sum_points == BLACKJACK || dealer.sum_points > player.sum_points && dealer.sum_points < BLACKJACK || player.sum_points > BLACKJACK
end
def show_winner
puts "Player #{player.name} Balance: #{player.deposit.deposit} - Win!" if player_win?
puts "Dealer #{dealer.name} - Win!" if dealer_win?
puts 'The dealer and the player have the same amount of money - a Draw!' if draw?
puts "Player cards: #{player.show_cards}, Dealer cards: #{dealer.show_cards}"
show_player_points
show_dealer_points
drop_cards_players
end
def show_cards_players
puts "Player cards: #{player.show_cards}, Dealer cards: #{dealer.show_cards_hidden}"
show_player_points
end
def show_card_player
puts "Player cards: #{player.show_cards}"
show_player_points
end
def show_player_points
puts "You points: #{player.sum_points}"
end
def show_dealer_points
puts "Dealer points: #{dealer.sum_points}"
end
def continue?
puts 'Play again 1.Yes 2.No'
gets.chomp.to_i == 1
end
def deal_two_card
2.times { deal_card_dealer }
2.times { deal_card_player }
end
def create_player
print 'What is your name: '
name = gets.strip
@player = Player.new(name)
rescue ArgumentError => e
show_exception(e)
retry
end
def drop_cards_players
dealer.drop_card
player.drop_card
end
def deal_card_player
dealer.deal_card!(deck, player)
end
def deal_card_dealer
dealer.deal_card!(deck)
end
def shuffle_deck
dealer.shuffle!(deck)
end
def main_menu
print <<~MENU
Enter the action number:
1 - Start the game
2 - View BlackJack game rules
3 - Exit the game
MENU
end
def menu_action_user
print <<~MENU
Enter the action number:
1 - Take card
2 - Skip a turn
3 - Open cards
MENU
end
def show_exception(exception)
puts " #{exception.message} "
end
end