Решение на Шеста задача от Любомир Папазов

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

Към профила на Любомир Папазов

Резултати

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

Код

module TurtleGraphics
class Turtle
def initialize(rows, cols)
@orientations = {}
[0,1,2,3].map {|x| @orientations.merge!(x => %i(left up right down)[x])}
@orientation_index = 2
@rows = rows
@cols = cols
@matrix = Array.new(@rows) { Array.new(@cols,0)}
@position_x = 0
@position_y = 0
end
def draw(&block)
result = self.instance_eval &block if block_given?
@matrix
end
def turn_right
@orientation_index = (@orientation_index + 1 ) % 4
end
def turn_left
@orientation_index = (@orientation_index - 1 ) % 4
end
def look(orientation)
@orientation_index = @orientations.invert[orientation]
end
def spawn_at(row, column)
@position_x = row
@position_y = column
@is_spawned = true
end
def move
case @orientation_index
when 0
@position_y = (@position_y - 1) % @cols
when 1
@position_x = (@position_x - 1) % @rows
when 2
@position_y = (@position_y + 1) % @cols
when 3
@position_x = (@position_x + 1) % @rows
end
populate_matrix
end
private
def populate_matrix
@matrix[0][0] = 1 unless @is_spawned
@matrix[@position_x][@position_y] += 1
end
end
end

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

F.F....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, 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]
       
       (compared using ==)
     # /tmp/d20151203-5272-1a20jlj/spec.rb: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: expect(canvas[0]).to eq [2, 2]
       
       expected: [2, 2]
            got: [1, 2]
       
       (compared using ==)
     # /tmp/d20151203-5272-1a20jlj/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 #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: [[0, 0], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1a20jlj/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)>'

  4) 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-1a20jlj/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)>'

  5) 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-1a20jlj/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)>'

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

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

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

Failed examples:

rspec /tmp/d20151203-5272-1a20jlj/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-1a20jlj/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-1a20jlj/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-1a20jlj/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-1a20jlj/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-1a20jlj/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-1a20jlj/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-1a20jlj/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:36 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+
+ def initialize(rows, cols)
+ @orientations = {}
+ [0,1,2,3].map {|x| @orientations.merge!(x => %i(left up right down)[x])}
+ @orientation_index = 2
+ @rows = rows
+ @cols = cols
+ @matrix = Array.new(@rows) { Array.new(@cols,0)}
+ @position_x = 0
+ @position_y = 0
+ end
+
+ def draw(&block)
+ result = self.instance_eval &block if block_given?
+ @matrix
+ end
+
+ def turn_right
+ @orientation_index = (@orientation_index + 1 ) % 4
+ end
+
+ def turn_left
+ @orientation_index = (@orientation_index - 1 ) % 4
+ end
+
+ def look(orientation)
+ @orientation_index = @orientations.invert[orientation]
+ end
+
+ def spawn_at(row, column)
+ @position_x = row
+ @position_y = column
+ @is_spawned = true
+ end
+
+ def move
+ case @orientation_index
+ when 0
+ @position_y = (@position_y - 1) % @cols
+ when 1
+ @position_x = (@position_x - 1) % @rows
+ when 2
+ @position_y = (@position_y + 1) % @cols
+ when 3
+ @position_x = (@position_x + 1) % @rows
+ end
+ populate_matrix
+ end
+
+ private
+ def populate_matrix
+ @matrix[0][0] = 1 unless @is_spawned
+ @matrix[@position_x][@position_y] += 1
+ end
+ end
+end