Решение на Шеста задача от Георги Стефанов

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

Към профила на Георги Стефанов

Резултати

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

Код

class TurtleGraphics
class TurtleGraphics::Turtle
@@movement = { :right => [1, 0],
:up => [0, -1],
:left => [-1, 0],
:down => [0, +1] }
attr_accessor :canvas, :position, :orientation, :max_opacity
def initialize(rows, columns)
@canvas = []
for i in 1..rows
@canvas << Array.new(columns, 0)
end
@position, @canvas[0][0], @orientation, @max_opacity = [0, 0], 1, :right, 1
end
def draw(palette = nil, &block)
instance_eval(&block) if block_given?
palette.class == NilClass ? canvas : palette.paint(canvas, max_opacity)
end
def move
@position[1] += @@movement[orientation][0]
@position[1] %= canvas[0].size
@position[0] += @@movement[orientation][1]
@position[0] %= canvas.size
@canvas[position[0]][position[1]] += 1
@max_opacity += 1 if max_opacity < canvas[position[0]][position[1]]
end
def turn_left
@orientation = case orientation
when :right then :up
when :up then :left
when :left then :down
when :down then :right
end
end
def turn_right
3.times { turn_left }
end
def spawn_at(row, column)
canvas[0][0], position[0], position[1] = 0, row, column
canvas[row][column] = 1
end
def look(orientation)
@orientation = orientation
end
end
class TurtleGraphics::Canvas
class TurtleGraphics::Canvas::ASCII
attr_accessor :spectrum
def initialize(spectrum)
@spectrum = spectrum
end
def brush(number, max_opacity)
color = 0
until(number.fdiv(max_opacity)) <= color * (1.0) / (spectrum.size - 1)
color += 1
end
spectrum[color]
end
def paint(array, max_opacity)
array.map { |row| row.map { |number| brush(number, max_opacity) } }
.map { |x| x.join("") }.join("\n")
end
end
class TurtleGraphics::Canvas::HTML
attr_accessor :pixel_size
@@td = "\t\s\s<td style=\"opacity: "
@@td_end = "\"></td>"
@@body_start = "\n<body>\n\s\s<table>\n\t<tr>\n"
@@body_end = "\n\t</tr>\n\s\s</table>\n</body>\n</html>"
def initialize(pixel_size)
@pixel_size = pixel_size
@head = "<!DOCTYPE html>\n<html>\n<head>\n\s\s<title>Turtle "\
"graphics</title>\n\n\n\s\s<style>\n\ttable {\n\t\s\s"\
"border-spacing: 0;\n\t}\n\n\n\ttr {\n\t\s\spadding: 0"\
";\n\t}\n\n\n\ttd {\n\t\s\swidth: #{@pixel_size}px;\n\t"\
"\s\sheight: #{pixel_size}px;\n\n\n\t\s\sbackground-colo"\
"r: black;\n\t\s\spadding: 0;\n\t}\n\s\s</style>\n</head>"
end
def brush(number, max_opacity)
number.fdiv(max_opacity).round(2)
end
def paint_row(row, max_opacity)
row.map { |number| @@td + "#{brush(number, max_opacity)}" + @@td_end }
end
def combine_rows(array, max_opacity)
array.map { |row| paint_row(row, max_opacity)}
.map { |row| row.join("\n")}
.join("\n\t</tr>\n\t<tr>\n")
end
def paint(array, max_opacity)
message = combine_rows(array, max_opacity)
print @head + @@body_start + "#{message}" + @@body_end
end
end
end
end

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

...........<!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>
	  <td style="opacity: 1.0"></td>
	  <td style="opacity: 1.0"></td>
	  <td style="opacity: 1.0"></td>
	</tr>
	<tr>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
	<tr>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
  </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>
	  <td style="opacity: 1.0"></td>
	  <td style="opacity: 1.0"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
	<tr>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
	<tr>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
  </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>
	  <td style="opacity: 1.0"></td>
	  <td style="opacity: 0.67"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
	<tr>
	  <td style="opacity: 0.33"></td>
	  <td style="opacity: 0.33"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
	<tr>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	  <td style="opacity: 0.0"></td>
	</tr>
  </table>
</body>
</html>F

Failures:

  1) 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-1i57e6s/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 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-1i57e6s/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)>'

  3) 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-1i57e6s/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.01073 seconds
14 examples, 3 failures

Failed examples:

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

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

Георги обнови решението на 30.11.2015 19:39 (преди над 8 години)

+class TurtleGraphics
+ class TurtleGraphics::Turtle
+ @@movement = { :right => [1, 0],
+ :up => [0, -1],
+ :left => [-1, 0],
+ :down => [0, +1] }
+
+ attr_accessor :canvas, :position, :orientation, :max_opacity
+
+ def initialize(rows, columns)
+ @canvas = []
+ for i in 1..rows
+ @canvas << Array.new(columns, 0)
+ end
+ @position, @canvas[0][0], @orientation, @max_opacity = [0, 0], 1, :right, 1
+ end
+
+ def draw(palette = nil, &block)
+ instance_eval(&block) if block_given?
+ palette.class == NilClass ? canvas : palette.paint(canvas, max_opacity)
+ end
+
+ def move
+ @position[1] += @@movement[@orientation][0]
+ @position[1] %= canvas[0].size
+ @position[0] += @@movement[@orientation][1]
+ @position[0] %= canvas.size
+ canvas[@position[0]][@position[1]] += 1
+ @max_opacity += 1 if max_opacity < canvas[@position[0]][@position[1]]
+ end
+
+ def turn_left
+ @orientation = case @orientation
+ when :right then :up
+ when :up then :left
+ when :left then :down
+ when :down then :right
+ end
+ end
+
+ def turn_right
+ 3.times { turn_left }
+ end
+
+ def spawn_at(row, column)
+ @canvas[0][0] , @position[0], @position[1] = 0, row, column
+
+ @canvas[row][column] = 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+ end
+
+ class TurtleGraphics::Canvas
+
+ class TurtleGraphics::Canvas::ASCII
+ attr_accessor :spectrum
+
+ def initialize(spectrum)
+ @spectrum = spectrum
+ end
+
+ def brush(number, max_opacity)
+ color = 0
+ until(number.fdiv(max_opacity)) <= color * (1.0) / (spectrum.size - 1)
+ color += 1
+ end
+ spectrum[color]
+ end
+
+ def paint(array, max_opacity)
+ array.map { |row| row.map { |number| brush(number, max_opacity) } }
+ .map { |x| x.join("\t") }.join("\n")
+ end
+ end
+
+ class TurtleGraphics::Canvas::HTML
+ attr_accessor :pixel_size
+
+ @@td_start = "<td style=\"opacity: "
+ @@td_end = "\"></td>"
+ @@body_start = "\n<body>\n<table>\n<tr>\n"
+ @@body_end = "\n</tr>\n</table>\n</body>\n</html>"
+
+ def initialize(pixel_size)
+ @pixel_size = pixel_size
+
+ @head = "<!DOCTYPE html>\n<html>\n<head>\n<title>Turtle graphics</title>"\
+ "\n<style>\ntable{border-spacing: 0;}\ntr {padding: 0;}\ntd"\
+ "{width: #{@pixel_size}px; height: #{pixel_size}px;\nbackground"\
+ "-color: black;\npadding: 0;}\n</style>\n</head>"
+ end
+
+ def brush(number, max_opacity)
+ number.fdiv(max_opacity).round(2)
+ end
+
+ def paint_row(row, max_opacity)
+ row.map { |number| @@td_start + "#{brush(number,max_opacity)}" + @@td_end }
+ end
+
+ def combine_rows(array, max_opacity)
+ array.map { |row| paint_row(row, max_opacity)}
+ .map { |row| row.join("\n")}
+ .join("\n</tr>\n<tr>\n")
+ end
+
+ def paint(array, max_opacity)
+ message = combine_rows(array, max_opacity)
+
+ print @head + @@body_start + "#{message}" + @@body_end
+ end
+ end
+ end
+end

Георги обнови решението на 01.12.2015 12:51 (преди над 8 години)

class TurtleGraphics
class TurtleGraphics::Turtle
@@movement = { :right => [1, 0],
:up => [0, -1],
:left => [-1, 0],
:down => [0, +1] }
attr_accessor :canvas, :position, :orientation, :max_opacity
def initialize(rows, columns)
@canvas = []
for i in 1..rows
@canvas << Array.new(columns, 0)
end
@position, @canvas[0][0], @orientation, @max_opacity = [0, 0], 1, :right, 1
end
def draw(palette = nil, &block)
instance_eval(&block) if block_given?
palette.class == NilClass ? canvas : palette.paint(canvas, max_opacity)
end
def move
- @position[1] += @@movement[@orientation][0]
+ @position[1] += @@movement[orientation][0]
@position[1] %= canvas[0].size
- @position[0] += @@movement[@orientation][1]
+ @position[0] += @@movement[orientation][1]
@position[0] %= canvas.size
- canvas[@position[0]][@position[1]] += 1
- @max_opacity += 1 if max_opacity < canvas[@position[0]][@position[1]]
+ @canvas[position[0]][position[1]] += 1
+ @max_opacity += 1 if max_opacity < canvas[position[0]][position[1]]
end
def turn_left
- @orientation = case @orientation
+ @orientation = case orientation
when :right then :up
when :up then :left
when :left then :down
when :down then :right
end
end
def turn_right
3.times { turn_left }
end
def spawn_at(row, column)
- @canvas[0][0] , @position[0], @position[1] = 0, row, column
+ canvas[0][0], position[0], position[1] = 0, row, column
- @canvas[row][column] = 1
+ canvas[row][column] = 1
end
def look(orientation)
@orientation = orientation
end
end
class TurtleGraphics::Canvas
class TurtleGraphics::Canvas::ASCII
attr_accessor :spectrum
def initialize(spectrum)
@spectrum = spectrum
end
def brush(number, max_opacity)
color = 0
until(number.fdiv(max_opacity)) <= color * (1.0) / (spectrum.size - 1)
color += 1
end
spectrum[color]
end
def paint(array, max_opacity)
array.map { |row| row.map { |number| brush(number, max_opacity) } }
- .map { |x| x.join("\t") }.join("\n")
+ .map { |x| x.join("") }.join("\n")
end
end
class TurtleGraphics::Canvas::HTML
attr_accessor :pixel_size
- @@td_start = "<td style=\"opacity: "
+ @@td = "\t\s\s<td style=\"opacity: "
@@td_end = "\"></td>"
- @@body_start = "\n<body>\n<table>\n<tr>\n"
- @@body_end = "\n</tr>\n</table>\n</body>\n</html>"
+ @@body_start = "\n<body>\n\s\s<table>\n\t<tr>\n"
+ @@body_end = "\n\t</tr>\n\s\s</table>\n</body>\n</html>"
def initialize(pixel_size)
@pixel_size = pixel_size
- @head = "<!DOCTYPE html>\n<html>\n<head>\n<title>Turtle graphics</title>"\
- "\n<style>\ntable{border-spacing: 0;}\ntr {padding: 0;}\ntd"\
- "{width: #{@pixel_size}px; height: #{pixel_size}px;\nbackground"\
- "-color: black;\npadding: 0;}\n</style>\n</head>"
+ @head = "<!DOCTYPE html>\n<html>\n<head>\n\s\s<title>Turtle "\
+ "graphics</title>\n\n\n\s\s<style>\n\ttable {\n\t\s\s"\
+ "border-spacing: 0;\n\t}\n\n\n\ttr {\n\t\s\spadding: 0"\
+ ";\n\t}\n\n\n\ttd {\n\t\s\swidth: #{@pixel_size}px;\n\t"\
+ "\s\sheight: #{pixel_size}px;\n\n\n\t\s\sbackground-colo"\
+ "r: black;\n\t\s\spadding: 0;\n\t}\n\s\s</style>\n</head>"
end
def brush(number, max_opacity)
number.fdiv(max_opacity).round(2)
end
def paint_row(row, max_opacity)
- row.map { |number| @@td_start + "#{brush(number,max_opacity)}" + @@td_end }
+ row.map { |number| @@td + "#{brush(number, max_opacity)}" + @@td_end }
end
def combine_rows(array, max_opacity)
array.map { |row| paint_row(row, max_opacity)}
.map { |row| row.join("\n")}
- .join("\n</tr>\n<tr>\n")
+ .join("\n\t</tr>\n\t<tr>\n")
end
def paint(array, max_opacity)
message = combine_rows(array, max_opacity)
print @head + @@body_start + "#{message}" + @@body_end
end
end
end
end

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

  • Какво прави този for там?
  • Тези \n\t неща са излишни, а дори и да не бяха, така определено нищо не може да се разчете.
  • Принтираш на разни места
  • Ползваш паралелно присвояване на няколко (> 2) променливи
  • Използваш класови променливи като константи
class TurtleGraphics::Canvas

    class TurtleGraphics::Canvas::ASCII

Второто не трябва да е дефинирано с пълното си име, защото вече си "отворил" TurtleGraphics::ASCII

Благодаря! Ех, наистина можеше да ползвам просто each. Принтирането не знам защо съм го сложил, много невнимание. Ще се постарая повече на следващата задача. :D Иначе имам въпрос относно резултатите от тестовете - защо canvas ми е nil? Показва , че няма метод gsub за nil от NilClass, което значи че canvas е nil в този момент на call-ването.