-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.rb
53 lines (42 loc) · 872 Bytes
/
position.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
class Position
attr_accessor :x, :y
def initialize(x = 0, y = 0)
@x, @y = x, y
end
def self.xy_to_s(x,y)
# raise "x#{x.to_s}y#{y.to_s}"
"x#{x.to_s}y#{y.to_s}"
end
def to_s
self.class.xy_to_s(@x,@y)
# "x#{@x.to_s}y#{@y.to_s}"
end
def move
# this is just convenience to make using the object more readable
self
end
def copy
Position.new(@x, @y)
end
def east_by(distance = 0)
@x += distance
self
end
def west_by(distance = 0)
east_by(0-distance)
end
def north_by(distance = 0)
@y += distance
self
end
def south_by(distance = 0)
north_by(0-distance)
end
def in_compass_terms
direction_x = x > 0 ? "East" : "West"
value_x = x.abs
direction_y = x > 0 ? "North" : "South"
value_y = y.abs
"#{value_y} #{direction_y}, #{value_x} #{direction_x}"
end
end