Решение на Шеста задача от Виктор Радивчев

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

Към профила на Виктор Радивчев

Резултати

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

Код

module TurtleGraphics
class Turtle
def initialize(rows = 1, columns = 1)
@rows, @columns = rows, columns
@canvas = Array.new(@rows) { Array.new(@columns, 0) }
@canvas[0][0] = 1
@position = [0, 0]
@orientation = :right
end
def draw(canvas = [], &block)
self.instance_eval(&block)
return @canvas if canvas.instance_of? Array
return canvas_into_html(@canvas, canvas) if canvas.instance_of? Canvas::HTML
return canvas_into_ascii(@canvas, canvas) if canvas.instance_of? Canvas::ASCII
end
def spawn_at(row, column)
@position = [row, column]
@canvas[0][0] -= 1
@canvas[row][column] = 1
end
def turn_right
case @orientation
when :up
@orientation = :right
when :down
@orientation = :left
when :right
@orientation = :down
when :left
@orientation = :up
end
end
def turn_left
case @orientation
when :up
@orientation = :left
when :down
@orientation = :right
when :right
@orientation = :up
when :left
@orientation = :down
end
end
def move
possible_moves = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
new_row = (@position[0] + possible_moves[@orientation][0]) % @rows
new_column = (@position[1] + possible_moves[@orientation][1]) % @columns
@canvas[new_row][new_column] += 1
@position = [new_row, new_column]
end
def look(orientation)
@orientation = orientation
end
end
module Canvas
class HTML
attr_accessor :pixels
def initialize(pixels)
@pixels = pixels
end
end
class ASCII
attr_accessor :array
def initialize(array)
@array = array
end
def [](index)
@array[index]
end
def size
@array.size
end
end
end
class Turtle
def canvas_into_ascii(matrix, canvas)
intensity = matrix.flatten.max
opacity_matrix = matrix.map do |row|
row.map { |cell| cell * 1.0 / intensity }
end
result = ""
opacity_matrix.each do |row|
row.each { |cell| result << canvas[(cell * (canvas.size - 1)).ceil] }
result << "\n"
end
result.chop
end
end
class Turtle
def begin_canvas(canvas)
message = ""
message << "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics</title>
\n <style>\n table {\n border-spacing: 0;\n }\n\n tr {
padding: 0;\n }\n\n td {\n width: #{canvas.pixels} px;
height: #{canvas.pixels} px;\n\n background-color: black;
padding: 0;\n }\n </style>\n</head>\n<body>\n <table>\n"
end
def canvas_into_html(matrix, canvas)
message, intensity = begin_canvas(canvas), matrix.flatten.max
matrix.each do |row|
message << " <tr>\n"
row.each do |cell|
opacity = format('%.2f', cell * 1.0 / intensity)
message << " <td style=\"opacity: #{opacity}\"></td>\n"
end
message << " </tr>\n"
end
message << " </table>\n</body>\n</html>"
end
end
end

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

..............

Finished in 0.01837 seconds
14 examples, 0 failures

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

Виктор обнови решението на 02.12.2015 14:27 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+ def initialize(rows = 1, columns = 1)
+ @rows, @columns = rows, columns
+ @canvas = Array.new(@rows) { Array.new(@columns, 0) }
+ @canvas[0][0] = 1
+ @position = [0, 0]
+ @orientation = :right
+ end
+
+ def draw(canvas = [], &block)
+ self.instance_eval(&block)
+ return @canvas if canvas.instance_of? Array
+ return canvas_into_html(@canvas, canvas) if canvas.instance_of? Canvas::HTML
+ return canvas_into_ascii(@canvas, canvas) if canvas.instance_of? Canvas::ASCII
+ end
+
+ def spawn_at(row, column)
+ @position = [row, column]
+ @canvas[0][0] -= 1
+ @canvas[row][column] = 1
+ end
+
+ def turn_right
+ case @orientation
+ when :up
+ @orientation = :right
+ when :down
+ @orientation = :left
+ when :right
+ @orientation = :down
+ when :left
+ @orientation = :up
+ end
+ end
+
+ def turn_left
+ case @orientation
+ when :up
+ @orientation = :left
+ when :down
+ @orientation = :right
+ when :right
+ @orientation = :up
+ when :left
+ @orientation = :down
+ end
+ end
+
+ def move
+ possible_moves = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
+ new_row = (@position[0] + possible_moves[@orientation][0]) % @rows
+ new_column = (@position[1] + possible_moves[@orientation][1]) % @columns
+ @canvas[new_row][new_column] += 1
+ @position = [new_row, new_column]
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end
+
+ module Canvas
+ class HTML
+ attr_accessor :pixels
+
+ def initialize(pixels)
+ @pixels = pixels
+ end
+ end
+
+ class ASCII
+ attr_accessor :array
+
+ def initialize(array)
+ @array = array
+ end
+
+ def [](index)
+ @array[index]
+ end
+
+ def size
+ @array.size
+ end
+ end
+ end
+
+ class Turtle
+
+ def canvas_into_ascii(matrix, canvas)
+ intensity = matrix.flatten.max
+ opacity_matrix = matrix.map do |row|
+ row.map { |cell| cell * 1.0 / intensity }
+ end
+ result = ""
+ opacity_matrix.each do |row|
+ row.each { |cell| result << canvas[(cell * (canvas.size - 1)).ceil] }
+ result << "\n"
+ end
+ result.chop
+ end
+ end
+
+ class Turtle
+ def begin_canvas(canvas)
+ message = ""
+ message << "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics</title>
+\n <style>\n table {\n border-spacing: 0;\n }\n\n tr {
+ padding: 0;\n }\n\n td {\n width: #{canvas.pixels} px;
+ height: #{canvas.pixels} px;\n\n background-color: black;
+ padding: 0;\n }\n </style>\n</head>\n<body>\n <table>\n"
+ end
+ def canvas_into_html(matrix, canvas)
+ message, intensity = begin_canvas(canvas), matrix.flatten.max
+ matrix.each do |row|
+ message << " <tr>\n"
+ row.each do |cell|
+ opacity = format('%.2f', cell * 1.0 / intensity)
+ message << " <td style=\"opacity: #{opacity}\"></td>\n"
+ end
+ message << " </tr>\n"
+ end
+ message << " </table>\n</body>\n</html>"
+ end
+ end
+end

Здравей,

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

  • Няколкократното отваряне на класа Turtle.
  • Методите canvas_into_ascii и canvas_into_html, дефинирани в Turtle, вместо в техните си класове.
  • Излишните конкатенации и нечетимият HTML

Прегледай нашето решение и помисли за правилните места на кода за генериране на ASCII и HTML.