forked from kevinkey/klank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragon.rb
127 lines (104 loc) · 3.94 KB
/
dragon.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
module Klank
require_relative "utils.rb"
class Dragon
DRAW = [2, 2, 3, 3, 4, 4, 5]
attr_reader :bank
def initialize(game)
@game = game
@level = 4 - @game.num
@bag = []
@bank = []
@dragon_cubes = 0
30.times do
@bag << "D"
end
end
def anger()
move_marker(1)
end
def move_marker(count)
@level = [0, [DRAW.count - 1, @level + count].min].max
@game.broadcast("The dragon marker moves #{(count > 0) ? "up" : "down"} #{count.abs()} space(s) and now draws #{DRAW[@level]}!")
end
def add(cube, count = 1)
count.times do
@bank << cube
end
@game.broadcast("#{@game.player[cube].name} adds #{count} clank to the bank!")
end
def remove(cube, count)
removed = 0
if count != 0
count.times do
if @bank.delete_at(@bank.index(cube) || @bank.length)
removed += 1
end
end
@game.broadcast("#{@game.player[cube].name} removed #{removed} clank from the bank!")
end
removed
end
def reveal(count)
@bag = Klank.randomize(@bag)
@bag[0..([count, @bag.count].min - 1)]
end
def attack()
@bag += @bank
@bank = []
@bag = Klank.randomize(@bag)
draw = DRAW[@level] + @game.dungeon.danger() + @game.escalation
view_bag(draw)
@game.broadcast("The dragon attacks, drawing #{draw} cubes...")
sleep(1)
draw.times do
if @bag.count > 0
c = @bag.pop
if c == "D"
@game.broadcast("The dragon's attack misses!")
@dragon_cubes += 1
else
@game.broadcast("+1 damage to #{@game.player[c].name}!")
@game.player[c].damage()
end
sleep(1)
else
@game.broadcast("The dragon bag is empty!")
break
end
end
end
def add_dragon_cubes()
actual = [@dragon_cubes, 3].min
actual.times do
@bag << "D"
end
@dragon_cubes -= actual
@game.broadcast("#{actual} dragon cube(s) were added back to the dragon bag!")
end
def view_bag(draw_count = 0)
if @bag.length > 0
cubes = []
@game.player.each_with_index do |p, i|
if (draw_count > 0)
cubes << {"PLAYER" => p.name, "COUNT" => @bag.select { |c| c.to_s == i.to_s }.count, "EXPECTED DRAW COUNT" => calculate_expected_draw_count(i.to_s, draw_count), "HEALTH" => p.health}
else
cubes << {"PLAYER" => p.name, "COUNT" => @bag.select { |c| c.to_s == i.to_s }.count}
end
end
if (draw_count > 0)
cubes << {"PLAYER" => "Dragon", "COUNT" => @bag.select { |c| c.to_s == "D" }.count, "EXPECTED DRAW COUNT" => calculate_expected_draw_count("D", draw_count)}
else
cubes << {"PLAYER" => "Dragon", "COUNT" => @bag.select { |c| c.to_s == "D" }.count}
end
@game.broadcast("\nDRAGON BAG\n#{Klank.table(cubes)}")
end
end
def clank_in_bank?(player)
@bank.select { |c| c == player.index }.count > 0
end
private
def calculate_expected_draw_count(player, num_draws)
return (@bag.select { |c| c.to_s == player }.count.to_f / @bag.count * ([num_draws, @bag.count].min)).round(2)
end
end
end