forked from cdepair/darts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darts.rb
56 lines (50 loc) · 984 Bytes
/
darts.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
class Darts
RADIUSES = [0.5, 1.25, 4.025, 4.2, 6.525, 6.7, Float::INFINITY]
SLICES = [6, 13, 4, 18, 1, 20, 5, 12, 9, 14, 11, 8, 16, 7, 19, 3, 17, 2, 15, 10]
attr_accessor :x, :y, :theta, :distance
def initialize x, y
@x = x
@y = y
@theta = get_theta
@distance = distance_from_bullseye
end
def score
return 50 if theta == 'bullseye'
case ring
when 1
50
when 2
25
when 4
slice * 3
when 6
slice * 2
when 7
0
else
slice
end
end
def get_theta
return 'bullseye' if x == 0 && y == 0
if x == 0
y > 0 ? 90 : 270
elsif y == 0
x > 0 ? 0 : 180
else
Math.atan(y/x) * (180/Math::PI)
end
end
def distance_from_bullseye
Math.sqrt(x*x + y*y)
end
def slice
offset = ((theta + 9) % 360) / 18
SLICES[offset]
end
def ring
RADIUSES.each_with_index do |radius, index|
return index + 1 if radius >= distance
end
end
end