Решение на Шеста задача от Веселин Стоянов

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

Към профила на Веселин Стоянов

Резултати

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

Код

module TurtleGraphics
class Turtle
attr :canvas, :canvas_size_x, :canvas_size_y, :position, :orientation
attr :steps_counter, :max_steps
def initialize(canvas_size_x, canvas_size_y)
@canvas_size_x, @canvas_size_y = canvas_size_x, canvas_size_y
@canvas, row, @steps_counter, @max_steps = [], [], 0, 0
0..canvas_size_x.each { row.push(0) }
0..canvas_size_y.each { @canvas.push(row) }
end
def draw(&block)
instance_eval &block
end
def move
case @orientation
when :left then @position[0] -= 1
when :right then @position[0] += 1
when :up then @position[1] -= 1
when :down then @position[1] += 1
end
check_boundaries
@canvas[@position[1]][@position[0]] += 1
@steps_counter += 1
@max_steps = @steps_counter if @steps_counter > @max_steps
end
def check_boundaries
position[0] = 0 if @position[0] < 0 or @position[0] > @canvas_size_x
position[1] = 0 if @position[1] < 0 or @position[1] > @canvas_size_y
end
def turn_left
case @orientation
when :left then @orientation = :down
when :right then @orientation = :up
when :up then @orientation = :left
when :down then @orientation = :right
end
end
def turn_right
case @orientation
when :left then @orientation = :up
when :right then @orientation = :down
when :up then @orientation = :right
when :down then @orientation = :left
end
end
def spawn_at(row, column)
@position = [row, column]
end
def look(orientation)
if [:left, :right, :up, :down].include?(orientation)
@orientation = orientation
end
end
end
module Canvas
class ASCII
attr :symbols
def initialize(*symbols)
@symbols = symbols
end
def print_canvas(canvas, steps_counter, max_steps)
canvas.each do |row|
row.each do |counter|
end
end
end
def print_char(counter, steps_counter, max_steps)
if counter == 0
symbols.first
else
end
end
end
end
end

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

FFFFFFFFFFFFFF

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: canvas = TurtleGraphics::Turtle.new(20, 20).draw do
     NoMethodError:
       undefined method `each' for 20:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:265:in `new'
     # /tmp/d20151203-5272-idu777/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 marks where the turtle has moved
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/spec.rb:10: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 moves the turtle to the start of row or column when we are at its end
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/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)>'

  4) TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/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)>'

  5) TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/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)>'

  6) TurtleGraphics Turtle #draw #turn_right turns the orienation of the turtle right of where we stand
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/spec.rb:56: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 #turn_left turns the orienation of the turtle left of where we stand
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/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)>'

  8) TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/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)>'

  9) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: TurtleGraphics::Turtle.new(2, 2).draw(&block)
     NoMethodError:
       undefined method `each' for 2:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `new'
     # /tmp/d20151203-5272-idu777/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-idu777/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)>'

  10) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: ascii = TurtleGraphics::Turtle.new(3, 3).draw(ascii_canvas) do
     NoMethodError:
       undefined method `each' for 3:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:114:in `new'
     # /tmp/d20151203-5272-idu777/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)>'

  11) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: ascii = TurtleGraphics::Turtle.new(3, 3).draw(ascii_canvas) do
     NoMethodError:
       undefined method `each' for 3:Fixnum
     # /tmp/d20151203-5272-idu777/solution.rb:10:in `initialize'
     # /tmp/d20151203-5272-idu777/spec.rb:137:in `new'
     # /tmp/d20151203-5272-idu777/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)>'

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

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

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

Failed examples:

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

+module TurtleGraphics
+ class Turtle
+ attr :canvas, :canvas_size_x, :canvas_size_y, :position, :orientation
+ attr :steps_counter, :max_steps
+
+ def initialize(canvas_size_x, canvas_size_y)
+ @canvas_size_x, @canvas_size_y = canvas_size_x, canvas_size_y
+ @canvas, row, @steps_counter, @max_steps = [], [], 0, 0
+
+ 0..canvas_size_x.each { row.push(0) }
+ 0..canvas_size_y.each { @canvas.push(row) }
+ end
+
+ def draw(&block)
+ instance_eval &block
+ end
+
+ def move
+ case @orientation
+ when :left then @position[0] -= 1
+ when :right then @position[0] += 1
+ when :up then @position[1] -= 1
+ when :down then @position[1] += 1
+ end
+ check_boundaries
+ @canvas[@position[1]][@position[0]] += 1
+ @steps_counter += 1
+ @max_steps = @steps_counter if @steps_counter > @max_steps
+ end
+
+ def check_boundaries
+ position[0] = 0 if @position[0] < 0 or @position[0] > @canvas_size_x
+ position[1] = 0 if @position[1] < 0 or @position[1] > @canvas_size_y
+ end
+
+ def turn_left
+ case @orientation
+ when :left then @orientation = :down
+ when :right then @orientation = :up
+ when :up then @orientation = :left
+ when :down then @orientation = :right
+ end
+ end
+
+ def turn_right
+ case @orientation
+ when :left then @orientation = :up
+ when :right then @orientation = :down
+ when :up then @orientation = :right
+ when :down then @orientation = :left
+ end
+ end
+
+ def spawn_at(row, column)
+ @position = [row, column]
+ end
+
+ def look(orientation)
+ if [:left, :right, :up, :down].include?(orientation)
+ @orientation = orientation
+ end
+ end
+ end
+
+ module Canvas
+ class ASCII
+ attr :symbols
+
+ def initialize(*symbols)
+ @symbols = symbols
+ end
+
+ def print_canvas(canvas, steps_counter, max_steps)
+ canvas.each do |row|
+ row.each do |counter|
+
+ end
+ end
+ end
+
+ def print_char(counter, steps_counter, max_steps)
+ if counter == 0
+ symbols.first
+ else
+
+ end
+ end
+ end
+ end
+end