------ art_94466_28847338.1181489956051
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Here is my solution for the Mexican Blanket Quiz.
It's also available here:
http://code.rendale.com/svn/RubyQuiz/MexicanBlanket_127/Mexican_Blanket.html<http://code.rendale.com/svn/RubyQuiz/MexicanBlanket>
I was having a hard time seeing the pattern in the ascii so I added a
feature to convert the ascii string into an html image composed of colored
divs.
The colors in the blanket are randomly selected from an array of possible
colors and gradients are alternately separated by Mexican flag bands and
black bands. The technique I use is to create a string with enough of the
pattern to fill the entire blanket and then shift back by one unit per row.
Any feedback on how I can improve this code would be much appreciated.
-------------------------------------------------------------------------------------------------------------
#!/usr/local/bin/ruby
module RubyQuiz
#creates the pattern
class Pattern
attr_reader :colors
attr_reader :value
def initialize(length)
@length ength
@color_count :color1, :color2}
@colors Hash["0","black","1","red","2","orange","3","yellow","4","green","5","blue","6","indigo","7","violet","8","white","9","grey"]
@value
@flag_switch rue
generate
end
def generate
pick_new_colors
(0..(@length.to_f/6).ceil).each do |i|
@value + @color1.to_s * @color_count[:color1]) + (@color2.to_s *
@color_count[:color2])
update_colors
end
end
private
def update_colors
if @color_count[:color1] > 1
@color_count[:color1]- @color_count[:color2]+ else
@color_count[:color1],@color_count[:color2]P1
@flag_switch ? add_flag : add_seperator
pick_new_colors
end
end
def add_flag
@value + 01840"
@flag_switch alse
end
def add_seperator
@value + 000"
@flag_switch rue
end
def pick_new_colors
@color1 rand*9).ceil
@color2 color1 < 2 ? @color1 + 1 : @color1 - 1
end
end
# composes the pattern into a blanket
class Blanket
attr_reader :ascii
attr_accessor :scale
def initialize(widthu,height0, scale
@width, @height, @scale idth.to_i, height.to_i, scale.to_i
@ascii, @html "",""]
@pattern attern.new(@width+@height)
weave
end
private
def weave
0.upto(@height) do |i|
@ascii + @pattern.value[0 + i,@width])[0,@width] + "\n"
end
puts @ascii + "\n"
puts "open Mexican_Blanket.html in your interweb browser to see it
in full color"
html
end
def html
@html <html>\n<head>\n<title>Mexican Blanket
Solution</title>\n</head>\n"
@html + body>\n"
@html
+ style>\nbody{background:#ccc;font-weight:bold;text-align:center}\ndiv.pixel{float:left;
width:#{@scale}px; height:#{@scale}px;}\n.wrapper{border:2px solid #fff;
width:#{@width*@scale}px; margin:auto;}\n</style>\n"
@html + div class wrapper\">\n"
@ascii.split("\n").each do |line|
line.gsub (/(.)/) do |char|
@html + <div class pixel\" style
background:#{@pattern.colors[char]}\"></div>"
end
end
@html + \n<div style clear:both\"></div>\n</div>"
@html + \n<div><pre>\n#{@ascii}\n</pre></div>\n"
@html + </body>\n</html>"
to_file
end
def to_file
output_file ile.open("Mexican_Blanket.html","w")do |f|
f.write(@html)
end
end
end
end
# Blanket.new accepts height, width, and scale (used for html version)
if __FILE__ $0
blanket ubyQuiz::Blanket.new(*ARGV)
end
-------------------------------------------------------------------------------------------------------------
------ art_94466_28847338.1181489956051--