Commit 3c46df7
Changed files (4)
bin
bin/app.rb
@@ -1,43 +1,12 @@
#!/usr/bin/ruby
-require 'east'
-require 'north'
-require 'rover'
-require 'south'
-require 'terrain'
-require 'west'
+require "console"
-def get_heading(pneumonic)
- case(pneumonic)
- when 'N'
- North.new
- when 'E'
- East.new
- when 'W'
- West.new
- when 'S'
- South.new
- end
+begin
+ Console.new(STDIN, STDOUT).run
+rescue Exception => e
+ puts e
+ return 1
end
-puts "enter size of terrain:"
-edge = gets.split(' ')
-terrain = Terrain.new({:x => edge[0].to_i, :y => edge[1].to_i})
+exit 0
-puts "enter landing coordinates:"
-landing = gets
-rover = Rover.new(get_heading(landing.split(" ")[2]), {:x => landing.split(" ")[0].to_i, :y => landing.split(" ")[1].to_i})
-
-puts "enter instructions:"
-gets.split(//).each do |instruction|
- puts instruction
- case(instruction)
- when 'M'
- rover.move_forward(terrain)
- when 'R'
- rover.turn_right
- when 'L'
- rover.turn_left
- end
-end
-
-puts "final location: #{rover}"
lib/console.rb
@@ -0,0 +1,52 @@
+require 'terrain'
+require 'rover'
+require 'north'
+require 'east'
+require 'west'
+require 'south'
+
+class Console
+ def initialize(input, output)
+ @input = input
+ @output = output
+ end
+ def run
+ edge = ask('enter size of terrain:', ' ')
+ terrain = Terrain.new({:x => edge[0].to_i, :y => edge[1].to_i})
+
+ landing = ask('enter landing coordinate:', ' ')
+ rover = Rover.new(get_heading(landing[2]), {:x => landing[0].to_i, :y => landing[1].to_i})
+
+ ask('enter instructions:', //).each do |instruction|
+ puts instruction
+ case(instruction)
+ when 'M'
+ rover.move_forward(terrain)
+ when 'R'
+ rover.turn_right
+ when 'L'
+ rover.turn_left
+ end
+ end
+
+ @output.puts "final location: #{rover}"
+ end
+
+ def ask(message, split)
+ @output.puts message
+ @input.gets.split(split)
+ end
+
+ def get_heading(pneumonic)
+ case(pneumonic)
+ when 'N'
+ North.new
+ when 'E'
+ East.new
+ when 'W'
+ West.new
+ when 'S'
+ South.new
+ end
+ end
+end
lib/north.rb
@@ -6,6 +6,6 @@ class North
West.new
end
def forward(location)
- location[:y] = location[:y]+1
+ location[:y] = location[:y].to_i + 1
end
end
lib/rover.rb
@@ -3,7 +3,6 @@ class Rover
def initialize(heading, coordinates)
@heading = heading
@location = coordinates
- puts coordinates
end
def heading
@heading.class.name.downcase.to_sym
@@ -17,5 +16,8 @@ class Rover
def move_forward(terrain)
terrain.move_forward(@heading, @location)
end
+ def to_s
+ "#{@heading} #{@location}"
+ end
end