Решение на Шеста задача от Борис Монев

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

Към профила на Борис Монев

Резултати

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

Код

class TurtleGraphics
class Canvas
class ASCII
def initialize(symbols)
@hash = symbols.map.with_index { |n, i| [i, n] }.to_h
end
def symbols
@hash
end
end
end
class Turtle
def initialize(rows, columns)
@orientation = :right
@turtle_position
@grid, helper = [], []
while columns > 0
helper << 0
columns -= 1
end
while rows > 0
@grid << helper.dup
rows -= 1
end
end
def look(orientation)
@orientation = orientation
end
def spawn_at(row, column)
@turtle_position = [row, column]
raise_counter
if (row != 0) or (column != 0)
@grid[0][0] = 0
end
end
def turn_right
case @orientation
when :right
@orientation = :down
when :down
@orientation = :left
when :left
@orientation = :up
when :up
@orientation = :right
end
end
def turn_left
case @orientation
when :right
@orientation = :up
when :down
@orientation = :right
when :left
@orientation = :down
when :up
@orientation = :left
end
end
def draw(type = nil, &block)
spawn_at(0,0)
self.instance_eval(&block) if block_given?
if type == nil
@grid
elsif type.class == TurtleGraphics::Canvas::ASCII
ascii_grid(type)
end
end
def move
case @orientation
when :right
@turtle_position[1] += 1
when :left
@turtle_position[1] -= 1
when :up
@turtle_position[0] -= 1
when :down
@turtle_position[0] += 1
end
raise_counter
end
private
def raise_counter
row, column = *@turtle_position
row = (row + @grid.size) % @grid.size
column = (column + @grid[0].size) % @grid[0].size
@turtle_position = [row,column]
@grid[row][column] += 1
end
def ascii_grid(type)
@grid.map { |row| row.map { |n| type.symbols[n] } }.
map { |n| n.join"" }.join"\n"
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: "tz\nooz\nzzz"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -ttz
       +tz
        ooz
        zzz
     # /tmp/d20151203-5272-16y4owa/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: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-16y4owa/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-16y4owa/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: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-16y4owa/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-16y4owa/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: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas::HTML
     # /tmp/d20151203-5272-16y4owa/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-16y4owa/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.01378 seconds
14 examples, 4 failures

Failed examples:

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

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

Борис обнови решението на 01.12.2015 17:34 (преди над 8 години)

+class TurtleGraphics
+ class Canvas
+ class ASCII
+
+ def initialize(symbols)
+ @hash = symbols.map.with_index { |n, i| [i, n] }.to_h
+ end
+
+ def symbols
+ @hash
+ end
+ end
+ end
+
+ class Turtle
+
+ def initialize(rows, columns)
+ @orientation = :right
+ @turtle_position
+ @grid, helper = [], []
+ while columns > 0
+ helper << 0
+ columns -= 1
+ end
+ while rows > 0
+ @grid << helper.dup
+ rows -= 1
+ end
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def spawn_at(row, column)
+ @turtle_position = [row, column]
+ raise_counter
+ if (row != 0) or (column != 0)
+ @grid[0][0] = 0
+ end
+ end
+
+ def turn_right
+ case @orientation
+ when :right
+ @orientation = :down
+ when :down
+ @orientation = :left
+ when :left
+ @orientation = :up
+ when :up
+ @orientation = :right
+ end
+ end
+
+ def turn_left
+ case @orientation
+ when :right
+ @orientation = :up
+ when :down
+ @orientation = :right
+ when :left
+ @orientation = :down
+ when :up
+ @orientation = :left
+ end
+ end
+
+ def draw(type = nil, &block)
+ spawn_at(0,0)
+ self.instance_eval(&block) if block_given?
+ if type == nil
+ @grid
+ elsif type.class == TurtleGraphics::Canvas::ASCII
+ ascii_grid(type)
+ end
+ end
+
+ def move
+ case @orientation
+ when :right
+ @turtle_position[1] += 1
+ when :left
+ @turtle_position[1] -= 1
+ when :up
+ @turtle_position[0] -= 1
+ when :down
+ @turtle_position[0] += 1
+ end
+ raise_counter
+ end
+
+ private
+
+ def raise_counter
+ row, column = *@turtle_position
+ row = (row + @grid.size) % @grid.size
+ column = (column + @grid[0].size) % @grid[0].size
+ @turtle_position = [row,column]
+ @grid[row][column] += 1
+ end
+
+ def ascii_grid(type)
+ @grid.map { |row| row.map { |n| type.symbols[n] } }.
+ map { |n| n.join"" }.join"\n"
+ end
+ end
+end

Здравей,

Отнемам ти точка заради:

  • Процедурното генериране на матрица с while и използване на брояч.
  • Мястото, на което си дефинирал логиката за създаване на ASCII арт. Това е логично да се случва в класа за ASCII. В противен случай ако имаше и HTML всичко щеше да е на едно място, което води до лош код, който се поддържа трудно.
  • Имената n и i

Прегледай нашето решение и помисли за местата на различните методи.