Решение на Шеста задача от Димитър Узунов

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

Към профила на Димитър Узунов

Резултати

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

Код

module TurtleGraphics
class Turtle
ORIENTATIONS = [:left, :up, :right, :down]
DIRECTIONS = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
def initialize(height, width)
@matrix = TurtleGraphics::Canvas::Matrix.new(height, width)
@matrix[0, 0] = 1
@orientation = :right
@position = [0, 0]
end
def draw(canvas = @matrix)
instance_eval(&Proc.new) if block_given?
canvas.draw(@matrix)
end
def spawn_at(row, column)
@position = [row, column]
@matrix[0, 0] = 0
@matrix[row, column] = 1
end
def look(orientation)
@orientation = orientation
end
def turn_left
look ORIENTATIONS[ORIENTATIONS.find_index(@orientation) - 1]
end
def turn_right
look ORIENTATIONS[(ORIENTATIONS.find_index(@orientation) + 1) % 4]
end
def move
next_position_row, next_position_column = next_position
if next_position_row < 0
@position = [@matrix.height - 1, next_position_column]
elsif next_position_row >= @matrix.height
@position = [0, next_position_column]
elsif next_position_column < 0
@position = [next_position_row, @matrix.width - 1]
elsif next_position_column >= @matrix.width
@position = [next_position_row, 0]
end
@matrix[*@position] += 1
end
private
def next_position
direction_row, direction_column = DIRECTIONS[@orientation]
position_row, position_column = @position
@position = [position_row + direction_row, position_column + direction_column]
end
end
end
module TurtleGraphics::Canvas
class Matrix
attr_reader :height, :width
def initialize(height, width)
@height = height
@width = width
@matrix = Array.new(@height) { Array.new(@width, 0) }
end
def [](row, column)
@matrix[row][column]
end
def []=(row, column, value)
@matrix[row][column] = value
end
def find_max_element
@matrix.reduce(@matrix.first.max) do |current_max, row|
row_max = row.max
row_max > current_max ? row_max : current_max
end
end
def intensity_matrix
max_element = find_max_element
@matrix.map { |row| row.map { |cell| cell.to_f / max_element } }
end
def draw(_ = nil)
@matrix
end
end
class ASCII
def initialize(symbols)
@symbols = symbols
end
def draw(matrix)
matrix.intensity_matrix.map { |row| symbols_row(row) }.join("\n")
end
private
def boundaries
interval_start, interval_end = 0, 0
intervals_count = @symbols.size - 1
(intervals_count).times do |index|
part = (index + 1).to_f
interval_start, interval_end = interval_end, part / intervals_count
yield interval_start, interval_end
end
end
def intervals
enum_for(:boundaries).map do |range_begin, range_end|
(range_begin.to_f.next_float..range_end)
end
end
def symbols_for_intervals
symbols = intervals.zip(@symbols.drop(1)).to_h
symbols[(0..0)] = @symbols.first
symbols
end
def symbol_for_intensity(intensity)
symbols_for_intervals.select { |interval| interval.include?(intensity) }.
values.first
end
def symbols_row(matrix_row)
matrix_row.map { |intensity| symbol_for_intensity(intensity) }.join
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def draw(matrix)
opening_tags + table(matrix) + closing_tags
end
private
def opening_tags
"<!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>"
end
def pixel(opacity)
"<td style=\"opacity: #{opacity}\"></td>"
end
def pixels_row(matrix_row)
"<tr>" +
matrix_row.map { |intensity| pixel(format('%.2f', intensity)) }.join("\n") +
"</tr>"
end
def table(matrix)
"<table>" +
matrix.intensity_matrix.map { |row| pixels_row(row) }.join("\n") +
"</table>"
end
def closing_tags
"</body></html>"
end
end
end

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

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

Finished in 0.014 seconds
14 examples, 0 failures

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

Димитър обнови решението на 01.12.2015 11:34 (преди над 8 години)

+module TurtleGraphics
+ class Turtle
+ ORIENTATIONS = [:left, :up, :right, :down]
+ DIRECTIONS = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
+
+ def initialize(height, width)
+ @matrix = TurtleGraphics::Canvas::Matrix.new(height, width)
+ @matrix[0, 0] = 1
+
+ @orientation = :right
+ @position = [0, 0]
+ end
+
+ def draw(canvas = @matrix)
+ instance_eval(&Proc.new) if block_given?
+
+ canvas.draw(@matrix)
+ end
+
+ def spawn_at(row, column)
+ @position = [row, column]
+
+ @matrix[0, 0] = 0
+ @matrix[row, column] = 1
+ end
+
+ def look(orientation)
+ @orientation = orientation
+ end
+
+ def turn_left
+ look ORIENTATIONS[ORIENTATIONS.find_index(@orientation) - 1]
+ end
+
+ def turn_right
+ look ORIENTATIONS[(ORIENTATIONS.find_index(@orientation) + 1) % 4]
+ end
+
+ def move
+ next_position_row, next_position_column = next_position
+
+ if next_position_row < 0
+ @position = [@matrix.height - 1, next_position_column]
+ elsif next_position_row >= @matrix.height
+ @position = [0, next_position_column]
+ elsif next_position_column < 0
+ @position = [next_position_row, @matrix.width - 1]
+ elsif next_position_column >= @matrix.width
+ @position = [next_position_row, 0]
+ end
+
+ @matrix[*@position] += 1
+ end
+
+ private
+
+ def next_position
+ direction_row, direction_column = DIRECTIONS[@orientation]
+ position_row, position_column = @position
+
+ new_position = [position_row + direction_row,
+ position_column + direction_column]
+
+ @position = new_position
+ new_position
+ end
+ end
+end
+
+module TurtleGraphics::Canvas
+ class Matrix
+ attr_reader :height, :width
+
+ def initialize(height, width)
+ @height = height
+ @width = width
+ @matrix = Array.new(@height) { Array.new(@width, 0) }
+ end
+
+ def [](row, column)
+ @matrix[row][column]
+ end
+
+ def []=(row, column, value)
+ @matrix[row][column] = value
+ end
+
+ def find_max_element
+ @matrix.reduce(@matrix.first.max) do |current_max, row|
+ row_max = row.max
+ row_max > current_max ? row_max : current_max
+ end
+ end
+
+ def intensity_matrix
+ max_element = find_max_element
+
+ @matrix.map { |row| row.map { |cell| cell.to_f / max_element } }
+ end
+
+ def draw(_ = nil)
+ @matrix
+ end
+ end
+
+ class ASCII
+ def initialize(symbols)
+ @symbols = symbols
+ end
+
+ def draw(matrix)
+ matrix.intensity_matrix.map { |row| symbols_row(row) }.join("\n")
+ end
+
+ private
+
+ def boundaries
+ interval_start, interval_end = 0, 0
+ intervals_count = @symbols.size - 1
+
+ (intervals_count).times do |index|
+ part = (index + 1).to_f
+ interval_start, interval_end = interval_end, part / intervals_count
+
+ yield interval_start, interval_end
+ end
+ end
+
+ def intervals
+ enum_for(:boundaries).map do |range_begin, range_end|
+ (range_begin.to_f.next_float..range_end)
+ end
+ end
+
+ def symbols_for_intervals
+ symbols = intervals.zip(@symbols.drop(1)).to_h
+ symbols[(0..0)] = @symbols.first
+ symbols
+ end
+
+ def symbol_for_intensity(intensity)
+ symbols_for_intervals.select { |interval| interval.include?(intensity) }.
+ values.first
+ end
+
+ def symbols_row(matrix_row)
+ matrix_row.map { |intensity| symbol_for_intensity(intensity) }.join
+ end
+ end
+
+ class HTML
+ def initialize(pixel_size)
+ @pixel_size = pixel_size
+ end
+
+ def draw(matrix)
+ opening_tags + table(matrix) + closing_tags
+ end
+
+ private
+
+ def opening_tags
+ "<!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>"
+ end
+
+ def pixel(opacity)
+ "<td style=\"opacity: #{opacity}\"></td>"
+ end
+
+ def pixels_row(matrix_row)
+ "<tr>" +
+ matrix_row.map { |intensity| pixel(format('%.2f', intensity)) }.join("\n") +
+ "</tr>"
+ end
+
+ def table(matrix)
+ "<table>" +
+ matrix.intensity_matrix.map { |row| pixels_row(row) }.join("\n") +
+ "</table>"
+ end
+
+ def closing_tags
+ "</body></html>"
+ end
+ end
+end

Димитър обнови решението на 01.12.2015 11:37 (преди над 8 години)

module TurtleGraphics
class Turtle
ORIENTATIONS = [:left, :up, :right, :down]
DIRECTIONS = {left: [0, -1], up: [-1, 0], right: [0, 1], down: [1, 0]}
def initialize(height, width)
@matrix = TurtleGraphics::Canvas::Matrix.new(height, width)
@matrix[0, 0] = 1
@orientation = :right
@position = [0, 0]
end
def draw(canvas = @matrix)
instance_eval(&Proc.new) if block_given?
canvas.draw(@matrix)
end
def spawn_at(row, column)
@position = [row, column]
@matrix[0, 0] = 0
@matrix[row, column] = 1
end
def look(orientation)
@orientation = orientation
end
def turn_left
look ORIENTATIONS[ORIENTATIONS.find_index(@orientation) - 1]
end
def turn_right
look ORIENTATIONS[(ORIENTATIONS.find_index(@orientation) + 1) % 4]
end
def move
next_position_row, next_position_column = next_position
if next_position_row < 0
@position = [@matrix.height - 1, next_position_column]
elsif next_position_row >= @matrix.height
@position = [0, next_position_column]
elsif next_position_column < 0
@position = [next_position_row, @matrix.width - 1]
elsif next_position_column >= @matrix.width
@position = [next_position_row, 0]
end
@matrix[*@position] += 1
end
private
def next_position
direction_row, direction_column = DIRECTIONS[@orientation]
position_row, position_column = @position
- new_position = [position_row + direction_row,
- position_column + direction_column]
-
- @position = new_position
- new_position
+ @position = [position_row + direction_row, position_column + direction_column]
end
end
end
module TurtleGraphics::Canvas
class Matrix
attr_reader :height, :width
def initialize(height, width)
@height = height
@width = width
@matrix = Array.new(@height) { Array.new(@width, 0) }
end
def [](row, column)
@matrix[row][column]
end
def []=(row, column, value)
@matrix[row][column] = value
end
def find_max_element
@matrix.reduce(@matrix.first.max) do |current_max, row|
row_max = row.max
row_max > current_max ? row_max : current_max
end
end
def intensity_matrix
max_element = find_max_element
@matrix.map { |row| row.map { |cell| cell.to_f / max_element } }
end
def draw(_ = nil)
@matrix
end
end
class ASCII
def initialize(symbols)
@symbols = symbols
end
def draw(matrix)
matrix.intensity_matrix.map { |row| symbols_row(row) }.join("\n")
end
private
def boundaries
interval_start, interval_end = 0, 0
intervals_count = @symbols.size - 1
(intervals_count).times do |index|
part = (index + 1).to_f
interval_start, interval_end = interval_end, part / intervals_count
yield interval_start, interval_end
end
end
def intervals
enum_for(:boundaries).map do |range_begin, range_end|
(range_begin.to_f.next_float..range_end)
end
end
def symbols_for_intervals
symbols = intervals.zip(@symbols.drop(1)).to_h
symbols[(0..0)] = @symbols.first
symbols
end
def symbol_for_intensity(intensity)
symbols_for_intervals.select { |interval| interval.include?(intensity) }.
values.first
end
def symbols_row(matrix_row)
matrix_row.map { |intensity| symbol_for_intensity(intensity) }.join
end
end
class HTML
def initialize(pixel_size)
@pixel_size = pixel_size
end
def draw(matrix)
opening_tags + table(matrix) + closing_tags
end
private
def opening_tags
"<!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>"
end
def pixel(opacity)
"<td style=\"opacity: #{opacity}\"></td>"
end
def pixels_row(matrix_row)
"<tr>" +
matrix_row.map { |intensity| pixel(format('%.2f', intensity)) }.join("\n") +
"</tr>"
end
def table(matrix)
"<table>" +
matrix.intensity_matrix.map { |row| pixels_row(row) }.join("\n") +
"</table>"
end
def closing_tags
"</body></html>"
end
end
end