Skip to content

Instantly share code, notes, and snippets.

@ror3d
Last active August 29, 2015 14:19
Show Gist options
  • Save ror3d/2ba23845d65649c82ca8 to your computer and use it in GitHub Desktop.
Save ror3d/2ba23845d65649c82ca8 to your computer and use it in GitHub Desktop.

Revisions

  1. @Rcrmn Rcrmn revised this gist Apr 13, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions jigsaw_puzzle_generator.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    # Called as
    # ruby puzzle_generator.rb [-o] [-r] [_n_different_connections_]
    # -o outputs pieces in order (rows); by default outputs them in randomized order
    # -r outputs pieces randomly rotated, except the first one
    # n_different_connections defaults to log2(w*h)
    # -o - outputs pieces in order (rows); by default outputs them in randomized order
    # -r - outputs pieces randomly rotated, except the first one
    # n_different_connections - number of different connections to use. Defaults to log2(w*h)
    #
    # Reads from standard input a space/newline separated tiles map

  2. @Rcrmn Rcrmn revised this gist Apr 13, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gradient_map_generator.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    # Creates a grid of numbers, where each number is the minimum L2 (Manhattan) distance to
    # the nearest zero in the grid.
    # Called as
    # $ ruby image_generator.rb _width_ [_height_ [_num_zeroes_ [_max_num_]]]
    # height defaults to width
    # width - the number of columns of the output
    # height - defaults to width
    # num_zeroes - maximum number of zeroes in the output
    # max_num defaults to 9

    require 'optparse'
  3. @Rcrmn Rcrmn revised this gist Apr 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jigsaw_puzzle_generator.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Called as
    # ruby puzzle_generator.rb [-o] [-r] [_n_different_connections_]
    # -o outputs pieces in order (rows)
    # -r outputs pieces randomly rotated
    # -o outputs pieces in order (rows); by default outputs them in randomized order
    # -r outputs pieces randomly rotated, except the first one
    # n_different_connections defaults to log2(w*h)
    #
    # Reads from standard input a space/newline separated tiles map
  4. @Rcrmn Rcrmn revised this gist Apr 13, 2015. 1 changed file with 46 additions and 13 deletions.
    59 changes: 46 additions & 13 deletions jigsaw_puzzle_generator.rb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,23 @@
    # Called as
    # ruby puzzle_generator.rb [_n_different_connections_]
    # ruby puzzle_generator.rb [-o] [-r] [_n_different_connections_]
    # -o outputs pieces in order (rows)
    # -r outputs pieces randomly rotated
    # 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
    ordered = false
    rotate = false

    ARGV.each do |a|
    if a == '-o'
    ordered = true
    elsif a == '-r'
    rotate = true
    else
    $n_cTypes = ARGV[0].to_i if Integer(a) rescue $n_cTypes
    end
    end

    w = nil
    @@ -16,7 +27,7 @@

    eof = false
    until eof do
    line = gets
    line = $stdin.gets
    line = line.chomp unless line.nil?
    if !line.nil? and line.length > 0
    mat.push line.split(' ').map &:to_i
    @@ -58,22 +69,44 @@ def getRandomConnection

    pieces = []

    positions = []
    h.times do |y|
    w.times do |x|
    p = mat[y][x].to_s

    left = x > 0 ? 2 : 0
    positions.push [x, y]
    end
    end

    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"
    unless ordered
    positions = positions.shuffle
    end

    pieces.push p
    positions.each do |x,y|
    p = mat[y][x].to_s

    #left = x > 0 ? 2 : 0
    cn = []
    cn.push (y > 0 ? vert[y-1][x].swapcase : nil)
    cn.push (x < w-1 ? horz[y][x] : nil)
    cn.push (y < h-1 ? vert[y][x] : nil)
    cn.push (x > 0 ? horz[y][x-1].swapcase : nil)

    if rotate and pieces.length > 0 # Rotate only after the first
    cn.rotate! $rnd.rand(4)
    end

    left = cn[3] ? 2 : 0

    p = cn[3] + " " + p if cn[3]
    p = " "*left + cn[0] + "\n" + p if cn[0]
    p = p + " " + cn[1] if cn[1]
    p = p + "\n" + " "*left + cn[2] if cn[2]

    p += "\n"

    pieces.push p
    end


    pieces.each do |p|
    puts p + "\n"
    end
  5. @Rcrmn Rcrmn revised this gist Apr 13, 2015. 2 changed files with 83 additions and 5 deletions.
    8 changes: 4 additions & 4 deletions gradient_map_generator.rb
    Original file line number Diff line number Diff line change
    @@ -35,11 +35,11 @@ def dist(a,b)
    z_pos.push [prng.rand(w), prng.rand(h)]
    end

    w.times do |x|
    h.times do |y|
    img.push([])
    h.times do |y|
    img[x][y] = z_pos.map {|z| [max,(dist z,[x,y])].min}.min
    w.times do |x|
    img[y][x] = z_pos.map {|z| [max,(dist z,[x,y])].min}.min
    end
    line = img[x].map(&:to_s).join ' '
    line = img[y].map(&:to_s).join ' '
    puts line
    end
    80 changes: 79 additions & 1 deletion jigsaw_puzzle_generator.rb
    Original file line number Diff line number Diff line change
    @@ -1 +1,79 @@
    # Input: space/newline separated tiles map
    # 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
  6. @Rcrmn Rcrmn created this gist Apr 13, 2015.
    45 changes: 45 additions & 0 deletions gradient_map_generator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # 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

    w.times do |x|
    img.push([])
    h.times do |y|
    img[x][y] = z_pos.map {|z| [max,(dist z,[x,y])].min}.min
    end
    line = img[x].map(&:to_s).join ' '
    puts line
    end
    1 change: 1 addition & 0 deletions jigsaw_puzzle_generator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # Input: space/newline separated tiles map