-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-gameplay.rb
executable file
·84 lines (69 loc) · 2.08 KB
/
simple-gameplay.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
#!/usr/bin/env ruby
require 'bundler/setup'
Bundler.require :default, :development
RENDER_TIMEOUT = 1 # seconds
players = [
{ id: 0, balance: 1000 },
{ id: 1, balance: 900 },
{ id: 2, balance: 500 },
]
def event_handler(action, state)
draw state, action
sleep RENDER_TIMEOUT
end
def draw_border(delimiter, title = '')
title.center 80, delimiter
end
def player_name(id)
"Player#{id}".yellow
end
def draw(state, action)
system 'clear'
puts <<~TEMPLATE.yellow
#{draw_border '>'}
POT: #{state[:pot]}
BOARD: #{state[:board].map(&:to_s).join(', ')}
#{draw_border '<'}
TEMPLATE
state[:players].sort_by { |id, _| id }.each do |id, player|
player_box = <<~TEMPLATE
#{draw_border '=', player_name(id)}
POSITION: #{player[:position]}
BALANCE: #{player[:balance]}
CARDS: #{player[:cards].map(&:to_s).join(', ')}
Money in the pot: #{player[:money_in_pot]}
#{draw_border '='}
TEMPLATE
puts player[:active] ? player_box : player_box.red
end
case action[:type]
when :move_request
print "#{player_name(action[:player_id])} make a move [move, bet] -> "
move, bet = gets.strip.split("\s")
PokerEngine::Game.next state,
type: move.to_sym,
bet: bet&.to_i,
player_id: action[:player_id],
&method(:event_handler)
when :game_end
winners = state[:winner_ids].map(&method(:player_name))
winners_label = winners.one? ? "Winner #{winners.first}" : "Winners #{winners.join(', ')}"
players_hands =
state[:top_hands]
.sort_by { |_, hand| hand }
.map do |id, hand|
"#{player_name(id)} with hand #{hand.cards} #{hand.level.name}\n"
end
.join
puts <<~TEMPLATE
#{draw_border '#', winners_label}
#{players_hands}
#{draw_border '#'}
TEMPLATE
end
end
PokerEngine::Game.start players,
small_blind: 10,
big_blind: 20,
deck_seed: rand(1000),
&method(:event_handler)