# Called as # ruby puzzle_generator.rb [_n_different_connections_] # n_different_connections defaults to log2(w*h) # # Reads from standard input a space/newline separated tiles map $n_cTypes = nil if ARGV.length > 0 $n_cTypes = ARGV[0].to_i end w = nil h = 0 mat = [] $rnd = Random.new eof = false until eof do line = gets line = line.chomp unless line.nil? if !line.nil? and line.length > 0 mat.push line.split(' ').map &:to_i w = w ? ([w, mat.last.length].min) : mat.last.length h+=1 else eof = true end end if $n_cTypes.nil? || $n_cTypes < 1 $n_cTypes = Math.log2(w*h).floor + 1 end horz = [] vert = [] def getRandomConnection rnd = $rnd l = ('a'.ord + rnd.rand($n_cTypes)).chr if rnd.rand(2) == 0 l = l.upcase end return l end h.times do |y| horz.push [] vert.push [] w.times do |x| horz.last.push getRandomConnection vert.last.push getRandomConnection end end pieces = [] h.times do |y| w.times do |x| p = mat[y][x].to_s left = x > 0 ? 2 : 0 p = horz[y][x-1].swapcase + " " + p if x > 0 p = " "*left + vert[y-1][x].swapcase + "\n" + p if y > 0 p = p + " " + horz[y][x] if x < w-1 p = p + "\n" + " "*left + vert[y][x] if y < h-1 p += "\n" pieces.push p end end pieces.each do |p| puts p + "\n" end