Решение на Шеста задача от Даяна Маринова

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

Към профила на Даяна Маринова

Резултати

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

Код

module TurtleGraphics
class Turtle
attr_accessor :canvas
ORIENTATIONS = [:left, :up, :right, :down]
def initialize(rows, columns)
@canvas = Array.new(rows) { |row| Array.new(columns) { |column| 0 } }
@orientation = :right
@turtle_position = spawn_at
@canvas[@turtle_position[:row]][@turtle_position[:column]] += 1
end
def draw(special_way = nil, &block)
self.instance_exec &block
special_way ? special_way.draw(@canvas) : @canvas
end
def move
case @orientation
when :right then @turtle_position[:column] += 1
when :left then @turtle_position[:column] -= 1
when :up then @turtle_position[:row] -= 1
when :down then @turtle_position[:row] += 1
end
transfer_turtle
@canvas[@turtle_position[:row]][@turtle_position[:column]] += 1
end
def turn_left
@orientation = ORIENTATIONS[ORIENTATIONS.index(@orientation) - 1]
end
def turn_right
@orientation = ORIENTATIONS[ORIENTATIONS.index(@orientation) + 1]
end
def spawn_at(row = 0, column = 0)
{row: row, column: column}
end
def look(orientation)
@orientation = orientation
end
private
def transfer_turtle
row = @turtle_position[:row]
column = @turtle_position[:column]
case
when row >= @canvas.size then @turtle_position[:row] == 0
when row < 0 then @turtle_position[:row] == @canvas.size - 1
when column >= @canvas.size then @turtle_position[:column] == 0
when column < 0 then @turtle_position[:column] == @canvas.size - 1
end
end
end
module Canvas
class ASCII
def initialize(*symbols)
@symbols = symbols.flatten
end
def draw(matrix)
end
end
end
end

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

F.FFFF.F.FFFFF

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: turn_right
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-na4ppd/solution.rb:37:in `turn_right'
     # /tmp/d20151203-5272-na4ppd/spec.rb:276:in `block (4 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:270:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:270:in `block (3 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/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: 3.times { move }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-na4ppd/solution.rb:29:in `move'
     # /tmp/d20151203-5272-na4ppd/spec.rb:16:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:16:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:16:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-na4ppd/spec.rb:15: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-na4ppd/solution.rb:29:in `move'
     # /tmp/d20151203-5272-na4ppd/spec.rb:36:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:36:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:36:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-na4ppd/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: 2.times { move }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-na4ppd/solution.rb:29:in `move'
     # /tmp/d20151203-5272-na4ppd/spec.rb:47:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:47:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:47:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-na4ppd/spec.rb:46: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_right turns the orienation of the turtle right of where we stand
     Failure/Error: 4.times { turn_right }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-na4ppd/solution.rb:37:in `turn_right'
     # /tmp/d20151203-5272-na4ppd/spec.rb:64:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:64:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:64:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-na4ppd/spec.rb:63: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: expect(canvas).to eq [[0, 0], [1, 0]]
       
       expected: [[0, 0], [1, 0]]
            got: [[1, 0], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-na4ppd/spec.rb:93: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 Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: 2.times { turn_right }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-na4ppd/solution.rb:37:in `turn_right'
     # /tmp/d20151203-5272-na4ppd/spec.rb:118:in `block (5 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:118:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:118:in `block (4 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/spec.rb:114: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)>'

  8) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: 2.times { turn_right }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20151203-5272-na4ppd/solution.rb:37:in `turn_right'
     # /tmp/d20151203-5272-na4ppd/spec.rb:141:in `block (5 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/spec.rb:141:in `times'
     # /tmp/d20151203-5272-na4ppd/spec.rb:141:in `block (4 levels) in <top (required)>'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `instance_exec'
     # /tmp/d20151203-5272-na4ppd/solution.rb:15:in `draw'
     # /tmp/d20151203-5272-na4ppd/spec.rb:137: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::HTML renders the proper template
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-na4ppd/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-na4ppd/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)>'

  10) 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::HTML
     # /tmp/d20151203-5272-na4ppd/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-na4ppd/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)>'

  11) 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::HTML
     # /tmp/d20151203-5272-na4ppd/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-na4ppd/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.00769 seconds
14 examples, 11 failures

Failed examples:

rspec /tmp/d20151203-5272-na4ppd/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-na4ppd/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-na4ppd/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-na4ppd/spec.rb:45 # TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
rspec /tmp/d20151203-5272-na4ppd/spec.rb:55 # TurtleGraphics Turtle #draw #turn_right turns the orienation of the turtle right of where we stand
rspec /tmp/d20151203-5272-na4ppd/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-na4ppd/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-na4ppd/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-na4ppd/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-na4ppd/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-na4ppd/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:34 (преди около 9 години)

+module TurtleGraphics
+ class Turtle
+ attr_accessor :canvas
+
+ ORIENTATIONS = [:left, :up, :right, :down]
+
+ def initialize(rows, columns)
+ @canvas = Array.new(rows) { |row| Array.new(columns) { |column| 0 } }
+ @orientation = :right
+ @turtle_position = spawn_at
+ @canvas[@turtle_position[:row]][@turtle_position[:column]] += 1
+ end
+
+ def draw(special_way = nil, &block)
+ self.instance_exec &block
+
+ special_way ? special_way.draw(@canvas) : @canvas
+ end
+
+ def move
+ case @orientation
+ when :right then @turtle_position[:column] += 1
+ when :left then @turtle_position[:column] -= 1
+ when :up then @turtle_position[:row] -= 1
+ when :down then @turtle_position[:row] += 1
+ end
+
+ transfer_turtle
+ @canvas[@turtle_position[:row]][@turtle_position[:column]] += 1
+ end
+
+ def turn_left
+ @orientation = ORIENTATIONS[ORIENTATIONS.index(@orientation) - 1]
+ end
+
+ def turn_right
+ @orientation = ORIENTATIONS[ORIENTATIONS.index(@orientation) + 1]
+ end
+
+ def spawn_at(row = 0, column = 0)
+ {row: row, column: column}
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ private
+
+ def transfer_turtle
+ row = @turtle_position[:row]
+ column = @turtle_position[:column]
+ case
+ when row >= @canvas.size then @turtle_position[:row] == 0
+ when row < 0 then @turtle_position[:row] == @canvas.size - 1
+ when column >= @canvas.size then @turtle_position[:column] == 0
+ when column < 0 then @turtle_position[:column] == @canvas.size - 1
+ end
+ end
+ end
+
+ module Canvas
+ class ASCII
+ def initialize(*symbols)
+ @symbols = symbols.flatten
+ end
+
+ def draw(matrix)
+ end
+ end
+ end
+end