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

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

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

Резултати

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

Код

module TurtleGraphics
module Canvas
class ASCII
attr_accessor :array_of_symbols
def initialize(array_of_symbols)
@array_of_symbols = array_of_symbols
end
def length
@array_of_symbols.size
end
def nth(number)
@array_of_symbols[number]
end
def element_intensity(elem, arg, canvas)
index = ((elem.to_f / find_the_highest(canvas)) * arg.length).ceil - 1
print arg.nth(index)
end
def find_the_highest(canvas)
canvas.flatten.sort[-1]
end
end
class HTML
attr_accessor :pixel_size
def initialize(pixel_size)
@pixel_size = pixel_size
end
def to_s
@pixel_size.to_s
end
def intensity(element, canvas)
element.to_f / find_the_highest(canvas)
end
def find_the_highest(canvas)
canvas.flatten.sort[-1]
end
def header
"<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics</title>\n"
end
def style(arg)
" <style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
width: " + arg.to_s + "px;
height: " + arg.to_s + "px;
background-color: black;
padding: 0;
}
</style>
</head>
"
end
def intensity_table(canvas)
"<body>
<table>\n" << table(canvas) << " </table>
</body>
</html>"
end
def table(c)
(c.map {|x| "<tr>\n" << table_help(x, c).to_s << "</tr>\n"}).to_s
end
def table_help(x, canvas)
x.map{|y|
"<td style=\"opacity:" << intensity(y, canvas).to_s << "></td>\"\n"}
end
end
end
class Turtle
attr_reader :width, :height
def initialize(width, height)
@width, @height, @x, @y = width, height, 0, 0
@canvas = Array.new(width) { Array.new(height, default = 0) }
@canvas[0][0] += 1
@orientation = :right
@all_orientations = { :left => [0, -1], :right => [0, 1],
:up => [-1, 0], :down => [1, 0] }
end
def draw(arg = nil)
instance_eval(&Proc.new) if block_given?
if (arg.class == TurtleGraphics::Canvas::ASCII)
draw_ascii(arg, @canvas)
elsif (arg.class == TurtleGraphics::Canvas::HTML)
draw_html(arg, @canvas)
end
end
def draw_ascii(arg, canvas)
canvas.each do |row|
puts row.map{ |x| arg.element_intensity(x, arg, canvas) }.join(" ")
end
end
def draw_html(arg, canvas)
html_text = ""
html_text << arg.header << arg.style(arg) << arg.intensity_table(canvas)
print html_text
end
def spawn_at(row, column)
@x, @y = row, column
@canvas[@x][@y] += 1
@canvas[0][0] -= 1
end
def move
if (@x + @all_orientations[@orientation][0] >= @width or
@y + @all_orientations[@orientation][1] >= @height)
move_help
else
@x += @all_orientations[@orientation][0]
@y += @all_orientations[@orientation][1]
end
@canvas[@x][@y] += 1
end
def move_help
case @orientation
when :left
@y = @width - 1
when :right
@y = 0
when :up
@x = @height - 1
when :down
@x = 0
end
end
def turn_right
look(@all_orientations.key(@all_orientations[@orientation].reverse))
end
def turn_left
look(@all_orientations.key(@all_orientations[@orientation].reverse))
end
def look(orientation)
@orientation = orientation
end
end
end

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

FFFFFFFFF333  
333  
333  
Fttt  
ttt  
ttt  
F<!DOCTYPE html>
<html>
<head>
  <title>Turtle graphics</title>
  <style>
            table {
              border-spacing: 0;
            }

            tr {
              padding: 0;
            }

            td {
              width: 5px;
              height: 5px;

              background-color: black;
              padding: 0;
            }
          </style>
        </head>
        <body>
          <table>
["<tr>\n[\"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\"]</tr>\n", "<tr>\n[\"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\"]</tr>\n", "<tr>\n[\"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\"]</tr>\n"] </table>
        </body>
        </html>F<!DOCTYPE html>
<html>
<head>
  <title>Turtle graphics</title>
  <style>
            table {
              border-spacing: 0;
            }

            tr {
              padding: 0;
            }

            td {
              width: 3px;
              height: 3px;

              background-color: black;
              padding: 0;
            }
          </style>
        </head>
        <body>
          <table>
["<tr>\n[\"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\"]</tr>\n", "<tr>\n[\"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\"]</tr>\n", "<tr>\n[\"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:0.0></td>\\\"\\n\"]</tr>\n"] </table>
        </body>
        </html>F<!DOCTYPE html>
<html>
<head>
  <title>Turtle graphics</title>
  <style>
            table {
              border-spacing: 0;
            }

            tr {
              padding: 0;
            }

            td {
              width: 5px;
              height: 5px;

              background-color: black;
              padding: 0;
            }
          </style>
        </head>
        <body>
          <table>
["<tr>\n[\"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\"]</tr>\n", "<tr>\n[\"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\"]</tr>\n", "<tr>\n[\"<td style=\\\"opacity:0.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\", \"<td style=\\\"opacity:1.0></td>\\\"\\n\"]</tr>\n"] </table>
        </body>
        </html>F

Failures:

  1) TurtleGraphics renders a complex shape
     Failure/Error: expect(canvas).to eq [
       
       expected: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:284:in `block (2 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 Turtle #draw #move marks where the turtle has moved
     Failure/Error: expect(canvas).to eq [[1, 1], [0, 0]]
       
       expected: [[1, 1], [0, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:11:in `block (5 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 Turtle #draw #move moves the turtle to the start of row or column when we are at its end
     Failure/Error: expect(canvas[0]).to eq [2, 2]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151203-5272-13vgu3x/spec.rb:19:in `block (5 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 Turtle #draw #move keeps the orientation when we get out of column
     Failure/Error: expect(canvas[0][0]).to be > 0
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151203-5272-13vgu3x/spec.rb:39:in `block (5 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 Turtle #draw #move counts the times we have passed through every cell
     Failure/Error: expect(canvas).to eq [[2, 1], [0, 0]]
       
       expected: [[2, 1], [0, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:50:in `block (5 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)>'

  6) TurtleGraphics Turtle #draw #turn_right turns the orienation of the turtle right of where we stand
     Failure/Error: expect(canvas).to eq [[1, 0], [1, 0]]
       
       expected: [[1, 0], [1, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:61:in `block (5 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)>'

  7) TurtleGraphics Turtle #draw #turn_left turns the orienation of the turtle left of where we stand
     Failure/Error: expect(canvas).to eq [[1, 0], [1, 0]]
       
       expected: [[1, 0], [1, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:79:in `block (5 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)>'

  8) TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: expect(canvas).to eq [[0, 0], [1, 0]]
       
       expected: [[0, 0], [1, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:93:in `block (5 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)>'

  9) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: expect(canvas).to eq [[1, 0], [1, 0]]
       
       expected: [[1, 0], [1, 0]]
            got: nil
       
       (compared using ==)
     # /tmp/d20151203-5272-13vgu3x/spec.rb:105:in `block (5 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)>'

  10) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [[1, 1, 1], [0, 1, 1], [0, 1, 1]]:Array
     # /tmp/d20151203-5272-13vgu3x/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)>'

  11) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: expect(ascii.sub(/\n\z/, '')).to eq [
     NoMethodError:
       undefined method `sub' for [[1, 1, 1], [0, 1, 1], [0, 1, 1]]:Array
     # /tmp/d20151203-5272-13vgu3x/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)>'

  12) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to eq <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-13vgu3x/spec.rb:171: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)>'

  13) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-13vgu3x/spec.rb:220: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)>'

  14) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to include <<-HTML.gsub(/\s+/, '')
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20151203-5272-13vgu3x/spec.rb:242: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.01019 seconds
14 examples, 14 failures

Failed examples:

rspec /tmp/d20151203-5272-13vgu3x/spec.rb:264 # TurtleGraphics renders a complex shape
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:9 # TurtleGraphics Turtle #draw #move marks where the turtle has moved
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:14 # TurtleGraphics Turtle #draw #move moves the turtle to the start of row or column when we are at its end
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:33 # TurtleGraphics Turtle #draw #move keeps the orientation when we get out of column
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:45 # TurtleGraphics Turtle #draw #move counts the times we have passed through every cell
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:55 # TurtleGraphics Turtle #draw #turn_right turns the orienation of the turtle right of where we stand
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:73 # TurtleGraphics Turtle #draw #turn_left turns the orienation of the turtle left of where we stand
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:98 # TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-13vgu3x/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Пламена обнови решението на 02.12.2015 17:22 (преди над 8 години)

+module TurtleGraphics
+ module Canvas
+ class ASCII
+ attr_accessor :array_of_symbols
+
+ def initialize(array_of_symbols)
+ @array_of_symbols = array_of_symbols
+ end
+
+ def length
+ @array_of_symbols.size
+ end
+
+ def nth(number)
+ @array_of_symbols[number]
+ end
+
+ def element_intensity(elem, arg, canvas)
+ index = ((elem.to_f / find_the_highest(canvas)) * arg.length).ceil - 1
+ print arg.nth(index)
+ end
+
+ def find_the_highest(canvas)
+ canvas.flatten.sort[-1]
+ end
+ end
+
+ class HTML
+ attr_accessor :pixel_size
+
+ def initialize(pixel_size)
+ @pixel_size = pixel_size
+ end
+
+ def to_s
+ @pixel_size.to_s
+ end
+
+ def intensity(element, canvas)
+ element.to_f / find_the_highest(canvas)
+ end
+
+ def find_the_highest(canvas)
+ canvas.flatten.sort[-1]
+ end
+
+ def header
+ "<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics</title>\n"
+ end
+
+ def style(arg)
+ " <style>
+ table {
+ border-spacing: 0;
+ }
+
+ tr {
+ padding: 0;
+ }
+
+ td {
+ width: " + arg.to_s + "px;
+ height: " + arg.to_s + "px;
+
+ background-color: black;
+ padding: 0;
+ }
+ </style>
+ </head>
+ "
+ end
+
+ def intensity_table(canvas)
+ "<body>
+ <table>
+ </table>\n" << table(canvas) << "</body>
+ </html>"
+ end
+
+ def table(c)
+ (c.map {|x| "<tr>\n" << table_help(x, c).to_s << "</tr>\n"}).to_s
+ end
+
+ def table_help(x, canvas)
+ x.map{|y|
+ "<td style=\"opacity:" << intensity(y, canvas).to_s << "></td>\"\n"}
+ end
+ end
+ end
+
+ class Turtle
+ attr_reader :width, :height
+
+ def initialize(width, height)
+ @width, @height, @x, @y = width, height, 0, 0
+ @canvas = Array.new(width) { Array.new(height, default = 0) }
+ @canvas[0][0] += 1
+ @orientation = :right
+ @all_orientations = { :left => [0, -1], :right => [0, 1],
+ :up => [-1, 0], :down => [1, 0] }
+ end
+
+ def draw(arg = nil)
+ instance_eval(&Proc.new) if block_given?
+ if (arg.class == TurtleGraphics::Canvas::ASCII)
+ draw_ascii(arg, @canvas)
+ elsif (arg.class == TurtleGraphics::Canvas::HTML)
+ draw_html(arg, @canvas)
+ end
+ end
+
+ def draw_ascii(arg, canvas)
+ canvas.each do |row|
+ puts row.map{ |x| arg.element_intensity(x, arg, canvas) }.join(" ")
+ end
+ end
+
+ def draw_html(arg, canvas)
+ html_text = ""
+ html_text << arg.header << arg.style(arg) << arg.intensity_table(canvas)
+ print html_text
+ end
+
+ def spawn_at(row, column)
+ @x, @y = row, column
+ @canvas[@x][@y] += 1
+ @canvas[0][0] -= 1
+ end
+
+ def move
+ if (@x + @all_orientations[@orientation][0] >= @width or
+ @y + @all_orientations[@orientation][1] >= @height)
+ move_help
+ else
+ @x += @all_orientations[@orientation][0]
+ @y += @all_orientations[@orientation][1]
+ end
+ @canvas[@x][@y] += 1
+ end
+
+ def move_help
+ case @orientation
+ when :left
+ @y = @width - 1
+ when :right
+ @y = 0
+ when :up
+ @x = @height - 1
+ when :down
+ @x = 0
+ end
+ end
+
+ def turn_right
+ look(@all_orientations.key(@all_orientations[@orientation].reverse))
+ end
+
+ def turn_left
+ look(@all_orientations.key(@all_orientations[@orientation].reverse))
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end
+end

Пламена обнови решението на 02.12.2015 17:25 (преди над 8 години)

module TurtleGraphics
module Canvas
class ASCII
attr_accessor :array_of_symbols
def initialize(array_of_symbols)
@array_of_symbols = array_of_symbols
end
def length
@array_of_symbols.size
end
def nth(number)
@array_of_symbols[number]
end
def element_intensity(elem, arg, canvas)
index = ((elem.to_f / find_the_highest(canvas)) * arg.length).ceil - 1
print arg.nth(index)
end
def find_the_highest(canvas)
canvas.flatten.sort[-1]
end
end
class HTML
attr_accessor :pixel_size
def initialize(pixel_size)
@pixel_size = pixel_size
end
def to_s
@pixel_size.to_s
end
def intensity(element, canvas)
element.to_f / find_the_highest(canvas)
end
def find_the_highest(canvas)
canvas.flatten.sort[-1]
end
def header
"<!DOCTYPE html>\n<html>\n<head>\n <title>Turtle graphics</title>\n"
end
def style(arg)
" <style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
width: " + arg.to_s + "px;
height: " + arg.to_s + "px;
background-color: black;
padding: 0;
}
</style>
</head>
"
end
def intensity_table(canvas)
"<body>
- <table>
- </table>\n" << table(canvas) << "</body>
+ <table>\n" << table(canvas) << " </table>
+ </body>
</html>"
end
def table(c)
(c.map {|x| "<tr>\n" << table_help(x, c).to_s << "</tr>\n"}).to_s
end
def table_help(x, canvas)
x.map{|y|
"<td style=\"opacity:" << intensity(y, canvas).to_s << "></td>\"\n"}
end
end
end
class Turtle
attr_reader :width, :height
def initialize(width, height)
@width, @height, @x, @y = width, height, 0, 0
@canvas = Array.new(width) { Array.new(height, default = 0) }
@canvas[0][0] += 1
@orientation = :right
@all_orientations = { :left => [0, -1], :right => [0, 1],
:up => [-1, 0], :down => [1, 0] }
end
def draw(arg = nil)
instance_eval(&Proc.new) if block_given?
if (arg.class == TurtleGraphics::Canvas::ASCII)
draw_ascii(arg, @canvas)
elsif (arg.class == TurtleGraphics::Canvas::HTML)
draw_html(arg, @canvas)
end
end
def draw_ascii(arg, canvas)
canvas.each do |row|
puts row.map{ |x| arg.element_intensity(x, arg, canvas) }.join(" ")
end
end
def draw_html(arg, canvas)
html_text = ""
html_text << arg.header << arg.style(arg) << arg.intensity_table(canvas)
print html_text
end
def spawn_at(row, column)
@x, @y = row, column
@canvas[@x][@y] += 1
@canvas[0][0] -= 1
end
def move
if (@x + @all_orientations[@orientation][0] >= @width or
@y + @all_orientations[@orientation][1] >= @height)
move_help
else
@x += @all_orientations[@orientation][0]
@y += @all_orientations[@orientation][1]
end
@canvas[@x][@y] += 1
end
def move_help
case @orientation
when :left
@y = @width - 1
when :right
@y = 0
when :up
@x = @height - 1
when :down
@x = 0
end
end
def turn_right
look(@all_orientations.key(@all_orientations[@orientation].reverse))
end
def turn_left
look(@all_orientations.key(@all_orientations[@orientation].reverse))
end
def look(orientation)
@orientation = orientation
end
end
end