Клара обнови решението на 29.11.2015 22:14 (преди около 9 години)
+module TurtleGraphics
+ class Turtle
+
+ def initialize(rows, cols)
+ @grid = Array.new(rows) { Array.new(cols, 0) }
+ @orientation = :right
+ @ascii_string = ""
+ end
+
+ def make_orientation
+ if @current_turn == nil
+ if @orientation == :right
+ @current_turn = [0, 1]
+ elsif @orientation == :left
+ @current_turn = [0, -1]
+ elsif @orientation == :up
+ @current_turn = [-1, 0]
+ else @current_turn = [1, 0]
+ end
+ end
+ end
+
+ def move
+ make_orientation
+ if @current_position == nil
+ @current_position = [0, 0]
+ @grid[@current_position.first][@current_position.last] += 1
+ end
+ if @current_position.last == @grid.size - 1 and @orientation == :right
+ @current_position = [@current_position.first, 0]
+ elsif @current_position.first == @grid.size - 1 and @orientation == :down
+ @current_position = [0, @current_position.last]
+ else
+ @current_position = @current_position.zip(@current_turn).map { |x, y| x + y }
+ end
+ @grid[@current_position.first][@current_position.last] += 1
+ end
+
+ def turn_left
+ if @orientation == :right
+ @orientation, @current_turn = :up, [-1, 0]
+ elsif @orientation == :left
+ @orientation, @current_turn = :down, [1, 0]
+ elsif @orientation == :up
+ @orientation, @current_turn = :left, [0, -1]
+ elsif @orientation == :down
+ @orientation, @current_turn = :right, [0, 1]
+ end
+ end
+
+ def turn_right
+ if @orientation == :right
+ @orientation, @current_turn = :down, [1, 0]
+ elsif @orientation == :left
+ @orientation, @current_turn = :up, [-1, 0]
+ elsif @orientation == :up
+ @orientation, @current_turn = :right, [0, 1]
+ elsif @orientation == :down
+ @orientation, @current_turn = :left, [0, -1]
+ end
+ end
+
+ def spawn_at(row, column)
+ @current_position = [row, column]
+ @grid[@current_position.first][@current_position.last] += 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def draw(instance = nil, &block)
+ if instance.instance_of? TurtleGraphics::Canvas::ASCII
+ @ascii, @html = instance, nil
+ else
+ @html, @ascii = instance, nil
+ end
+ self.instance_eval &block
+ if @ascii != nil
+ make_ascii_string(@ascii)
+ elsif @html != nil
+ make_table
+ @html.make_html(@table)
+ else
+ @grid
+ end
+ end
+
+ def make_ascii_string(ascii)
+ @grid.each_with_index do |elem, row|
+ elem.each_with_index do |element, col|
+ intensity_of_pixel = element.to_f / @grid.flatten.max
+ @ascii_string << ascii.intensity(intensity_of_pixel)
+ end
+ @ascii_string << "\n"
+ end
+ @ascii_string
+ end
+
+ def make_table
+ @table = "<table>"
+ @grid.each_with_index { |elem, row|
+ @table << "<tr>"
+ elem.each_with_index { |element, col|
+ intensity_of_pixel = element.to_f / @grid.flatten.max
+ intensity = "#{format('%.2f', intensity_of_pixel)}'"
+ @table << "<td style=\'opacity: #{intensity}></td>"
+ }
+ @table << "</tr>"
+ }
+ p "after all"
+ end
+end
+
+module Canvas
+
+ class ASCII
+
+ def initialize(list_of_symbols)
+ @list_of_symbols = list_of_symbols
+ end
+
+ def intensity(intensity)
+ current_list = @list_of_symbols.dup
+ return current_list[0] if intensity == 0
+ interval = 1.0 / (current_list.size - 1)
+ return current_list[1] if intensity > 0 and intensity <= interval
+ 2.times { current_list.delete_at(0) }
+ current_list.each do |iter|
+ last_interval = interval
+ new_interval = 2 * interval
+ interval = new_interval
+ return iter if intensity > last_interval and intensity <= new_interval
+ end
+ end
+ end
+
+ class HTML
+
+ def initialize(pixel_size)
+ @pixel_size = pixel_size
+ end
+
+ def make_html(table)
+ head = "<!DOCTYPE html>
+ <html>
+ <head>
+ <title>Turtle graphics</title>
+
+ <style>
+ table {
+ border-spacing: 0;
+ }
+
+ tr {
+ padding: 0;
+ }
+
+ td {
+ width: #{@pixel_size}px;
+ height: #{@pixel_size}px;
+
+ background-color: black;
+ padding: 0;
+ }
+ </style>
+ </head>"
+ body = "<body>" << table
+ body << "</body>"
+ head << body << "</html>"
+ end
+ end
+ end
+end
Здравей!
Имаш сериозни стилови проблеми - неправилно форматиран код, много конкатенация на низове, използване на in-place изтривания, много if-ове.
Прегледай пак style guide-а ни и се опитай да изчистиш решението си.
Също, раздели логиката по рендерирането от Turtle
. Turtle
не трябва да знае как се прави HTML или ASCII art. Това е задача за съответните canvas-и.