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

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

Към профила на Мария Османлиева

Резултати

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

Код

module TurtleGraphics
class Turtle
def initialize(height, width)
@x = 0
@y = 0
@direction = :right
@width = width
@height = height
@table = Array.new(@height, 0) { Array.new(@width, 0) }
draw_in_table(@x, @y)
end
def draw(&block)
self.instance_eval(&block)
@table
end
private
def draw_in_table(x,y)
@table[@x][@y] = @table[@x][@y] + 1
end
def move
if @y >= @height then @y = 0 end
if @x >= @width then @x = 0 end
case @direction
when :left then @y = @y + 1
when :right then @y = @y - 1
when :up then @x = @x - 1
when :down then @x = @x + 1
end
draw_in_table(@x, @y)
end
def turn_right
case @direction
when :right then @direction = :down
when :down then @direction = :left
when :left then @direction = :up
when :up then @direction = :right
end
end
def turn_left
case @direction
when :right then @direction = :up
when :down then @direction = :right
when :left then @direction = :down
when :up then @direction = :left
end
end
def spawn_at(row, column)
@x = row
@y = column
end
def look(orientation)
@direction = orientation
end
end
end

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

F.FF...F.FFFFF

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: expect(canvas).to eq [
       
       expected: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 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b:284: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-11kdeqy/solution.rb:23:in `draw_in_table'
     # /tmp/d20151203-5272-11kdeqy/solution.rb:36:in `move'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:16:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:16:in `times'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:16:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-11kdeqy/solution.rb:16:in `instance_eval'
     # /tmp/d20151203-5272-11kdeqy/solution.rb:16:in `draw'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-11kdeqy/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-11kdeqy/solution.rb:23:in `draw_in_table'
     # /tmp/d20151203-5272-11kdeqy/solution.rb:36:in `move'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:36:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:36:in `times'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:36:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-11kdeqy/solution.rb:16:in `instance_eval'
     # /tmp/d20151203-5272-11kdeqy/solution.rb:16:in `draw'
     # /tmp/d20151203-5272-11kdeqy/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-11kdeqy/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 #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-11kdeqy/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)>'

  5) 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-11kdeqy/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)>'

  6) 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-11kdeqy/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)>'

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

  8) 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-11kdeqy/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-11kdeqy/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)>'

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

Failed examples:

rspec /tmp/d20151203-5272-11kdeqy/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-11kdeqy/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-11kdeqy/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-11kdeqy/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-11kdeqy/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-11kdeqy/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-11kdeqy/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-11kdeqy/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-11kdeqy/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Мария обнови решението на 02.12.2015 14:54 (преди над 8 години)

+module TurtleGraphics
+
+ class Turtle
+
+ def initialize(height, width)
+ @x = 0
+ @y = 0
+ @direction = :right
+ @width = width
+ @height = height
+ @table = Array.new(@height, 0) { Array.new(@width, 0) }
+ draw_in_table(@x, @y)
+ end
+
+ def draw(&block)
+ self.instance_eval(&block)
+ @table
+ end
+
+ def draw_in_table(x,y)
+ @table[@x][@y] = @table[@x][@y] + 1
+ end
+
+ def move
+ if @y >= @height then @y = 0 end
+ if @x >= @width then @x = 0 end
+
+ case @direction
+ when :left then @y = @y + 1
+ when :right then @y = @y - 1
+ when :up then @x = @x - 1
+ when :down then @x = @x + 1
+ end
+ draw_in_table(@x, @y)
+ end
+
+ def turn_right
+ case @direction
+ when :right then @direction = :down
+ when :down then @direction = :left
+ when :left then @direction = :up
+ when :up then @direction = :right
+ end
+ end
+
+ def turn_left
+ case @direction
+ when :right then @direction = :up
+ when :down then @direction = :right
+ when :left then @direction = :down
+ when :up then @direction = :left
+ end
+ end
+
+ def spawn_at(row, column)
+ @x = row
+ @y = column
+ end
+
+ def look(orientation)
+ @direction = orientation
+ end
+ end
+end

Мария обнови решението на 02.12.2015 16:03 (преди над 8 години)

module TurtleGraphics
class Turtle
def initialize(height, width)
@x = 0
@y = 0
@direction = :right
@width = width
@height = height
@table = Array.new(@height, 0) { Array.new(@width, 0) }
draw_in_table(@x, @y)
end
def draw(&block)
self.instance_eval(&block)
@table
end
+ private
+
def draw_in_table(x,y)
@table[@x][@y] = @table[@x][@y] + 1
end
def move
if @y >= @height then @y = 0 end
if @x >= @width then @x = 0 end
case @direction
when :left then @y = @y + 1
when :right then @y = @y - 1
when :up then @x = @x - 1
when :down then @x = @x + 1
end
draw_in_table(@x, @y)
end
def turn_right
case @direction
when :right then @direction = :down
when :down then @direction = :left
when :left then @direction = :up
when :up then @direction = :right
end
end
def turn_left
case @direction
when :right then @direction = :up
when :down then @direction = :right
when :left then @direction = :down
when :up then @direction = :left
end
end
def spawn_at(row, column)
@x = row
@y = column
end
def look(orientation)
@direction = orientation
end
end
end

Здравей,

Кодът ти е на вълни, няма как да не ти взема точка за стил. Предполагам, че част от нещата са от неправилно настроен текстов редактор. Виж тази тема във форума, ако използваш Sublime Text.

Прегледай и нашето решение за идеи, и за другото домашно започни по-рано.