Решение на Шеста задача от София Петрова

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

Към профила на София Петрова

Резултати

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

Код

module TurtleGraphics
require 'matrix'
class Turtle
ORIENTATIONS = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows = rows
@columns = columns
@orientation = :right
@canvas = Matrix.zero(rows,columns).to_a
@current_position = {row: 0, column: 0}
@canvas[0][0] = 1
end
def draw(renderer = nil, &block)
self.instance_exec &block
if renderer.nil?
@canvas
else
renderer.render(@canvas)
end
end
def move
case @orientation
when :right
@current_position[:column] += 1
when :left
@current_position[:column] -= 1
when :up
@current_position[:row] -= 1
when :down
@current_position[:row] += 1
end
outliner(@current_position[:row], @current_position[:column])
end
def turn_left
current_orientation = ORIENTATIONS.index(@orientation)
@orientation = ORIENTATIONS[current_orientation - 1]
end
def turn_right
current_orientation = ORIENTATIONS.index(@orientation)
@orientation = ORIENTATIONS[(current_orientation + 1) % 4]
end
def look(orientation)
@orientation = orientation
end
def spawn_at(row = 0, column = 0)
@canvas[0][0] = 0
@canvas[row][column] = 1
@current_position = {row: row, column: column}
end
private
def outliner(current_row, current_column)
current_row = 0 if current_row >= @rows
current_column = 0 if current_column >= @columns
current_row = @rows - 1 if current_row < 0
column_column = @columns - 1 if current_column < 0
@current_position[:row] = current_row
@current_position[:column] = current_column
@canvas[current_row][current_column] += 1
end
end
module Canvas
class ASCII
def initialize(*args)
@symbols = *args.flatten
@step = 100 / @symbols.size
end
def render(canvas)
biggest_number = canvas.max.max
procents = 100 / biggest_number
canvas = canvas.flatten.join()
#hard coding is bad, I know, but needed it for some kind of working prototype
canvas = canvas.gsub("0", "#{@symbols[0]}")
canvas = canvas.gsub("1", "#{@symbols[1]}")
canvas = canvas.gsub("2", "#{@symbols[2]}")
canvas = canvas.gsub("3", "#{@symbols[3]}")
canvas = canvas.gsub("4", "#{@symbols[4]}")
canvas_half_size = canvas.size / 2
canvas.insert(canvas_half_size,"\n")
end
end
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 [
       
       expected: "320\n110\n000"
            got: "3201\n10000"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,3 @@
       -320
       -110
       -000
       +3201
       +10000
     # /tmp/d20151203-5272-1uez80h/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 [
       
       expected: "ttz\nooz\nzzz"
            got: "tzoo\nzzzz"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,3 @@
       -ttz
       -ooz
       -zzz
       +tzoo
       +zzzz
     # /tmp/d20151203-5272-1uez80h/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-1uez80h/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1uez80h/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-1uez80h/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1uez80h/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-1uez80h/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1uez80h/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.01145 seconds
14 examples, 5 failures

Failed examples:

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

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

София обнови решението на 02.12.2015 15:17 (преди над 8 години)

+module TurtleGraphics
+require 'matrix'
+ class Turtle
+ ORIENTATIONS = [:left, :up, :right, :down]
+
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+ @orientation = :right
+ @canvas = Matrix.zero(rows,columns).to_a
+ @current_position = {row: 0, column: 0}
+ @canvas[0][0] = 1
+ end
+
+ def draw(renderer = nil, &block)
+ self.instance_exec &block
+
+ if renderer.nil?
+ @canvas
+ else
+ renderer.render(@canvas)
+ end
+ end
+
+ def move
+ case @orientation
+ when :right
+ @current_position[:column] += 1
+ when :left
+ @current_position[:column] -= 1
+ when :up
+ @current_position[:row] -= 1
+ when :down
+ @current_position[:row] += 1
+ end
+
+ outliner(@current_position[:row], @current_position[:column])
+ end
+
+ def turn_left
+ current_orientation = ORIENTATIONS.index(@orientation)
+ @orientation = ORIENTATIONS[current_orientation - 1]
+ end
+
+ def turn_right
+ current_orientation = ORIENTATIONS.index(@orientation)
+ @orientation = ORIENTATIONS[(current_orientation + 1) % 4]
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def spawn_at(row = 0, column = 0)
+ @canvas[0][0] = 0
+ @canvas[row][column] = 1
+ @current_position = {row: row, column: column}
+ end
+
+ private
+ def outliner(current_row, current_column)
+ current_row = 0 if current_row >= @rows
+ current_column = 0 if current_column >= @columns
+ current_row = @rows - 1 if current_row < 0
+ column_column = @columns - 1 if current_column < 0
+ @current_position[:row] = current_row
+ @current_position[:column] = current_column
+ @canvas[current_row][current_column] += 1
+ end
+ end
+end

София обнови решението на 02.12.2015 17:24 (преди над 8 години)

module TurtleGraphics
require 'matrix'
class Turtle
ORIENTATIONS = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows = rows
@columns = columns
@orientation = :right
@canvas = Matrix.zero(rows,columns).to_a
@current_position = {row: 0, column: 0}
@canvas[0][0] = 1
end
def draw(renderer = nil, &block)
self.instance_exec &block
if renderer.nil?
@canvas
else
renderer.render(@canvas)
end
end
def move
case @orientation
when :right
@current_position[:column] += 1
when :left
@current_position[:column] -= 1
when :up
@current_position[:row] -= 1
when :down
@current_position[:row] += 1
end
outliner(@current_position[:row], @current_position[:column])
end
def turn_left
current_orientation = ORIENTATIONS.index(@orientation)
@orientation = ORIENTATIONS[current_orientation - 1]
end
def turn_right
current_orientation = ORIENTATIONS.index(@orientation)
@orientation = ORIENTATIONS[(current_orientation + 1) % 4]
end
def look(orientation)
@orientation = orientation
end
def spawn_at(row = 0, column = 0)
@canvas[0][0] = 0
@canvas[row][column] = 1
@current_position = {row: row, column: column}
end
private
def outliner(current_row, current_column)
current_row = 0 if current_row >= @rows
current_column = 0 if current_column >= @columns
current_row = @rows - 1 if current_row < 0
column_column = @columns - 1 if current_column < 0
@current_position[:row] = current_row
@current_position[:column] = current_column
@canvas[current_row][current_column] += 1
end
+
end
-end
+
+ module Canvas
+ class ASCII
+
+ def initialize(*args)
+ @symbols = *args.flatten
+ @step = 100 / @symbols.size
+ end
+
+ def render(canvas)
+ biggest_number = canvas.max.max
+ procents = 100 / biggest_number
+ canvas = canvas.flatten.join()
+
+ canvas = canvas.gsub("0", "#{@symbols[0]}")
+ canvas = canvas.gsub("1", "#{@symbols[1]}")
+ canvas = canvas.gsub("2", "#{@symbols[2]}")
+ canvas = canvas.gsub("3", "#{@symbols[3]}")
+ canvas = canvas.gsub("4", "#{@symbols[4]}")
+ canvas_half_size = canvas.size / 2
+ canvas.insert(canvas_half_size,"\n")
+ end
+
+ end
+ end
+end

София обнови решението на 02.12.2015 17:27 (преди над 8 години)

module TurtleGraphics
require 'matrix'
class Turtle
ORIENTATIONS = [:left, :up, :right, :down]
def initialize(rows, columns)
@rows = rows
@columns = columns
@orientation = :right
@canvas = Matrix.zero(rows,columns).to_a
@current_position = {row: 0, column: 0}
@canvas[0][0] = 1
end
def draw(renderer = nil, &block)
self.instance_exec &block
if renderer.nil?
@canvas
else
renderer.render(@canvas)
end
end
def move
case @orientation
when :right
@current_position[:column] += 1
when :left
@current_position[:column] -= 1
when :up
@current_position[:row] -= 1
when :down
@current_position[:row] += 1
end
outliner(@current_position[:row], @current_position[:column])
end
def turn_left
current_orientation = ORIENTATIONS.index(@orientation)
@orientation = ORIENTATIONS[current_orientation - 1]
end
def turn_right
current_orientation = ORIENTATIONS.index(@orientation)
@orientation = ORIENTATIONS[(current_orientation + 1) % 4]
end
def look(orientation)
@orientation = orientation
end
def spawn_at(row = 0, column = 0)
@canvas[0][0] = 0
@canvas[row][column] = 1
@current_position = {row: row, column: column}
end
private
def outliner(current_row, current_column)
current_row = 0 if current_row >= @rows
current_column = 0 if current_column >= @columns
current_row = @rows - 1 if current_row < 0
column_column = @columns - 1 if current_column < 0
@current_position[:row] = current_row
@current_position[:column] = current_column
@canvas[current_row][current_column] += 1
end
end
module Canvas
class ASCII
def initialize(*args)
@symbols = *args.flatten
@step = 100 / @symbols.size
end
def render(canvas)
biggest_number = canvas.max.max
procents = 100 / biggest_number
canvas = canvas.flatten.join()
-
+ #hard coding is bad, I know, but needed it for some kind of working prototype
canvas = canvas.gsub("0", "#{@symbols[0]}")
canvas = canvas.gsub("1", "#{@symbols[1]}")
canvas = canvas.gsub("2", "#{@symbols[2]}")
canvas = canvas.gsub("3", "#{@symbols[3]}")
canvas = canvas.gsub("4", "#{@symbols[4]}")
canvas_half_size = canvas.size / 2
canvas.insert(canvas_half_size,"\n")
end
end
end
end