diff --git a/Training/Easy/power_of_thor.rb b/Training/Easy/power_of_thor.rb index d1972fd..bef8219 100644 --- a/Training/Easy/power_of_thor.rb +++ b/Training/Easy/power_of_thor.rb @@ -3,65 +3,68 @@ class Thor attr_accessor :light_x, :light_y, :x, :y - def initialize - @light_x, @light_y, @x, @y = gets.split(' ').collect(&:to_i) + def initialize(light_x, light_y, thor_x, thor_y) + @light_x = light_x + @light_y = light_y + @x = thor_x + @y = thor_y end - def find_direction - direction = '' - if x < light_x - direction = 'E' - if y > light_y - direction = 'NE' - else - direction = 'SE' unless y == light_y - end - elsif x == light_x - if y > light_y - direction = 'N' - else - direction = 'S' - end + def move_north + self.y -= 1 + if x == light_x + 'N' + elsif x > light_x + self.x -= 1 + 'NW' else - direction = 'W' - if y > light_y - direction = 'NW' - else - direction = 'SW' unless y == light_y - end + self.x += 1 + 'NE' end - move(direction) - direction end - def move(direction) - case direction - when 'N' then self.y -= 1 - when 'NE' - self.y -= 1 - self.x += 1 - when 'E' then self.x += 1 - when 'SE' - self.x += 1 - self.y += 1 - when 'S' then self.y += 1 - when 'SW' - self.y += 1 + def move_south + self.y += 1 + if x == light_x + 'S' + elsif x > light_x self.x -= 1 - when 'W' then self.x -= 1 - when 'NW' - self.x -= 1 - self.y -= 1 + 'SW' + else + self.x += 1 + 'SE' + end + end + + def move_west + self.x -= 1 + 'W' + end + + def move_east + self.x += 1 + 'E' + end + + def find_direction + if y == light_y && x > light_x + move_west + elsif y == light_y && x < light_x + move_east + elsif y > light_y + move_north + else + move_south end end def start loop do - remaining_turns = gets.to_i # The remaining amount of turns Thor can move. Do not remove this line. puts find_direction end end end -thor = Thor.new +light_x, light_y, x, y = gets.split(' ').collect(&:to_i) +thor = Thor.new(light_x, light_y, x, y) thor.start