Решение на Шеста задача от Александрина Каракехайова

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

Към профила на Александрина Каракехайова

Резултати

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

Код

module TurtleGraphics
class Turtle
def initialize(height, width)
@height = height
@width = width
@canvas = Array.new(height).map! {Array.new(width, 0)}
@canvas[0][0] = 1
@current_position = [0,0]
@orientation = :right
self
end
def define_which_sign(intensity, coefficient, ascii, drawing)
count = 1
length = ascii.length
while count < length
previous_sign = (count - 1) * coefficient
next_sign = (count + 1) * coefficient
if intensity >= previous_sign and intensity < next_sign
drawing << ascii[count]
count += 1
end
count += 1
end
end
def draw_ascii(length, ascii, &block)
self.instance_eval(&block)
drawing, max_intensity = "", (@canvas.max { |a,b| a.max <=> b.max }).max
@canvas.each do |row|
row.each do |element|
intensity, coefficient = (element.to_f) / max_intensity, 1.0 /(length - 1)
pick_sign(intensity, coefficient, ascii, drawing)
end
drawing << "\n"
end
drawing.chop
end
def pick_sign(intensity, coefficient, ascii, drawing)
if intensity == 0
drawing << ascii[0]
else define_which_sign(intensity, coefficient, ascii, drawing)
end
end
def draw(ascii = nil, &block)
if ascii
draw_ascii(ascii.length, ascii, &block)
else self.instance_eval(&block)
@canvas
end
end
def move
moves = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
new_row = (@current_position[0] + moves[@orientation][0]) % @height
new_column = (@current_position[1] + moves[@orientation][1]) % @width
@canvas[new_row][new_column] += 1
@current_position = [new_row, new_column]
end
def turn_left
case @orientation
when :up then @orientation = :left
when :down then @orientation = :right
when :left then @orientation = :down
when :right then @orientation = :up
end
end
def turn_right
case @orientation
when :up then @orientation = :right
when :down then @orientation = :left
when :left then @orientation = :up
when :right then @orientation = :down
end
end
def spawn_at(row, column)
@canvas[0][0] = 0
@current_position[0] = row
@current_position[1] = column
@canvas[row][column] += 1
end
def look(orientation)
@orientation = orientation
end
end
module Canvas
class ASCII
attr_reader :length
def initialize(list_of_symbols)
@symbols = Array.new (list_of_symbols)
@length = list_of_symbols.length
end
def [] (number)
@symbols[number]
end
end
class HTML
def initialize(size)
end
end
end
end

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

..........FFFF

Failures:

  1) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
       
       expected: "ttz\nooz\nzzz"
            got: "toz\nooz\nzzz"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -ttz
       +toz
        ooz
        zzz
     # /tmp/d20151203-5272-uluenf/spec.rb:151: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)>'

  2) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: TurtleGraphics::Turtle.new(3, 3).draw(html_canvas, &block)
     NoMethodError:
       undefined method `length' for #<TurtleGraphics::Canvas::HTML:0x007f712caa68c0>
     # /tmp/d20151203-5272-uluenf/solution.rb:49:in `draw'
     # /tmp/d20151203-5272-uluenf/spec.rb:162:in `create_html_canvas'
     # /tmp/d20151203-5272-uluenf/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)>'

  3) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: TurtleGraphics::Turtle.new(3, 3).draw(html_canvas, &block)
     NoMethodError:
       undefined method `length' for #<TurtleGraphics::Canvas::HTML:0x007f712ca934a0>
     # /tmp/d20151203-5272-uluenf/solution.rb:49:in `draw'
     # /tmp/d20151203-5272-uluenf/spec.rb:162:in `create_html_canvas'
     # /tmp/d20151203-5272-uluenf/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)>'

  4) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: TurtleGraphics::Turtle.new(3, 3).draw(html_canvas, &block)
     NoMethodError:
       undefined method `length' for #<TurtleGraphics::Canvas::HTML:0x007f712ca8afa8>
     # /tmp/d20151203-5272-uluenf/solution.rb:49:in `draw'
     # /tmp/d20151203-5272-uluenf/spec.rb:162:in `create_html_canvas'
     # /tmp/d20151203-5272-uluenf/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.01058 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20151203-5272-uluenf/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-uluenf/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-uluenf/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-uluenf/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Александрина обнови решението на 02.12.2015 16:55 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+ def initialize(height, width)
+ @height = height
+ @width = width
+ @canvas = Array.new(height).map! {Array.new(width, 0)}
+ @canvas[0][0] = 1
+ @current_position = [0,0]
+ @orientation = :right
+ self
+ end
+
+ def define_which_sign(intensity, coefficient, ascii, drawing)
+ count = 1
+ length = ascii.length
+ while count < length
+ previous_sign = (count - 1) * coefficient
+ next_sign = (count + 1) * coefficient
+ if intensity >= previous_sign and intensity < next_sign
+ drawing << ascii[count]
+ count += 1
+ end
+ count += 1
+ end
+ end
+
+ def draw_ascii(length, ascii, &block)
+ self.instance_eval(&block)
+ drawing, max_intensity = "", (@canvas.max { |a,b| a.max <=> b.max }).max
+ @canvas.each do |row|
+ row.each do |element|
+ intensity, coefficient = (element.to_f) / max_intensity, 1.0 /(length - 1)
+ pick_sign(intensity, coefficient, ascii, drawing)
+ end
+ drawing << "\n"
+ end
+ drawing.chop
+ end
+
+ def pick_sign(intensity, coefficient, ascii, drawing)
+ if intensity == 0
+ drawing << ascii[0]
+ else define_which_sign(intensity, coefficient, ascii, drawing)
+ end
+ end
+
+ def draw(ascii = nil, &block)
+ if ascii
+ draw_ascii(ascii.length, ascii, &block)
+ else self.instance_eval(&block)
+ @canvas
+ end
+ end
+
+ def move
+ moves = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
+ new_row = (@current_position[0] + moves[@orientation][0]) % @height
+ new_column = (@current_position[1] + moves[@orientation][1]) % @width
+ @canvas[new_row][new_column] += 1
+ @current_position = [new_row, new_column]
+ end
+
+ def turn_left
+ case @orientation
+ when :up then @orientation = :left
+ when :down then @orientation = :right
+ when :left then @orientation = :down
+ when :right then @orientation = :up
+ end
+ end
+
+ def turn_right
+ case @orientation
+ when :up then @orientation = :right
+ when :down then @orientation = :left
+ when :left then @orientation = :up
+ when :right then @orientation = :down
+ end
+ end
+
+ def spawn_at(row, column)
+ @canvas[0][0] = 0
+ @current_position[0] = row
+ @current_position[1] = column
+ @canvas[row][column] += 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end
+
+ module Canvas
+ class ASCII
+ attr_reader :length
+ def initialize(list_of_symbols)
+ @symbols = Array.new (list_of_symbols)
+ @length = list_of_symbols.length
+ end
+
+ def [] (number)
+ @symbols[number]
+ end
+ end
+
+ class HTML
+ def initialize(size)
+ end
+ end
+ end
+end

Здравей,

Взимам ти точка заради методите draw_ascii и define_which_sign:

  • Ненужно сложни са. define_which_sign може да се замени от този ред код: (intensity * (@symbols.size - 1)).ceil
  • Не е хубаво да се намират в Turtle, а в класа, свързан с рендериране на ASCII арт.

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