From ca817fbd57109ffcf771278f347a63db2808fa0e Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Thu, 12 Jan 2017 14:12:13 +0200 Subject: [PATCH 1/2] Create methods for each direction and refactor the find direction method --- Training/Easy/power_of_thor.rb | 78 ++++++++++++++++------------------ 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/Training/Easy/power_of_thor.rb b/Training/Easy/power_of_thor.rb index d1972fd..e0d43fd 100644 --- a/Training/Easy/power_of_thor.rb +++ b/Training/Easy/power_of_thor.rb @@ -7,58 +7,52 @@ def initialize @light_x, @light_y, @x, @y = gets.split(' ').collect(&:to_i) 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 + puts 'N' + elsif x > light_x + self.x -= 1 + puts 'NW' else - direction = 'W' - if y > light_y - direction = 'NW' - else - direction = 'SW' unless y == light_y - end + self.x += 1 + puts '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 - self.x -= 1 - when 'W' then self.x -= 1 - when 'NW' + def move_south + self.y += 1 + if x == light_x + puts 'S' + elsif x > light_x self.x -= 1 - self.y -= 1 + puts 'SW' + else + self.x += 1 + puts 'SE' end end + def move_west + self.x -= 1 + puts 'W' + end + + def move_east + self.x += 1 + puts 'E' + end + + def find_direction + move_west if y == light_y && x > light_x + move_east if y == light_y && x < light_x + move_north if y > light_y + move_south if y < light_y + 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 + find_direction end end end From f8ebba33e97915ebd910eec997729d2b247b2c69 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Tue, 24 Jan 2017 14:33:03 +0200 Subject: [PATCH 2/2] Refactor the code and put gets out of the class --- Training/Easy/power_of_thor.rb | 41 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/Training/Easy/power_of_thor.rb b/Training/Easy/power_of_thor.rb index e0d43fd..bef8219 100644 --- a/Training/Easy/power_of_thor.rb +++ b/Training/Easy/power_of_thor.rb @@ -3,59 +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 move_north self.y -= 1 if x == light_x - puts 'N' + 'N' elsif x > light_x self.x -= 1 - puts 'NW' + 'NW' else self.x += 1 - puts 'NE' + 'NE' end end def move_south self.y += 1 if x == light_x - puts 'S' + 'S' elsif x > light_x self.x -= 1 - puts 'SW' + 'SW' else self.x += 1 - puts 'SE' + 'SE' end end def move_west self.x -= 1 - puts 'W' + 'W' end def move_east self.x += 1 - puts 'E' + 'E' end def find_direction - move_west if y == light_y && x > light_x - move_east if y == light_y && x < light_x - move_north if y > light_y - move_south if y < light_y + 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 - find_direction + 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