Решение на Шеста задача от Петър Иванов

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

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

Резултати

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

Код

class TurtleGraphics
class Turtle
ORIENTATIONS = { 1 => :left, 2 => :up, 3 => :right, 4 => :down }
def initialize(rows, columns)
@rows = rows
@columns = columns
@matrix = Array.new(@rows) { Array.new(@columns) { 0 } }
@orientation = :right
@current_position = nil
end
def draw(canvas = nil, &block)
if block_given?
self.instance_eval(&block)
end
if @current_position == nil then spawn_at(0, 0) end
if canvas != nil
if canvas.respond_to?(:ascii_draw)
canvas.ascii_draw(@matrix)
elsif canvas.respond_to?(:html_draw)
canvas.html_draw(@matrix)
end
else
@matrix
end
end
def move
if @current_position == nil then spawn_at(0, 0) end
direction = [0, 0]
if @orientation == :left then direction[1] = -1 end
if @orientation == :up then direction[0] = -1 end
if @orientation == :right then direction[1] = 1 end
if @orientation == :down then direction[0] = 1 end
@current_position = [(@current_position[0] + direction[0]) % @rows,
(@current_position[1] + direction[1]) % @columns]
@matrix[@current_position[0]][@current_position[1]] += 1
end
def turn_left
temp = ORIENTATIONS.key(@orientation) - 1
if temp == 0 then temp = 4 end
@orientation = ORIENTATIONS[temp]
end
def turn_right
temp = ORIENTATIONS.key(@orientation) + 1
if temp == 5 then temp = 1 end
@orientation = ORIENTATIONS[temp]
end
def spawn_at(row, column)
@current_position = [row, column]
@matrix[@current_position[0]][@current_position[1]] += 1
end
def look(orientation)
@orientation = orientation
end
end
class Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
end
def ascii_draw(matrix)
max_number = matrix.flatten.max
result = []
matrix.each_with_index do |x, xi|
temp = ""
x.each_with_index do |y, yi|
itensity = y.fdiv(max_number) * (@symbols.size - 1)
symbol = @symbols[itensity.ceil]
temp += symbol
end
result << temp
end
result.join("\n")
end
end
class HTML
def initialize(size_pixels)
@size_pixels = size_pixels
@text = "<!DOCTYPE html><html><head><title>Turtle graphics</title>" +
"<style>table { border-spacing: 0; } tr { padding: 0; } " +
"td { width: #{@size_pixels}px; height: #{@size_pixels}px; " +
"background-color: black; padding: 0; } </style> " +
"</head> <body> <table>"
end
def html_draw(matrix)
max_number = matrix.flatten.max
matrix.each_with_index do |x, xi|
temp = "<tr>"
x.each_with_index do |y, yi|
itensity = format('%.2f', y.fdiv(max_number))
temp += "<td style=\"opacity: #{itensity}\"></td>"
end
temp += "</tr>"
@text += temp
end
@text += "</table> </body> </html>"
end
end
end
end

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

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

Finished in 0.01316 seconds
14 examples, 0 failures

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

Петър обнови решението на 02.12.2015 10:16 (преди над 8 години)

+class TurtleGraphics
+ class Turtle
+
+ ORIENTATIONS = { 1 => :left, 2 => :up, 3 => :right, 4 => :down }
+
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+ @matrix = Array.new(@rows) { Array.new(@columns) { 0 } }
+ @orientation = :right
+ @current_position = nil
+ end
+
+ def draw(canvas = nil, &block)
+ if block_given?
+ self.instance_eval(&block)
+ end
+
+ if @current_position == nil then spawn_at(0, 0) end
+
+ if canvas != nil
+ if canvas.respond_to?(:ascii_draw)
+ canvas.ascii_draw(@matrix)
+ elsif canvas.respond_to?(:html_draw)
+ canvas.html_draw(@matrix)
+ end
+ else
+ @matrix
+ end
+ end
+
+ def move
+ if @current_position == nil then spawn_at(0, 0) end
+ direction = [0, 0]
+ if @orientation == :left then direction[1] = -1
+ elsif @orientation == :up then direction[0] = -1
+ elsif @orientation == :right then direction[1] = 1
+ else @orientation == :down
+ direction[0] = 1
+ end
+ @current_position = [(@current_position[0] + direction[0]) % @rows,
+ (@current_position[1] + direction[1]) % @columns]
+ @matrix[@current_position[0]][@current_position[1]] += 1
+ end
+
+ def turn_left
+ temp = ORIENTATIONS.key(@orientation) - 1
+ if temp == 0 then temp = 4 end
+ @orientation = ORIENTATIONS[temp]
+ end
+
+ def turn_right
+ temp = ORIENTATIONS.key(@orientation) + 1
+ if temp == 5 then temp = 1 end
+ @orientation = ORIENTATIONS[temp]
+ end
+
+ def spawn_at(row, column)
+ @current_position = [row, column]
+ @matrix[@current_position[0]][@current_position[1]] += 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end
+
+ class Canvas
+ class ASCII
+ def initialize(symbols)
+ @symbols = symbols
+ end
+
+ def ascii_draw(matrix)
+ max_number = matrix.flatten.max
+ result = []
+ matrix.each_with_index do |x, xi|
+ temp = ""
+ x.each_with_index do |y, yi|
+ itensity = y.fdiv(max_number) * (@symbols.size - 1)
+ symbol = @symbols[itensity.ceil]
+ temp += symbol
+ end
+ result << temp
+ end
+ result.join("\n")
+ end
+ end
+
+ class HTML
+ def initialize(size_pixels)
+ @size_pixels = size_pixels
+
+ @text = "<!DOCTYPE html><html><head><title>Turtle graphics</title>" +
+ "<style>table { border-spacing: 0; } tr { padding: 0; } " +
+ "td { width: #{@size_pixels}px; height: #{@size_pixels}px; " +
+ "background-color: black; padding: 0; } </style> " +
+ "</head> <body> <table>"
+ end
+
+ def html_draw(matrix)
+ max_number = matrix.flatten.max
+ matrix.each_with_index do |x, xi|
+ temp = "<tr>"
+ x.each_with_index do |y, yi|
+ itensity = format('%.2f', y.fdiv(max_number))
+ temp += "<td style=\"opacity: #{itensity}\"></td>"
+ end
+ temp += "</tr>"
+ @text += temp
+ end
+ @text += "</table> </body> </html>"
+ return @text
+ end
+ end
+ end
+end

Петър обнови решението на 02.12.2015 12:38 (преди над 8 години)

class TurtleGraphics
class Turtle
ORIENTATIONS = { 1 => :left, 2 => :up, 3 => :right, 4 => :down }
def initialize(rows, columns)
@rows = rows
@columns = columns
@matrix = Array.new(@rows) { Array.new(@columns) { 0 } }
@orientation = :right
@current_position = nil
end
def draw(canvas = nil, &block)
if block_given?
self.instance_eval(&block)
end
if @current_position == nil then spawn_at(0, 0) end
if canvas != nil
if canvas.respond_to?(:ascii_draw)
canvas.ascii_draw(@matrix)
elsif canvas.respond_to?(:html_draw)
canvas.html_draw(@matrix)
end
else
@matrix
end
end
def move
if @current_position == nil then spawn_at(0, 0) end
direction = [0, 0]
if @orientation == :left then direction[1] = -1
elsif @orientation == :up then direction[0] = -1
elsif @orientation == :right then direction[1] = 1
else @orientation == :down
direction[0] = 1
end
@current_position = [(@current_position[0] + direction[0]) % @rows,
(@current_position[1] + direction[1]) % @columns]
@matrix[@current_position[0]][@current_position[1]] += 1
end
def turn_left
temp = ORIENTATIONS.key(@orientation) - 1
if temp == 0 then temp = 4 end
@orientation = ORIENTATIONS[temp]
end
def turn_right
temp = ORIENTATIONS.key(@orientation) + 1
if temp == 5 then temp = 1 end
@orientation = ORIENTATIONS[temp]
end
def spawn_at(row, column)
@current_position = [row, column]
@matrix[@current_position[0]][@current_position[1]] += 1
end
def look(orientation)
@orientation = orientation
end
end
class Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
end
def ascii_draw(matrix)
max_number = matrix.flatten.max
result = []
matrix.each_with_index do |x, xi|
temp = ""
x.each_with_index do |y, yi|
itensity = y.fdiv(max_number) * (@symbols.size - 1)
symbol = @symbols[itensity.ceil]
temp += symbol
end
result << temp
end
result.join("\n")
end
end
class HTML
def initialize(size_pixels)
@size_pixels = size_pixels
@text = "<!DOCTYPE html><html><head><title>Turtle graphics</title>" +
"<style>table { border-spacing: 0; } tr { padding: 0; } " +
"td { width: #{@size_pixels}px; height: #{@size_pixels}px; " +
"background-color: black; padding: 0; } </style> " +
"</head> <body> <table>"
end
def html_draw(matrix)
max_number = matrix.flatten.max
matrix.each_with_index do |x, xi|
temp = "<tr>"
x.each_with_index do |y, yi|
itensity = format('%.2f', y.fdiv(max_number))
temp += "<td style=\"opacity: #{itensity}\"></td>"
end
temp += "</tr>"
@text += temp
end
@text += "</table> </body> </html>"
- return @text
end
end
end
end

Петър обнови решението на 02.12.2015 12:41 (преди над 8 години)

class TurtleGraphics
class Turtle
ORIENTATIONS = { 1 => :left, 2 => :up, 3 => :right, 4 => :down }
def initialize(rows, columns)
@rows = rows
@columns = columns
@matrix = Array.new(@rows) { Array.new(@columns) { 0 } }
@orientation = :right
@current_position = nil
end
def draw(canvas = nil, &block)
if block_given?
self.instance_eval(&block)
end
if @current_position == nil then spawn_at(0, 0) end
if canvas != nil
if canvas.respond_to?(:ascii_draw)
canvas.ascii_draw(@matrix)
elsif canvas.respond_to?(:html_draw)
canvas.html_draw(@matrix)
end
else
@matrix
end
end
def move
if @current_position == nil then spawn_at(0, 0) end
direction = [0, 0]
- if @orientation == :left then direction[1] = -1
- elsif @orientation == :up then direction[0] = -1
- elsif @orientation == :right then direction[1] = 1
- else @orientation == :down
- direction[0] = 1
- end
+ if @orientation == :left then direction[1] = -1 end
+ if @orientation == :up then direction[0] = -1 end
+ if @orientation == :right then direction[1] = 1 end
+ if @orientation == :down then direction[0] = 1 end
@current_position = [(@current_position[0] + direction[0]) % @rows,
(@current_position[1] + direction[1]) % @columns]
@matrix[@current_position[0]][@current_position[1]] += 1
end
def turn_left
temp = ORIENTATIONS.key(@orientation) - 1
if temp == 0 then temp = 4 end
@orientation = ORIENTATIONS[temp]
end
def turn_right
temp = ORIENTATIONS.key(@orientation) + 1
if temp == 5 then temp = 1 end
@orientation = ORIENTATIONS[temp]
end
def spawn_at(row, column)
@current_position = [row, column]
@matrix[@current_position[0]][@current_position[1]] += 1
end
def look(orientation)
@orientation = orientation
end
end
class Canvas
class ASCII
def initialize(symbols)
@symbols = symbols
end
def ascii_draw(matrix)
max_number = matrix.flatten.max
result = []
matrix.each_with_index do |x, xi|
temp = ""
x.each_with_index do |y, yi|
itensity = y.fdiv(max_number) * (@symbols.size - 1)
symbol = @symbols[itensity.ceil]
temp += symbol
end
result << temp
end
result.join("\n")
end
end
class HTML
def initialize(size_pixels)
@size_pixels = size_pixels
@text = "<!DOCTYPE html><html><head><title>Turtle graphics</title>" +
"<style>table { border-spacing: 0; } tr { padding: 0; } " +
"td { width: #{@size_pixels}px; height: #{@size_pixels}px; " +
"background-color: black; padding: 0; } </style> " +
"</head> <body> <table>"
end
def html_draw(matrix)
max_number = matrix.flatten.max
matrix.each_with_index do |x, xi|
temp = "<tr>"
x.each_with_index do |y, yi|
itensity = format('%.2f', y.fdiv(max_number))
temp += "<td style=\"opacity: #{itensity}\"></td>"
end
temp += "</tr>"
@text += temp
end
@text += "</table> </body> </html>"
end
end
end
end

Здравей,

Имаш стилови проблеми:

  • Лошо именувани променливи. Например, използвал си temp в почти всеки метод.
  • Повторение на if-ове. Такива неща се пишат с elsif или case. Ако ти трябва единствен if, може да ползваш постфиксния вариант: <код> if <условие>
  • Логиката ти за рисуване е ненужно сложна. Погледни нашето решение за идеи. Също, конкатенирането на низове не е добра идея. Има join за тази цел.