-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
476 lines (357 loc) · 7.61 KB
/
game.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
module UX
PROMPT = ">> "
def clear_terminal
system("cls") || system("clear")
end
def concat_on_both_sides(original, left, right = nil)
right ||= left
left + original + right
end
def prompt(*messages)
messages.each { |msg| puts PROMPT + msg }
end
end
module UI
require 'io/console'
include UX
TERMINATION_CHARS = { "\u0003" => "^C",
"\u0004" => "^D",
"\u001A" => "^Z" }
def get_char(args)
get_input(**args) { yield_char }
end
def get_string(args)
get_input(**args) { gets.strip }
end
def wait_for_any_key
get_char(message: "Press ANY KEY to continue")
end
private
def fitting?(expected, input)
!expected || expected.include?(input)
end
def get_input(message:, invalid_msg: "Invalid input!", expected: nil)
prompt message
loop do
input = yield
return input if !input.empty? && fitting?(expected, input)
prompt invalid_msg
end
end
def quit_if_terminating(char_input)
termination_input = TERMINATION_CHARS[char_input]
abort("Program aborted (#{termination_input})") if termination_input
end
def yield_char
char_input = STDIN.getch.downcase
quit_if_terminating(char_input)
char_input
end
end
class Deck
RANKS = %i[j q k a] + (2..10).to_a
SUITS = %i[c d h s]
attr_reader :cards
def initialize
@cards = stanard_deck_cards
end
def deal
cards.shift
end
def shuffle
cards.shuffle!
end
private
attr_writer :cards
def stanard_deck_cards
RANKS.product(SUITS)
.map { |card| Card.new(*card) }
end
end
class Card
FACE_VALUES = { j: 10, q: 10, k: 10, a: 11 }
REDUCTIONS = { a: 10 }
SYMBOLS = { c: "♣", d: "♦", h: "♥", s: "♠" }
attr_reader :rank, :suit, :reduction
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def reduction
REDUCTIONS[rank]
end
def suit_symbol
SYMBOLS[suit]
end
def value
FACE_VALUES[rank] || rank
end
end
class Hand
include Comparable, UX
BUSTED_THRESHOLD = 21
CARD_WIDTH = 3
CARD_EDGES = { top: "_", side: "|", bottom: "‾" }
def initialize
@cards = []
end
def <<(card)
cards << card
end
def <=>(another)
[busted_factor, total] <=> [another.busted_factor, another.total]
end
def busted?
total > BUSTED_THRESHOLD
end
def size
cards.size
end
def to_s
all_rows.join("\n")
end
def total
curr_total = unreduced_total
possible_reductions.sort.each do |reduction|
curr_total -= reduction if curr_total > BUSTED_THRESHOLD
end
curr_total
end
protected
def busted_factor
busted? ? 0 : 1
end
private
attr_reader :cards
def all_rows
[top_edges, top_row, middle_row, bottom_row, bottom_edges]
end
def bottom_edges
edgize(:bottom)
end
def bottom_row
rowize(ranks.map { |rank| rank.to_s.upcase.ljust(CARD_WIDTH) })
end
def edgize(edge)
width = CARD_WIDTH
cards.map { |_| (CARD_EDGES[edge] * width).center(width + 2) }
.join
end
def middle_row
rowize(symbols.map { |symbol| symbol.center(CARD_WIDTH) })
end
def possible_reductions
cards.map(&:reduction).compact
end
def ranks
cards.map(&:rank)
end
def rowize(elements)
elements.map { |element| concat_on_both_sides(element, CARD_EDGES[:side]) }
.join
end
def symbols
cards.map(&:suit_symbol)
end
def top_edges
edgize(:top)
end
def top_row
rowize(ranks.map { |rank| rank.to_s.upcase.rjust(CARD_WIDTH) })
end
def unreduced_total
cards.map(&:value).reduce(:+)
end
end
class TwentyOneGame
include UX
def initialize(deck, *players)
@deck = deck
@players = players
end
def display(clear_screen: true)
clear_terminal if clear_screen
players_in_display_order.each(&:display_with_cards)
end
def hit(hand)
hand << deck.deal
end
def initial_deal
deck.shuffle
players.each { |player| player.initial_draw(self) }
end
def play
initial_deal
ask_for_playing(*players_in_move_order)
display_outcome
end
private
attr_reader :deck, :players
def ask_for_playing(*players)
players.each do |player|
player.play_turn(self)
break if last_man_standing?
end
end
def display_outcome
display
winner ? prompt("#{winner} wins!") : prompt("It's a tie!")
end
def last_man_standing?
players.reject(&:busted?).size == 1
end
def players_in_descending_order_by(criterium)
players.sort_by { |player| -player.send(criterium) }
end
def players_in_display_order
players_in_descending_order_by(:display_priority)
end
def players_in_move_order
players_in_descending_order_by(:move_sequence)
end
def runner_up
players.sort[-2]
end
def winner
best = players.max
best if best != runner_up
end
end
class Partaker
include Comparable
attr_reader :display_priority, :hand, :move_sequence, :name
DISPLAY_PRIORITY = 0
MOVE_SEQUENCE = 0
NAME = ""
def initialize
setup
new_hand
end
def <=>(another)
hand <=> another.hand
end
def busted?
hand.busted?
end
def display_with_cards
puts name + ":", hand
print "total: #{total}"
puts (busted? ? " (busted)" : ""), ""
end
def hit(game)
game.hit(hand)
end
def initial_draw
reset
raise NotImplementedError,
"method not implemented in #{self.class}"
end
def play_turn(game)
loop do
game.display
make_decision(game)
break if busted? || staying
end
end
def reset
new_hand
self.staying = false
end
def setup
partaker_type = self.class
@display_priority = partaker_type::DISPLAY_PRIORITY
@name = partaker_type::NAME
@move_sequence = partaker_type::MOVE_SEQUENCE
end
def stay
self.staying = true
end
def to_s
name
end
def total
hand.total
end
private
attr_accessor :staying
attr_writer :hand
def make_decision(_game)
raise NotImplementedError,
"method not implemented in #{self.class}"
end
def new_hand
self.hand = Hand.new
end
end
class Dealer < Partaker
include UX
DISPLAY_PRIORITY = 1
HITTING_THRESHOLD = 17
MOVE_DELAY_SECS = 1.5
NAME = "Dealer"
def initial_draw(game)
hit(game)
end
private
def delay_progress
sleep MOVE_DELAY_SECS
end
def make_decision(game)
if under_hitting_limit?
delay_progress unless hand.size == 1
hit(game)
else
stay
end
end
def under_hitting_limit?
total < HITTING_THRESHOLD
end
end
class Player < Partaker
include UI, UX
MOVE_SEQUENCE = 1
NAME = "Player"
def initial_draw(game)
2.times { hit(game) }
end
private
def ask_about_move
get_char(message: "Please choose: <h>it or <s>tay",
invalid_msg: "Please choose \"h\" or \"s\"",
expected: %w[h s])
end
def make_decision(game)
ask_about_move == "h" ? hit(game) : stay
end
end
class GameHandler
include UI, UX
def initialize(*players)
@players = players
end
def start
loop do
TwentyOneGame.new(new_deck, *players).play
break unless rematch?
reset
end
end
private
attr_reader :players
def ask_about_rematch
get_char(message: "Would you like to play again? (y/n)",
expected: %w[y n],
invalid_msg: "Please choose 'y' or 'n'")
end
def new_deck
Deck.new
end
def rematch?
ask_about_rematch == "y"
end
def reset
players.each(&:reset)
end
end
GameHandler.new(Player.new, Dealer.new).start