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
|
#!/usr/bin/env ruby
HORIZONTAL_ACCURACY = 32.0
VERTICAL_ACCURACY = 32.0
# board spec from WDF rules
WIRE_WIDTH = 1.56
INNER_DIAMETER_BULL = 12.7
INNER_DIAMETER_25 = 31.8
DOUBLE_OUTER_EDGE = 170.0
TREBLE_OUTER_EDGE = 107.4
DOUBLE_INSIDE_WIDTH = 8.0
TREBLE_INSIDE_WIDTH = 8.0
SECTORS = %w[20 1 18 4 13 6 10 15 2 17 3 19 7 16 8 11 14 9 12 5].map(&:to_i)
SECTOR_ANGLES = SECTORS.each_with_index.inject({}) do |m, (v, i)|
m[v] = (90 - (i * 360 / SECTORS.length)) % 360
m
end
# distance from centre to apex of outer wire
# must be ordered outwards from centre
OUTER_DISTS = {
bull: INNER_DIAMETER_BULL/2 + WIRE_WIDTH/2,
'25': INNER_DIAMETER_25/2 + WIRE_WIDTH/2,
small: TREBLE_OUTER_EDGE - WIRE_WIDTH - TREBLE_INSIDE_WIDTH - WIRE_WIDTH/2,
treble: TREBLE_OUTER_EDGE - WIRE_WIDTH/2,
big: DOUBLE_OUTER_EDGE - WIRE_WIDTH - DOUBLE_INSIDE_WIDTH - WIRE_WIDTH/2,
double: DOUBLE_OUTER_EDGE - WIRE_WIDTH/2
}
CENTRE_DISTS = OUTER_DISTS.inject([{}, nil]) { |(h, prev), (k, v)|
h[k] = prev ? (v - ((v - prev) / 2)) : 0.0
[h, v]
}[0]
def get_coordinates(angle, radius)
t = angle * Math::PI / 180
x = radius * Math.cos(t)
y = radius * Math.sin(t)
[x, y]
end
def throw_dart(angle, radius)
x, y = get_coordinates(angle, radius)
x += rand(-HORIZONTAL_ACCURACY..HORIZONTAL_ACCURACY)
y += rand(-VERTICAL_ACCURACY..VERTICAL_ACCURACY)
d_angle = Math.atan2(y, x) * 180 / Math::PI
d_radius = Math.sqrt(x**2 + y**2)
[d_angle, d_radius]
end
def get_sector(angle)
sector = SECTOR_ANGLES.detect do |v, c|
dist = (c - angle).abs % 360
dist = 360 - dist if dist > 180
dist < 360 / SECTORS.length / 2 #TODO hit wire
end
sector && sector.first
end
def get_ring(radius)
ring = OUTER_DISTS.detect do |sym, dist|
radius**2 < dist**2
end
ring ? ring[0] : :out
end
def get_segment(angle, radius)
sec = get_sector(angle)
ring = get_ring(radius)
case ring
when :bull
'BULL'
when :'25'
'25'
when :small, :big
"#{sec}"
when :double
"D#{sec}"
when :treble
"T#{sec}"
when :out
'OUT'
end
end
def get_points(segment)
case segment
when 'BULL'
50
when /^T(\d+)$/
3 * $1.to_i
when /^D(\d+)$/
2 * $1.to_i
when /^(\d+)$/
$1.to_i
else
0
end
end
def get_segment_coordinates(segment)
sector, ring =
case segment
when 'BULL'
[20, :bull]
when '25'
[20, :'25']
when /^T(\d+)$/
[$1.to_i, :treble]
when /^D(\d+)$/
[$1.to_i, :double]
when /^(\d+)$/
[$1.to_i, :big]
end
[SECTOR_ANGLES[sector], CENTRE_DISTS[ring]]
end
def next_dart(rem)
segment =
if rem > 40
'T20'
elsif rem % 2 == 1
'1'
else
"D#{rem/2}"
end
get_segment_coordinates(segment)
end
def play_visit(rem)
v_rem = rem
darts = []
bust = false
3.times do
t_angle, t_radius = next_dart(v_rem)
d_angle, d_radius = throw_dart(t_angle, t_radius)
segment = get_segment(d_angle, d_radius)
darts << segment
d_points = get_points(segment)
v_rem -= d_points
if v_rem < 0 || v_rem == 1 || (v_rem == 0 && segment !~ /^D/)
v_rem = rem
bust = true
break
end
break if v_rem.zero?
end
[v_rem, rem - v_rem, darts, bust]
end
def colour_score(score)
scor = "%3s" % score
if score == 180
"\e[1;38;5;82m#{scor}\e[0m"
elsif score > 139
"\e[38;5;82m#{scor}\e[0m"
elsif score > 99
"\e[38;5;154m#{scor}\e[0m"
elsif score > 59
"\e[38;5;226m#{scor}\e[0m"
elsif score > 39
"\e[38;5;214m#{scor}\e[0m"
elsif score > 19
"\e[38;5;202m#{scor}\e[0m"
else
"\e[38;5;196m#{scor}\e[0m"
end
end
def visit_darts(darts)
str = darts.map { |x| '%3s' % x }.join(' ')
str += ' ' * (3 - darts.length)
str
end
def output_visit(n, rem, points = nil, darts = nil, bust = false)
str = '(%2s) %3s' % [n, rem]
str << ' %s' % colour_score(points) if points
str << " \e[38;5;235m%s\e[0m" % visit_darts(darts) if darts
str << " \e[38;5;88mBUST\e[0m" if bust
str << " \e[38;5;76mWIN\e[0m" if rem.zero?
puts str
end
def play_match(start_points = 501, output = true)
rem = start_points
m_darts = []
output_visit(m_darts.length, rem) if output
while rem != 0
rem, score, darts, bust = play_visit(rem)
m_darts << darts
output_visit(m_darts.length, rem, score, darts, bust) if output
if rem.zero? && output
puts
req = (((m_darts.length - 1) * 3) + m_darts[-1].length)
puts "darts: %s" % req
puts "average: %.2f" % (start_points.to_f / req * 3)
end
end
m_darts
end
def test_average
averages = (0..10000).map do |x|
m_darts = play_match(501, false)
req = (((m_darts.length - 1) * 3) + m_darts[-1].length)
(501.to_f / req * 3)
end
puts
puts averages.inject { |sum, x| sum += x } / averages.length
end
play_match
#test_average
|