Решение на Шеста задача от Димитър Ангелов

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

Към профила на Димитър Ангелов

Резултати

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

Код

class TurtleGraphics
class TurtleGraphics::Turtle
DIRECTIONS = {
:left => [1, 0],
:up => [1, 1],
:right => [0, 1],
:down => [0, 0]
}
def initialize rows, columns
@rows = rows
@columns = columns
@draw = []
@orientation = :right
@current = [0, 0]
@type = :array
end
def draw type = :array, &block
@draw = Array.new(@rows) { Array.new(@columns, 0) }
@type = type
@draw[@current[0]][@current[1]] = 1
self.instance_eval &block if block_given?
what_to_draw
end
def move
case @orientation
when :left then @draw[@current[0]][@current[1] - 1] += 1
@current[1] -= 1
when :up then @draw[@current[0] - 1][@current[1]] += 1
@current[0] -= 1
when :right then @draw[@current[0]][@current[1] + 1] += 1
@current[1] += 1
when :down then @draw[@current[0] + 1][@current[1]] += 1
@current[0] += 1
end
@deck
end
def turn_left
orientation_index = DIRECTIONS.keys.index @orientation
@orientation = DIRECTIONS.keys[orientation_index - 1]
end
def turn_right
orientation_index = DIRECTIONS.keys.index @orientation
@orientation = DIRECTIONS.keys[(orientation_index + 1) % 4]
end
def spawn_at row, column
@draw[@current[0]][@current[1]] = 0
@current = [row, column]
@draw[@current[0]][@current[1]] = 1
end
def look orientation
@orientation = orientation
end
def what_to_draw
if @type == :array then @draw
elsif @type.kind_of? TurtleGraphics::Canvas::ASCII then draw_ascii
else draw_html
end
end
def draw_ascii
max, count = 0, 1
how_many = @type.string_array.size - 1
step = (1 / how_many).to_r
@deck.flatten.each{|x| if x > max then max = x end}
@deck.flatten!.map{|x| while x / max < count * step do count += 1 end
if x == 0 then x = @type.string_array[0]
else x = @type.string_array[count]
end
count = 1 }
end
def draw_html
end
end
class Canvas
class TurtleGraphics::Canvas::ASCII
def initialize string_array
@string_array = string_array
end
attr_reader :string_array
def intensity
end
end
class TurtleGraphics::Canvas::HTML
def initialize size
@size = size
end
end
end
end

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

..FFF....FFFFF

Failures:

  1) 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-5s8z6/solution.rb:30:in `move'
     # /tmp/d20151203-5272-5s8z6/spec.rb:16:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-5s8z6/spec.rb:16:in `times'
     # /tmp/d20151203-5272-5s8z6/spec.rb:16:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-5s8z6/solution.rb:21:in `instance_eval'
     # /tmp/d20151203-5272-5s8z6/solution.rb:21:in `draw'
     # /tmp/d20151203-5272-5s8z6/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-5s8z6/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)>'

  2) 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-5s8z6/solution.rb:32:in `move'
     # /tmp/d20151203-5272-5s8z6/spec.rb:36:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-5s8z6/spec.rb:36:in `times'
     # /tmp/d20151203-5272-5s8z6/spec.rb:36:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-5s8z6/solution.rb:21:in `instance_eval'
     # /tmp/d20151203-5272-5s8z6/solution.rb:21:in `draw'
     # /tmp/d20151203-5272-5s8z6/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-5s8z6/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)>'

  3) 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-5s8z6/solution.rb:30:in `move'
     # /tmp/d20151203-5272-5s8z6/spec.rb:47:in `block (7 levels) in <top (required)>'
     # /tmp/d20151203-5272-5s8z6/spec.rb:47:in `times'
     # /tmp/d20151203-5272-5s8z6/spec.rb:47:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-5s8z6/solution.rb:21:in `instance_eval'
     # /tmp/d20151203-5272-5s8z6/solution.rb:21:in `draw'
     # /tmp/d20151203-5272-5s8z6/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-5s8z6/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)>'

  4) 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 `flatten' for nil:NilClass
     # /tmp/d20151203-5272-5s8z6/solution.rb:63:in `draw_ascii'
     # /tmp/d20151203-5272-5s8z6/solution.rb:55:in `what_to_draw'
     # /tmp/d20151203-5272-5s8z6/solution.rb:22:in `draw'
     # /tmp/d20151203-5272-5s8z6/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)>'

  5) 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 `flatten' for nil:NilClass
     # /tmp/d20151203-5272-5s8z6/solution.rb:63:in `draw_ascii'
     # /tmp/d20151203-5272-5s8z6/solution.rb:55:in `what_to_draw'
     # /tmp/d20151203-5272-5s8z6/solution.rb:22:in `draw'
     # /tmp/d20151203-5272-5s8z6/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)>'

  6) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to eq <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-5s8z6/spec.rb:171: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: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-5s8z6/spec.rb:220: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: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-5s8z6/spec.rb:242: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.00857 seconds
14 examples, 8 failures

Failed examples:

rspec /tmp/d20151203-5272-5s8z6/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-5s8z6/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-5s8z6/spec.rb:45 # TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
rspec /tmp/d20151203-5272-5s8z6/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-5s8z6/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-5s8z6/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-5s8z6/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-5s8z6/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Димитър обнови решението на 02.12.2015 13:39 (преди над 8 години)

+class TurtleGraphics
+ class TurtleGraphics::Turtle
+ DIRECTIONS = {
+ :left => [1, 0],
+ :up => [1, 1],
+ :right => [0, 1],
+ :down => [0, 0]
+ }
+ def initialize rows, columns
+ @rows = rows
+ @columns = columns
+ @draw = []
+ @orientation = :right
+ @current = [0, 0]
+ @type = :array
+ end
+ def draw type = :array, &block
+ @draw = Array.new(@rows) { Array.new(@columns, 0) }
+ @type = type
+ @draw[@current[0]][@current[1]] = 1
+ self.instance_eval &block if block_given?
+ what_to_draw
+ end
+ def move
+ case @orientation
+ when :left then @draw[@current[0]][@current[1] - 1] += 1
+ @current[1] -= 1
+ when :up then @draw[@current[0] - 1][@current[1]] += 1
+ @current[0] -= 1
+ when :right then @draw[@current[0]][@current[1] + 1] += 1
+ @current[1] += 1
+ when :down then @draw[@current[0] + 1][@current[1]] += 1
+ @current[0] += 1
+ end
+ @deck
+ end
+ def turn_left
+ orientation_index = DIRECTIONS.keys.index @orientation
+ @orientation = DIRECTIONS.keys[orientation_index - 1]
+ end
+ def turn_right
+ orientation_index = DIRECTIONS.keys.index @orientation
+ @orientation = DIRECTIONS.keys[(orientation_index + 1) % 4]
+ end
+ def spawn_at row, column
+ @draw[@current[0]][@current[1]] = 0
+ @current = [row, column]
+ @draw[@current[0]][@current[1]] = 1
+ end
+ def look orientation
+ @orientation = orientation
+ end
+ def what_to_draw
+ if @type == :array then @draw
+ elsif @type.kind_of? TurtleGraphics::Canvas::ASCII then draw_ascii
+ else draw_html
+ end
+ end
+ def draw_ascii
+ max, count = 0, 1
+ how_many = @type.string_array.size - 1
+ step = (1 / how_many).to_r
+ @deck.flatten.each{|x| if x > max then max = x end}
+ @deck.flatten!.map{|x| while x / max < count * step do count += 1 end
+ if x == 0 then x = @type.string_array[0]
+ else x = @type.string_array[count]
+ end
+ count = 1 }
+ end
+ def draw_html
+ end
+ end
+ class Canvas
+ class TurtleGraphics::Canvas::ASCII
+ def initialize string_array
+ @string_array = string_array
+ end
+ attr_reader :string_array
+ def intensity
+ end
+ end
+ class TurtleGraphics::Canvas::HTML
+ def initialize size
+ @size = size
+ end
+ end
+ end
+end

Здравей,

Имаш сериозни стилови проблеми:

  • Индентация
  • Нямаш нито един празен ред. Дори между методите.
  • Inline while ... do ... end
  • Дефинираш аргументи на методи без скоби.
  • Използваш { ... } за многоредови блокове.
class Canvas
    class TurtleGraphics::Canvas::ASCII

Последното не трябва да има TurtleGraphics::Canvas::

Прегледай пак ръководството за стил и виж и нашето решение на задачата.