Решение на Шеста задача от Огнян Ангелов

Обратно към всички решения

Към профила на Огнян Ангелов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 9 успешни тест(а)
  • 5 неуспешни тест(а)

Код

module TurtleGraphics
class Turtle
ORIENTATIONS = [:left, :up, :right, :down]
def initialize(rows, cols)
@rows = rows
@cols = cols
@matrix = Array.new(rows) { Array.new(cols, 0) }
@position = { row: 0, col: 0 }
@orientation = :right
@default_spawn = true
end
def draw(&block)
instance_eval(&block)
@matrix[0][0] += @default_spawn == true ? 1 : 0
@matrix.clone
end
def move
advance_position
normalise_position
@matrix[@position[:row]][@position[:col]] += 1
end
def turn_right
next_orientation_index = (ORIENTATIONS.index(@orientation) + 1)
@orientation = ORIENTATIONS[next_orientation_index % ORIENTATIONS.count]
end
def turn_left
next_orientation_index = (ORIENTATIONS.index(@orientation) - 1)
@orientation = ORIENTATIONS[next_orientation_index % ORIENTATIONS.count]
end
def spawn_at(row, col)
@default_spawn = false
@position = { row: row, col: col }
@matrix[@position[:row]][@position[:col]] += 1
end
def look(orientation)
@orientation = orientation
end
private
def advance_position
case @orientation
when :up
@position[:row] -= 1
when :down
@position[:row] += 1
when :left
@position[:col] -= 1
when :right
@position[:col] += 1
end
end
def normalise_position
@position[:row] %= @rows
@position[:col] %= @cols
end
end
module Canvas
class ASCII
end
class HTML
end
end
end

Лог от изпълнението

.........FFFFF

Failures:

  1) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: ascii_canvas = TurtleGraphics::Canvas::ASCII.new(['0', '1', '2', '3'])
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151203-5272-yxqkmf/spec.rb:113:in `initialize'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:113:in `new'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:113:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: ascii_canvas = TurtleGraphics::Canvas::ASCII.new(['z', 'o', 't'])
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151203-5272-yxqkmf/spec.rb:136:in `initialize'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:136:in `new'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:136:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `initialize'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `new'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:166:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `initialize'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `new'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:219:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `initialize'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `new'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-yxqkmf/spec.rb:228:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01002 seconds
14 examples, 5 failures

Failed examples:

rspec /tmp/d20151203-5272-yxqkmf/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-yxqkmf/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-yxqkmf/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-yxqkmf/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-yxqkmf/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

История (1 версия и 0 коментара)

Огнян обнови решението на 02.12.2015 16:32 (преди около 9 години)

+module TurtleGraphics
+ class Turtle
+ ORIENTATIONS = [:left, :up, :right, :down]
+
+ def initialize(rows, cols)
+ @rows = rows
+ @cols = cols
+ @matrix = Array.new(rows) { Array.new(cols, 0) }
+ @position = { row: 0, col: 0 }
+ @orientation = :right
+ @default_spawn = true
+ end
+
+ def draw(&block)
+ instance_eval(&block)
+ @matrix[0][0] += @default_spawn == true ? 1 : 0
+ @matrix.clone
+ end
+
+ def move
+ advance_position
+ normalise_position
+ @matrix[@position[:row]][@position[:col]] += 1
+ end
+
+ def turn_right
+ next_orientation_index = (ORIENTATIONS.index(@orientation) + 1)
+ @orientation = ORIENTATIONS[next_orientation_index % ORIENTATIONS.count]
+ end
+
+ def turn_left
+ next_orientation_index = (ORIENTATIONS.index(@orientation) - 1)
+ @orientation = ORIENTATIONS[next_orientation_index % ORIENTATIONS.count]
+ end
+
+ def spawn_at(row, col)
+ @default_spawn = false
+ @position = { row: row, col: col }
+ @matrix[@position[:row]][@position[:col]] += 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ private
+
+ def advance_position
+ case @orientation
+ when :up
+ @position[:row] -= 1
+ when :down
+ @position[:row] += 1
+ when :left
+ @position[:col] -= 1
+ when :right
+ @position[:col] += 1
+ end
+ end
+
+ def normalise_position
+ @position[:row] %= @rows
+ @position[:col] %= @cols
+ end
+ end
+
+ module Canvas
+ class ASCII
+
+ end
+
+ class HTML
+
+ end
+ end
+end