From 7d902ed12a0dc4a94919df7b827756d1705201b8 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 27 Mar 2022 19:50:42 -0700 Subject: use normal distribution for accuracy --- dartbot.rb | 61 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'dartbot.rb') 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 -- cgit v1.2.3-70-g09d2