Решение на Шеста задача от Петър Нетовски

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

Към профила на Петър Нетовски

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 2 успешни тест(а)
  • 12 неуспешни тест(а)

Код

class TurtleGraphics
end
class TurtleGraphics::Turtle
attr_accessor :canvas
def initialize(rows, columns)
@rows = rows
@columns = columns
@canvas = Array.new(@rows).map { |x| x = Array.new(@columns, 0) }
@canvas[0][0] = 1
@direction = :right
@position = [0, 0]
@directions = [:right, :down, :left, :up]
end
def move
case @direction
when :right then @position[1] += 1
when :down then @position[0] += 1
when :left then @position[1] -= 1
when :up then @position[0] -= 1
end
@canvas[@position[0]][@position[1]] = 1
end
def turn_right
if @directions.index(@direction) + 1 < @directions.size
@direction = @directions[@directions.index(@direction) + 1]
else
@direction = @directions[0]
end
end
def draw(&block)
instance = TurtleGraphics::Turtle.new(@rows, @columns)
instance.instance_eval(&block)
instance.canvas
end
end

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

F.FFF.FFFFFFFF

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: spawn_at 10, 10
     NoMethodError:
       undefined method `spawn_at' for #<TurtleGraphics::Turtle:0x007fcc9ed31018>
     # /tmp/d20151203-5272-tn84yi/spec.rb:266:in `block (3 levels) in <top (required)>'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `instance_eval'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `draw'
     # /tmp/d20151203-5272-tn84yi/spec.rb:265:in `block (2 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 Turtle #draw #move moves the turtle to the start of row or column when we are at its end
     Failure/Error: expect(canvas[0]).to eq [2, 2]
       
       expected: [2, 2]
            got: [1, 1, 1, 1]
       
       (compared using ==)
     # /tmp/d20151203-5272-tn84yi/spec.rb:19:in `block (5 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 Turtle #draw #move keeps the orientation when we get out of column
     Failure/Error: 4.times { move }
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20151203-5272-tn84yi/solution.rb:25:in `move'
     # /tmp/d20151203-5272-tn84yi/spec.rb:36:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-tn84yi/spec.rb:36:in `times'
     # /tmp/d20151203-5272-tn84yi/spec.rb:36:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `instance_eval'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `draw'
     # /tmp/d20151203-5272-tn84yi/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-tn84yi/spec.rb:34:in `block (5 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 Turtle #draw #move counts the times we have passed through every cell
     Failure/Error: expect(canvas).to eq [[2, 1], [0, 0]]
       
       expected: [[2, 1], [0, 0]]
            got: [[1, 1, 1], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-tn84yi/spec.rb:50:in `block (5 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 Turtle #draw #turn_left turns the orienation of the turtle left of where we stand
     Failure/Error: turn_left
     NameError:
       undefined local variable or method `turn_left' for #<TurtleGraphics::Turtle:0x007fcc9e792e68>
     # /tmp/d20151203-5272-tn84yi/spec.rb:75:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `instance_eval'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `draw'
     # /tmp/d20151203-5272-tn84yi/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-tn84yi/spec.rb:74:in `block (5 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)>'

  6) TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: canvas = create_canvas { spawn_at(1, 0) }
     NoMethodError:
       undefined method `spawn_at' for #<TurtleGraphics::Turtle:0x007fcc9e9838a8>
     # /tmp/d20151203-5272-tn84yi/spec.rb:92:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `instance_eval'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `draw'
     # /tmp/d20151203-5272-tn84yi/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-tn84yi/spec.rb:92:in `block (5 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)>'

  7) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: turn_left
     NameError:
       undefined local variable or method `turn_left' for #<TurtleGraphics::Turtle:0x007fcc9e971b80>
     # /tmp/d20151203-5272-tn84yi/spec.rb:100:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `instance_eval'
     # /tmp/d20151203-5272-tn84yi/solution.rb:38:in `draw'
     # /tmp/d20151203-5272-tn84yi/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-tn84yi/spec.rb:99:in `block (5 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)>'

  8) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: ascii_canvas = TurtleGraphics::Canvas::ASCII.new(['0', '1', '2', '3'])
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-tn84yi/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)>'

  9) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: ascii_canvas = TurtleGraphics::Canvas::ASCII.new(['z', 'o', 't'])
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-tn84yi/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)>'

  10) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-tn84yi/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-tn84yi/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)>'

  11) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-tn84yi/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-tn84yi/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)>'

  12) 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)
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-tn84yi/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-tn84yi/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.01485 seconds
14 examples, 12 failures

Failed examples:

rspec /tmp/d20151203-5272-tn84yi/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-tn84yi/spec.rb:14 # TurtleGraphics Turtle #draw #move moves the turtle to the start of row or column when we are at its end
rspec /tmp/d20151203-5272-tn84yi/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-tn84yi/spec.rb:45 # TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
rspec /tmp/d20151203-5272-tn84yi/spec.rb:73 # TurtleGraphics Turtle #draw #turn_left turns the orienation of the turtle left of where we stand
rspec /tmp/d20151203-5272-tn84yi/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-tn84yi/spec.rb:98 # TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
rspec /tmp/d20151203-5272-tn84yi/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-tn84yi/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-tn84yi/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-tn84yi/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-tn84yi/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Петър обнови решението на 02.12.2015 15:49 (преди над 8 години)

+class TurtleGraphics
+end
+
+class TurtleGraphics::Turtle
+ attr_accessor :canvas
+
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+ @canvas = Array.new(@rows).map { |x| x = Array.new(@columns, 0) }
+ @canvas[0][0] = 1
+ @direction = :right
+ @position = [0, 0]
+ @directions = [:right, :down, :left, :up]
+ end
+
+ def move
+ case @direction
+ when :right then @position[1] += 1
+ when :down then @position[0] += 1
+ when :left then @position[1] -= 1
+ when :up then @position[0] -= 1
+ end
+
+ @canvas[@position[0]][@position[1]] = 1
+ end
+
+ def turn_right
+ if @directions.index(@direction) + 1 < @directions.size
+ @direction = @directions[@directions.index(@direction) + 1]
+ else
+ @direction = @directions[0]
+ end
+ end
+
+ def draw(&block)
+ instance = TurtleGraphics::Turtle.new(@rows, @columns)
+ instance.instance_eval(&block)
+ instance.canvas
+ end
+end