Решение на Шеста задача от Кристиян Тодоров

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

Към профила на Кристиян Тодоров

Резултати

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

Код

class TurtleGraphics
class Turtle
end
class Canvas
end
end
class TurtleGraphics::Turtle
def initialize(x, y)
@max_x = x
@max_y = y
@matrix = Array.new(@max_x) { Array.new(@max_y, 0) }
@orientation = 0 # directions are measured with degrees
end
def spawn_at(row, column)
@x = row
@y = column
@matrix[@x][@y] += 1
end
def fix_position
@x = 0 if @x == @max_x
@x = @max_x - 1 if @x < 0
@y = 0 if @y == @max_y
@y = @max_y - 1 if @y < 0
end
def all_steps
@matrix.flatten.reduce(:+)
end
def draw(ascii_canvas = nil, &block)
instance_eval &block
if ascii_canvas
ascii_canvas.steps = all_steps
ascii_canvas.range = max_intensity / ascii_canvas.symbols.size.to_f
end
@matrix
end
def move
unless instance_variable_defined? :@x
spawn_at(0, 0)
end
case @orientation
when 0 then @y += 1
when 90 then @x += 1
when 180 then @y -= 1
when 270 then @x -= 1
end
fix_position
@matrix[@x][@y] += 1
end
def turn_left
@orientation -= 90
if @orientation == -90
@orientation = 270
end
end
def look(orientation)
directions = {left: 180, right: 0, up: 90, down: 270}
@orientation = directions[orientation]
end
def max_intensity
@matrix.flatten.max
end
def turn_right
@orientation += 90
if @orientation == 360
@orientation = 0
end
end
end
class TurtleGraphics::Canvas::ASCII
attr_accessor :steps, :range, :symbols
def initialize(symbols)
@steps = 0
@range
@symbols = symbols
end
end

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

.........FFFFF

Failures:

  1) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [[3, 2, 0], [1, 1, 0], [0, 0, 0]]:Array
     # /tmp/d20151203-5272-1j4biax/spec.rb:128: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::ASCII can render with a different number of symbols
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [[3, 2, 0], [1, 1, 0], [0, 0, 0]]:Array
     # /tmp/d20151203-5272-1j4biax/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)>'

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

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

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

Failed examples:

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

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

Кристиян обнови решението на 02.12.2015 14:35 (преди над 9 години)

+class TurtleGraphics
+ class Turtle
+ end
+
+ class Canvas
+ end
+end
+
+class TurtleGraphics::Turtle
+ def initialize(x, y)
+ @max_x = x
+ @max_y = y
+ @matrix = Array.new(@max_x) { Array.new(@max_y, 0) }
+ @orientation = 0 # directions are measured with degrees
+ end
+
+ def spawn_at(row, column)
+ @x = row
+ @y = column
+ @matrix[@x][@y] += 1
+ end
+
+ def fix_position
+ @x = 0 if @x == @max_x
+ @x = @max_x - 1 if @x < 0
+ @y = 0 if @y == @max_y
+ @y = @max_y - 1 if @y < 0
+ end
+
+ def all_steps
+ @matrix.flatten.reduce(:+)
+ end
+
+ def draw(ascii_canvas = nil, &block)
+ instance_eval &block
+ if ascii_canvas
+ ascii_canvas.steps = all_steps
+ ascii_canvas.range = max_intensity / ascii_canvas.symbols.size.to_f
+ end
+ @matrix
+ end
+
+ def move
+ unless instance_variable_defined? :@x
+ spawn_at(0, 0)
+ end
+ case @orientation
+ when 0 then @y += 1
+ when 90 then @x += 1
+ when 180 then @y -= 1
+ when 270 then @x -= 1
+ end
+ fix_position
+ @matrix[@x][@y] += 1
+ end
+
+ def turn_left
+ @orientation -= 90
+ if @orientation == -90
+ @orientation = 270
+ end
+ end
+
+ def look(orientation)
+ directions = {left: 180, right: 0, up: 90, down: 270}
+ @orientation = directions[orientation]
+ end
+
+ def max_intensity
+ @matrix.flatten.max
+ end
+
+ def turn_right
+ @orientation += 90
+ if @orientation == 360
+ @orientation = 0
+ end
+ end
+end
+
+class TurtleGraphics::Canvas::ASCII
+ attr_accessor :steps, :range, :symbols
+
+ def initialize(symbols)
+ @steps = 0
+ @range
+ @symbols = symbols
+ end
+end