Skip to content

Instantly share code, notes, and snippets.

@ror3d
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save ror3d/2ba23845d65649c82ca8 to your computer and use it in GitHub Desktop.

Select an option

Save ror3d/2ba23845d65649c82ca8 to your computer and use it in GitHub Desktop.
Generators for the jigsaw code puzzle challenge.
# Called as
# $ ruby image_generator.rb _width_ [_height_ [_num_zeroes_ [_max_num_]]]
# height defaults to width
# max_num defaults to 9
require 'optparse'
w = ARGV[0].to_i
h = w
max = 999999
zeroes = 1
if ARGV.length > 1
h = ARGV[1].to_i
if ARGV.length > 2
zeroes = ARGV[2].to_i
if ARGV.length > 3
max = ARGV[3].to_i
end
end
end
def dist(a,b)
#a.zip(b).map {|l| (l[0]-l[1]).abs}.max
a.zip(b).map {|l| (l[0]-l[1]).abs}.reduce &:+
end
prng = Random.new
img = []
z_pos = []
zeroes.times do |i|
z_pos.push [prng.rand(w), prng.rand(h)]
end
h.times do |y|
img.push([])
w.times do |x|
img[y][x] = z_pos.map {|z| [max,(dist z,[x,y])].min}.min
end
line = img[y].map(&:to_s).join ' '
puts line
end
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment