summaryrefslogtreecommitdiff
path: root/dartbot.rb
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-03-27 19:50:42 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-03-27 19:50:42 -0700
commit7d902ed12a0dc4a94919df7b827756d1705201b8 (patch)
tree8b412187ec6e854a84b0950d4639cfd379197390 /dartbot.rb
parentf974e0349ff5b5730bbee1ac733d3f993762b660 (diff)
downloaddartboat-7d902ed12a0dc4a94919df7b827756d1705201b8.tar.gz
dartboat-7d902ed12a0dc4a94919df7b827756d1705201b8.tar.xz
use normal distribution for accuracy
Diffstat (limited to 'dartbot.rb')
-rwxr-xr-xdartbot.rb61
1 files changed, 36 insertions, 25 deletions
diff --git a/dartbot.rb b/dartbot.rb
index ccf5e38..facd745 100755
--- a/dartbot.rb
+++ b/dartbot.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
-HORIZONTAL_ACCURACY = 32.0
-VERTICAL_ACCURACY = 32.0
+HORIZONTAL_STDEV = 24
+VERTICAL_STDEV = 24
# board spec from WDF rules
WIRE_WIDTH = 1.56
@@ -42,10 +42,17 @@ def get_coordinates(angle, radius)
[x, y]
end
+def gauss
+ theta = 2 * Math::PI * rand
+ r = Math.sqrt(-2 * Math.log(1 - rand))
+ [r * Math.cos(theta), r * Math.sin(theta)]
+end
+
def throw_dart(angle, radius)
x, y = get_coordinates(angle, radius)
- x += rand(-HORIZONTAL_ACCURACY..HORIZONTAL_ACCURACY)
- y += rand(-VERTICAL_ACCURACY..VERTICAL_ACCURACY)
+ x_offset, y_offset = gauss
+ x += HORIZONTAL_STDEV * x_offset
+ y += VERTICAL_STDEV * y_offset
d_angle = Math.atan2(y, x) * 180 / Math::PI
d_radius = Math.sqrt(x**2 + y**2)
@@ -164,23 +171,23 @@ def play_visit(rem)
[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"
+def colour_points(points)
+ str = "%3s" % points
+
+ if points == 180
+ "\e[1;38;5;82m#{str}\e[0m"
+ elsif points > 139
+ "\e[38;5;82m#{str}\e[0m"
+ elsif points > 99
+ "\e[38;5;154m#{str}\e[0m"
+ elsif points > 59
+ "\e[38;5;226m#{str}\e[0m"
+ elsif points > 39
+ "\e[38;5;214m#{str}\e[0m"
+ elsif points > 19
+ "\e[38;5;202m#{str}\e[0m"
else
- "\e[38;5;196m#{scor}\e[0m"
+ "\e[38;5;196m#{str}\e[0m"
end
end
@@ -192,7 +199,7 @@ 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 << ' %s' % colour_points(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?
@@ -207,10 +214,10 @@ def play_match(start_points = 501, output = true)
output_visit(m_darts.length, rem) if output
while rem != 0
- rem, score, darts, bust = play_visit(rem)
+ rem, points, darts, bust = play_visit(rem)
m_darts << darts
- output_visit(m_darts.length, rem, score, darts, bust) if output
+ output_visit(m_darts.length, rem, points, darts, bust) if output
if rem.zero? && output
puts
@@ -224,14 +231,18 @@ def play_match(start_points = 501, output = true)
end
def test_average
- averages = (0..10000).map do |x|
+ avgs = (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
+ avg = avgs.sum / avgs.length
+ stdev = Math.sqrt(avgs.sum { |x| (x - avg) ** 2 } / (avgs.length - 1))
+
puts
- puts averages.inject { |sum, x| sum += x } / averages.length
+ puts avg
+ puts stdev
end
play_match