Skip to content

Commit 9ba0df4

Browse files
committed
Create methods for each direction and refactor the find direction method
1 parent 2ddc599 commit 9ba0df4

1 file changed

Lines changed: 36 additions & 42 deletions

File tree

power_of_thor.rb

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,52 @@ def initialize
77
@light_x, @light_y, @x, @y = gets.split(' ').collect(&:to_i)
88
end
99

10-
def find_direction
11-
direction = ''
12-
if x < light_x
13-
direction = 'E'
14-
if y > light_y
15-
direction = 'NE'
16-
else
17-
direction = 'SE' unless y == light_y
18-
end
19-
elsif x == light_x
20-
if y > light_y
21-
direction = 'N'
22-
else
23-
direction = 'S'
24-
end
10+
def move_north
11+
self.y -= 1
12+
if x == light_x
13+
puts 'N'
14+
elsif x > light_x
15+
self.x -= 1
16+
puts 'NW'
2517
else
26-
direction = 'W'
27-
if y > light_y
28-
direction = 'NW'
29-
else
30-
direction = 'SW' unless y == light_y
31-
end
18+
self.x += 1
19+
puts 'NE'
3220
end
33-
move(direction)
34-
direction
3521
end
3622

37-
def move(direction)
38-
case direction
39-
when 'N' then self.y -= 1
40-
when 'NE'
41-
self.y -= 1
42-
self.x += 1
43-
when 'E' then self.x += 1
44-
when 'SE'
45-
self.x += 1
46-
self.y += 1
47-
when 'S' then self.y += 1
48-
when 'SW'
49-
self.y += 1
50-
self.x -= 1
51-
when 'W' then self.x -= 1
52-
when 'NW'
23+
def move_south
24+
self.y += 1
25+
if x == light_x
26+
puts 'S'
27+
elsif x > light_x
5328
self.x -= 1
54-
self.y -= 1
29+
puts 'SW'
30+
else
31+
self.x += 1
32+
puts 'SE'
5533
end
5634
end
5735

36+
def move_west
37+
self.x -= 1
38+
puts 'W'
39+
end
40+
41+
def move_east
42+
self.x += 1
43+
puts 'E'
44+
end
45+
46+
def find_direction
47+
move_west if y == light_y && x > light_x
48+
move_east if y == light_y && x < light_x
49+
move_north if y > light_y
50+
move_south if y < light_y
51+
end
52+
5853
def start
5954
loop do
60-
remaining_turns = gets.to_i # The remaining amount of turns Thor can move. Do not remove this line.
61-
puts find_direction
55+
find_direction
6256
end
6357
end
6458
end

0 commit comments

Comments
 (0)