Решение на Шеста задача от Кристъфър Коруев

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

Към профила на Кристъфър Коруев

Резултати

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

Код

class TurtleGraphics
class Turtle
attr_accessor :field, :turtle_pos
def initialize(height, width)
@height = height
@width = width
@field = Array.new(height) { Array.new(width, 0) }
@turtle_pos = TurtlePosition.new(0, 0)
@field[0][0] += 1
@count_of_moves = 1
end
def draw(*canvas, &block)
if block_given?
self.instance_eval(&block)
end
if canvas.empty?
@field
else
canvas.first.draw(@field, @count_of_moves, @height, @widtd)
end
end
def move
@turtle_pos.move(@height, @width)
@field[@turtle_pos.position.first.first][@turtle_pos.position.first.last] += 1
@count_of_moves += 1
end
def turn_right
@turtle_pos.turn_to_right
end
def turn_left
@turtle_pos.turn_to_left
end
def look(orientation)
:turtlePosition.position[POSITION_INDEX] = orientation
end
def spawn_at(row, column)
@turtle_pos.position[0] = [row, column]
end
end
class Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
@field = ""
@intensity_steps = generate_intensity
end
def draw(field, count_of_moves, height, width)
p field
field.each_with_index do |row, row_i|
row.each_with_index do |col, col_i|
intensity = (field[row_i][col_i].to_f / count_of_moves).round(1)
p intensity
sign = @symbols[((intensity * 10) % (@intensity_steps * 10))]
@field += sign
end
@field += "\n"
end
@field
end
private
def generate_intensity
parts = (1.0 / @symbols.length).round(1)
end
end
end
end
class TurtlePosition
attr_accessor :position
DIRS = [:left, :up, :right, :down]
DIRECTION_POS = 1
COORD_POSITION = 0
MOVE_TO_DIRECTION_COORD = {left: [0, -1],
right: [0, 1],
up: [-1, 0],
down: [1, 0]}
def initialize(height, width)
@position = [[height, width], :right]
end
def turn_to_right
turn_to(1)
end
def turn_to_left
turn_to(-1)
end
def move(height, width)
next_move = MOVE_TO_DIRECTION_COORD[@position.last]
x_coord = (next_move.first + @position.first.first) % height
y_coord = (next_move.last + @position.first.last) % width
@position[COORD_POSITION] = [x_coord, y_coord]
end
private
def turn_to(direction)
dirs_index = DIRS.index(@position.last)
@position[DIRECTION_POS] = DIRS[(dirs_index + direction) % DIRS.length]
end
end

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

F......FF[[3, 2, 0], [1, 1, 0], [0, 0, 0]]
0.4
0.3
0.0
0.1
0.1
0.0
0.0
0.0
0.0
F[[3, 2, 0], [1, 1, 0], [0, 0, 0]]
0.4
0.3
0.0
0.1
0.1
0.0
0.0
0.0
0.0
FFFF

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, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 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, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 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, 0, 0], [0, 0, 0, 0, 0, 0, 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, 0, 0, 1, 1, 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, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 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, 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]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1tgbu2/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 #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: [[1, 0], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1tgbu2/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)>'

  3) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: look :down
     NoMethodError:
       undefined method `position' for :turtlePosition:Symbol
     # /tmp/d20151203-5272-1tgbu2/solution.rb:41:in `look'
     # /tmp/d20151203-5272-1tgbu2/spec.rb:101:in `block (6 levels) in <top (required)>'
     # /tmp/d20151203-5272-1tgbu2/solution.rb:16:in `instance_eval'
     # /tmp/d20151203-5272-1tgbu2/solution.rb:16:in `draw'
     # /tmp/d20151203-5272-1tgbu2/spec.rb:4:in `create_canvas'
     # /tmp/d20151203-5272-1tgbu2/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)>'

  4) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
       
       expected: "320\n110\n000"
            got: "100\n110\n000"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -320
       +100
        110
        000
     # /tmp/d20151203-5272-1tgbu2/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)>'

  5) 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: "ozz\nooz\nzzz"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -ttz
       +ozz
        ooz
        zzz
     # /tmp/d20151203-5272-1tgbu2/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)>'

  6) 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-1tgbu2/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1tgbu2/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::HTML
     # /tmp/d20151203-5272-1tgbu2/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1tgbu2/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::HTML
     # /tmp/d20151203-5272-1tgbu2/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1tgbu2/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.01549 seconds
14 examples, 8 failures

Failed examples:

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

+class TurtleGraphics
+ class Turtle
+ attr_accessor :field, :turtle_pos
+
+ def initialize(height, width)
+ @height = height
+ @width = width
+ @field = Array.new(height) { Array.new(width, 0) }
+ @turtle_pos = TurtlePosition.new(0, 0)
+ @field[0][0] += 1
+ @count_of_moves = 1
+ end
+
+ def draw(*canvas, &block)
+ if block_given?
+ self.instance_eval(&block)
+ end
+
+ if canvas.empty?
+ @field
+ else
+ canvas.first.draw(@field, @count_of_moves, @height, @widtd)
+ end
+ end
+
+ def move
+ @turtle_pos.move(@height, @width)
+ @field[@turtle_pos.position.first.first][@turtle_pos.position.first.last] += 1
+ @count_of_moves += 1
+ end
+
+ def turn_right
+ @turtle_pos.turn_to_right
+ end
+
+ def turn_left
+ @turtle_pos.turn_to_left
+ end
+
+ def look(orientation)
+ :turtlePosition.position[POSITION_INDEX] = orientation
+ end
+
+ def spawn_at(row, column)
+ @turtle_pos.position[0] = [row, column]
+ end
+ end
+
+ class Canvas
+ class ASCII
+ def initialize(symbols)
+ @symbols = symbols
+ @field = ""
+ @intensity_steps = generate_intensity
+ end
+
+ def draw(field, count_of_moves, height, width)
+ p field
+ field.each_with_index do |row, row_i|
+ row.each_with_index do |col, col_i|
+ intensity = (field[row_i][col_i].to_f / count_of_moves).round(1)
+ p intensity
+ sign = @symbols[((intensity * 10) % (@intensity_steps * 10))]
+ @field += sign
+ end
+ @field += "\n"
+ end
+ @field
+ end
+
+ private
+
+ def generate_intensity
+ parts = (1.0 / @symbols.length).round(1)
+ end
+ end
+ end
+end
+
+class TurtlePosition
+ attr_accessor :position
+ DIRS = [:left, :up, :right, :down]
+ DIRECTION_POS = 1
+ COORD_POSITION = 0
+ MOVE_TO_DIRECTION_COORD = {left: [0, -1],
+ right: [0, 1],
+ up: [-1, 0],
+ down: [1, 0]}
+
+ def initialize(height, width)
+ @position = [[height, width], :right]
+ end
+
+ def turn_to_right
+ turn_to(1)
+ end
+
+ def turn_to_left
+ turn_to(-1)
+ end
+
+ def move(height, width)
+ next_move = MOVE_TO_DIRECTION_COORD[@position.last]
+ x_coord = (next_move.first + @position.first.first) % height
+ y_coord = (next_move.last + @position.first.last) % width
+ @position[COORD_POSITION] = [x_coord, y_coord]
+ end
+
+ private
+
+ def turn_to(direction)
+ dirs_index = DIRS.index(@position.last)
+ @position[DIRECTION_POS] = DIRS[(dirs_index + direction) % DIRS.length]
+ end
+end