Георги обнови решението на 02.12.2015 13:30 (преди около 9 години)
+class Array
+ def to_cells(tag)
+ self.map { |c| "<#{tag}>#{c}</#{tag}>" }.join
+ end
+end
+
+class TurtleGraphics
+
+ def work(word, args)
+ counter = 0
+ while counter < word.length
+ if counter <= 3
+ word.gsub!("#{counter}", args[counter])
+ else
+ word.gsub!("#{counter}", args[3])
+ end
+ counter += 1
+ end
+ puts format(word)
+ end
+
+ def format(word)
+ count, counter = 0, 1
+ while counter < word.length
+ if counter % @row == 0
+ word.insert(counter + count, "\n")
+ counter, count = counter + 1, count + 1
+ else
+ counter += 1
+ end
+ end
+ word
+ end
+
+ def generate_table(number, field)
+ rows = [{:a => "", :b => ""}, {:c => "", :d => ""}]
+ cells = rows.map do |row|
+ "<tr>#{row.values.to_cells('td')}</tr>"
+ end.join("\n ")
+ table = "<table>\n#{cells}\n</table>"
+ puts table
+ end
+
+ class Canvas
+ class ASCII
+ def initialize(*args)
+ @symbols = args
+ end
+
+ def get_symbols
+ @symbols.flatten
+ end
+ end
+
+ class HTML
+ def initialize(number)
+ @number = number
+ end
+
+ def get_number
+ @number
+ end
+ end
+ end
+
+ class Turtle < TurtleGraphics
+ def initialize(row = 0, column = 0)
+ @row = row
+ @column = column
+ @position = [0, 0]
+ @next_position = [0, 1]
+ @field = Array.new(row){Array.new(column){0}}
+ #@field[@position[0]][@position[1]] += 1
+ end
+
+ def get_field
+ @field
+ end
+
+ def next_position(row, column)
+ @next_position[0] = row
+ @next_position[1] = column
+ end
+
+ def spawn_at(row = 0, column = 0)
+ @position = [row, column]
+ @field[@position[0]][@position[1]] += 1
+ end
+
+ def look(orientation)
+ case orientation
+ when :up then next_position(1, 0)
+ when :down then next_position(-1, 0)
+ when :left then next_position(0, -1)
+ when :right then next_position(0, 1)
+ end
+ end
+
+ def draw(*args, &block)
+ self.instance_eval(&block)
+ word = @field.flatten.join
+ if args == []
+ self.get_field
+ elsif args[0].class == TurtleGraphics::Canvas::HTML
+ generate_table(args[0].get_number, self.get_field)
+ else
+ work(word, args[0].get_symbols)
+ end
+ end
+
+ def move
+ if @field[@position[0]][@position[1]] == 0
+ @field[@position[0]][@position[1]] += 1
+ end
+ @position[0] += @next_position[0]
+ @position[1] += @next_position[1]
+ @field[@position[0]][@position[1]] += 1
+ @field
+ end
+
+ def turn_right
+ case @next_position
+ when [0, 1] then @next_position = [1, 0]
+ when [1, 0] then @next_position = [0, -1]
+ when [0, -1] then @next_position = [-1, 0]
+ when [-1, 0] then @next_position = [0, 1]
+ end
+ end
+
+ def turn_left
+ case @next_position
+ when [0, 1] then @next_position = [-1, 0]
+ when [-1, 0] then @next_position = [0, -1]
+ when [0, -1] then @next_position = [1, 0]
+ when [1, 0] then @next_position = [0, 1]
+ end
+ end
+ end
+end
Единствено не успях да направя HTML таблицата, или поне до някъде го добутах :\
Имаш сериозни стилови проблеми:
- В
TurtleGraphics
има методи, които ползваш като глобални. - Има забравени принтирания и коментари.
-
format
иwork
са много лоши имена за методи. Все още не мога да се ориентирам какво трябва да правиwork
с аргументите сиword
иargs
. И някаквиgsub!
-ове вътре. - Дефинирането на нови методи в съществуващи класове (monkey-patching) е лоша практика. Казвали сме го на лекции.
Прегледай нашето решение за идеи и другия път започни по-рано с решението.