Решение на Шеста задача от Клара Кайралах

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

Към профила на Клара Кайралах

Резултати

  • 5 точки от тестове
  • 2 отнети точки
  • 3 точки общо
  • 12 успешни тест(а)
  • 2 неуспешни тест(а)

Код

module TurtleGraphics
class Turtle
def initialize(rows, cols)
@grid = Array.new(rows) { Array.new(cols, 0) }
@orientation = :right
end
def make_orientation
orientation = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
@current_turn = orientation[@orientation] if @current_turn == nil
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
left_turn = {right: [:up, [-1, 0]], left: [:down, [1, 0]],
up: [:left, [0, -1]], down: [:right, [0, 1]]}
@current_turn = left_turn[@orientation].last
@orientation = left_turn[@orientation].first
end
def turn_right
right_turn = {right: [:down,[1,0]], left: [:up, [-1, 0]],
up: [:right, [0, 1]], down: [:left, [0, -1]]}
@current_turn = right_turn[@orientation].last
@orientation = right_turn[@orientation].first
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)
return_var = @grid
if instance.instance_of? TurtleGraphics::Canvas::ASCII
@ascii, @html = instance, nil
else
@html, @ascii = instance, nil
end
self.instance_eval &block
return @ascii.make_ascii_string(@grid) if @ascii != nil
return @html.make_html(@grid) if @html != nil
@grid
end
end
module Canvas
class ASCII
def initialize(list_of_symbols)
@list_of_symbols = list_of_symbols
@ascii_string = ""
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
def make_ascii_string(grid)
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 << intensity(intensity_of_pixel)
end
@ascii_string << "\n"
end
@ascii_string
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def make_table(grid)
@table = "<table>"
grid.each_with_index do |elem, row|
@table << "<tr>"
elem.each_with_index do |element, col|
intensity_of_pixel = element.to_f / grid.flatten.max
intensity = "#{format('%.2f', intensity_of_pixel)}'"
@table << "<td style=\'opacity: #{intensity}></td>"
end
@table << "</tr>"
end
@table
end
def make_html(grid)
make_table(grid)
"<!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>
#@table
</body>
</html>"
end
end
end
end

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

...........F.F

Failures:

  1) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: expect(canvas.gsub(/\s+/, '')).to eq <<-HTML.gsub(/\s+/, '')
       
       expected: "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:1.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr></table></body></html>"
            got: "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle='opacity:1.00'></td><tdstyle='opacity:1.00'></td><tdstyle='opacity:1.00'></td></tr><tr><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td></tr><tr><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td></tr></body></html>"
       
       (compared using ==)
     # /tmp/d20151203-5272-1tjrnex/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)>'

  2) 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+/, '')
       expected "<!DOCTYPEhtml><html><head><title>Turtlegraphics</title><style>table{border-spacing:0;}tr{padding:0;}td{width:5px;height:5px;background-color:black;padding:0;}</style></head><body><table><tr><tdstyle='opacity:1.00'></td><tdstyle='opacity:0.67'></td><tdstyle='opacity:0.00'></td></tr><tr><tdstyle='opacity:0.33'></td><tdstyle='opacity:0.33'></td><tdstyle='opacity:0.00'></td></tr><tr><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td><tdstyle='opacity:0.00'></td></tr></body></html>" to include "<table><tr><tdstyle=\"opacity:1.00\"></td><tdstyle=\"opacity:0.67\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.33\"></td><tdstyle=\"opacity:0.00\"></td></tr><tr><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td><tdstyle=\"opacity:0.00\"></td></tr></table>"
     # /tmp/d20151203-5272-1tjrnex/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.01296 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20151203-5272-1tjrnex/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-1tjrnex/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

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

Клара обнови решението на 29.11.2015 22:14 (преди над 8 години)

+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-и.

Клара обнови решението на 02.12.2015 13:57 (преди над 8 години)

module TurtleGraphics
- class Turtle
+ class Turtle
- def initialize(rows, cols)
- @grid = Array.new(rows) { Array.new(cols, 0) }
- @orientation = :right
- @ascii_string = ""
- end
+ def initialize(rows, cols)
+ @grid = Array.new(rows) { Array.new(cols, 0) }
+ @orientation = :right
+ 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]
+ def make_orientation
+ orientation = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
+ if @current_turn == nil
+ @current_turn = orientation[@orientation]
end
end
- end
- def move
- make_orientation
- if @current_position == nil
- @current_position = [0, 0]
+ 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
- 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 }
+
+ def turn_left
+ left_turn = {right: [:up, [-1, 0]], left: [:down, [1, 0]],
+ up: [:left, [0, -1]], down: [:right, [0, 1]]}
+ @current_turn = left_turn[@orientation].last
+ @orientation = left_turn[@orientation].first
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]
+ def turn_right
+ right_turn = {right: [:down,[1,0]], left: [:up, [-1, 0]],
+ up: [:right, [0, 1]], down: [:left, [0, -1]]}
+ @current_turn = right_turn[@orientation].last
+ @orientation = right_turn[@orientation].first
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]
+ def spawn_at(row, column)
+ @current_position = [row, column]
+ @grid[@current_position.first][@current_position.last] += 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
+ def look(orientation)
+ @orientation = orientation
end
- self.instance_eval &block
- if @ascii != nil
- make_ascii_string(@ascii)
- elsif @html != nil
- make_table
- @html.make_html(@table)
- else
+
+ def draw(instance = nil, &block)
+ return_var = @grid
+ if instance.instance_of? TurtleGraphics::Canvas::ASCII
+ @ascii, @html = instance, nil
+ else
+ @html, @ascii = instance, nil
+ end
+ self.instance_eval &block
+ return @ascii.make_ascii_string(@grid) if @ascii != nil
+ return @html.make_html(@grid) if @html != nil
@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
+ module Canvas
+ class ASCII
- 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
+ def initialize(list_of_symbols)
+ @list_of_symbols = list_of_symbols
+ 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|
+ 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
+ current_list(2..current_list.size - 1).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
- end
- class HTML
-
- def initialize(pixel_size)
- @pixel_size = pixel_size
+ def make_ascii_string(grid)
+ 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 << intensity(intensity_of_pixel)
+ end
+ @ascii_string << "\n"
+ end
+ @ascii_string
+ end
end
- def make_html(table)
- head = "<!DOCTYPE html>
- <html>
- <head>
- <title>Turtle graphics</title>
+ class HTML
- <style>
- table {
- border-spacing: 0;
- }
+ def initialize(pixel_size)
+ @pixel_size = pixel_size
+ end
- tr {
- padding: 0;
- }
+ def make_table(grid)
+ @table = "<table>"
+ grid.each_with_index do |elem, row|
+ @table << "<tr>"
+ elem.each_with_index do |element, col|
+ intensity_of_pixel = element.to_f / grid.flatten.max
+ intensity = "#{format('%.2f', intensity_of_pixel)}'"
+ @table << "<td style=\'opacity: #{intensity}></td>"
+ end
+ @table << "</tr>"
+ end
+ @table
+ end
- td {
- width: #{@pixel_size}px;
- height: #{@pixel_size}px;
-
- background-color: black;
- padding: 0;
- }
- </style>
- </head>"
- body = "<body>" << table
- body << "</body>"
- head << body << "</html>"
+ def make_html(grid)
+ make_table(grid)
+ "<!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>
+ #@table
+ </body>
+ </html>"
+ end
end
- end
end
end

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

module TurtleGraphics
class Turtle
def initialize(rows, cols)
@grid = Array.new(rows) { Array.new(cols, 0) }
@orientation = :right
end
def make_orientation
orientation = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
- if @current_turn == nil
- @current_turn = orientation[@orientation]
- end
+ @current_turn = orientation[@orientation] if @current_turn == nil
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
left_turn = {right: [:up, [-1, 0]], left: [:down, [1, 0]],
up: [:left, [0, -1]], down: [:right, [0, 1]]}
@current_turn = left_turn[@orientation].last
@orientation = left_turn[@orientation].first
end
def turn_right
right_turn = {right: [:down,[1,0]], left: [:up, [-1, 0]],
up: [:right, [0, 1]], down: [:left, [0, -1]]}
@current_turn = right_turn[@orientation].last
@orientation = right_turn[@orientation].first
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)
return_var = @grid
if instance.instance_of? TurtleGraphics::Canvas::ASCII
@ascii, @html = instance, nil
else
@html, @ascii = instance, nil
end
self.instance_eval &block
return @ascii.make_ascii_string(@grid) if @ascii != nil
return @html.make_html(@grid) if @html != nil
@grid
end
end
module Canvas
class ASCII
def initialize(list_of_symbols)
@list_of_symbols = list_of_symbols
+ @ascii_string = ""
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
- current_list(2..current_list.size - 1).each do |iter|
+ 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
def make_ascii_string(grid)
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 << intensity(intensity_of_pixel)
end
@ascii_string << "\n"
end
@ascii_string
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def make_table(grid)
@table = "<table>"
grid.each_with_index do |elem, row|
@table << "<tr>"
elem.each_with_index do |element, col|
intensity_of_pixel = element.to_f / grid.flatten.max
intensity = "#{format('%.2f', intensity_of_pixel)}'"
@table << "<td style=\'opacity: #{intensity}></td>"
end
@table << "</tr>"
end
@table
end
def make_html(grid)
make_table(grid)
"<!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>
#@table
</body>
</html>"
end
end
end
end

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

module TurtleGraphics
class Turtle
def initialize(rows, cols)
@grid = Array.new(rows) { Array.new(cols, 0) }
@orientation = :right
end
def make_orientation
orientation = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
@current_turn = orientation[@orientation] if @current_turn == nil
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
left_turn = {right: [:up, [-1, 0]], left: [:down, [1, 0]],
up: [:left, [0, -1]], down: [:right, [0, 1]]}
@current_turn = left_turn[@orientation].last
@orientation = left_turn[@orientation].first
end
def turn_right
right_turn = {right: [:down,[1,0]], left: [:up, [-1, 0]],
up: [:right, [0, 1]], down: [:left, [0, -1]]}
@current_turn = right_turn[@orientation].last
@orientation = right_turn[@orientation].first
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)
return_var = @grid
if instance.instance_of? TurtleGraphics::Canvas::ASCII
@ascii, @html = instance, nil
else
@html, @ascii = instance, nil
end
self.instance_eval &block
return @ascii.make_ascii_string(@grid) if @ascii != nil
return @html.make_html(@grid) if @html != nil
@grid
end
end
module Canvas
class ASCII
def initialize(list_of_symbols)
@list_of_symbols = list_of_symbols
@ascii_string = ""
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
def make_ascii_string(grid)
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 << intensity(intensity_of_pixel)
end
@ascii_string << "\n"
end
@ascii_string
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def make_table(grid)
@table = "<table>"
grid.each_with_index do |elem, row|
@table << "<tr>"
elem.each_with_index do |element, col|
intensity_of_pixel = element.to_f / grid.flatten.max
intensity = "#{format('%.2f', intensity_of_pixel)}'"
@table << "<td style=\'opacity: #{intensity}></td>"
end
@table << "</tr>"
end
@table
end
def make_html(grid)
make_table(grid)
"<!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;
+ width: #@pixel_sizepx;
+ height: #@pixel_sizepx;
background-color: black;
padding: 0;
}
</style>
</head>
<body>
#@table
</body>
</html>"
end
end
end
end

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

module TurtleGraphics
class Turtle
def initialize(rows, cols)
@grid = Array.new(rows) { Array.new(cols, 0) }
@orientation = :right
end
def make_orientation
orientation = {right: [0, 1], left: [0, -1], up: [-1, 0], down: [1, 0]}
@current_turn = orientation[@orientation] if @current_turn == nil
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
left_turn = {right: [:up, [-1, 0]], left: [:down, [1, 0]],
up: [:left, [0, -1]], down: [:right, [0, 1]]}
@current_turn = left_turn[@orientation].last
@orientation = left_turn[@orientation].first
end
def turn_right
right_turn = {right: [:down,[1,0]], left: [:up, [-1, 0]],
up: [:right, [0, 1]], down: [:left, [0, -1]]}
@current_turn = right_turn[@orientation].last
@orientation = right_turn[@orientation].first
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)
return_var = @grid
if instance.instance_of? TurtleGraphics::Canvas::ASCII
@ascii, @html = instance, nil
else
@html, @ascii = instance, nil
end
self.instance_eval &block
return @ascii.make_ascii_string(@grid) if @ascii != nil
return @html.make_html(@grid) if @html != nil
@grid
end
end
module Canvas
class ASCII
def initialize(list_of_symbols)
@list_of_symbols = list_of_symbols
@ascii_string = ""
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
def make_ascii_string(grid)
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 << intensity(intensity_of_pixel)
end
@ascii_string << "\n"
end
@ascii_string
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def make_table(grid)
@table = "<table>"
grid.each_with_index do |elem, row|
@table << "<tr>"
elem.each_with_index do |element, col|
intensity_of_pixel = element.to_f / grid.flatten.max
intensity = "#{format('%.2f', intensity_of_pixel)}'"
@table << "<td style=\'opacity: #{intensity}></td>"
end
@table << "</tr>"
end
@table
end
def make_html(grid)
make_table(grid)
"<!DOCTYPE html>
<html>
<head>
<title>Turtle graphics</title>
<style>
table {
border-spacing: 0;
}
tr {
padding: 0;
}
td {
- width: #@pixel_sizepx;
- height: #@pixel_sizepx;
+ width: #{@pixel_size}px;
+ height: #{@pixel_size}px;
background-color: black;
padding: 0;
}
</style>
</head>
<body>
#@table
</body>
</html>"
end
end
end
end