beetlebug

Skate Pond 6: Levels from a seed

In this step you make ponds with a generator, and prove each one with a solver. You learn a random generator of your own, a level from a seed, and a search by breadth over the moves of the game.

A generator of your own

The game needs the same pond from the same seed on each computer and in each browser. The game also calls rand() for its particles, and each call moves the stream of rand(). So the generator has its own random numbers: a linear congruential generator, with its state in one variable (the code).

proc gen::seed {n} { variable rs; set rs [expr {($n * 2654435761 + 12345) % 2147483648}]; if {$rs == 0} { set rs 1 } }
proc gen::rnd {} {
    variable rs
    set rs [expr {($rs * 1103515245 + 12345) % 2147483648}]
    expr {$rs / 2147483648.0}
}
proc gen::pick {n} { expr {int([rnd] * $n)} }

gen::seed sets the state from a seed. Each call of gen::rnd gives the next number of the stream, from 0 up to 1. The arithmetic uses only integers, so each platform gives the same numbers.

A pond from a seed

gen::pond SEED STYLE MODS builds the rows of a pond from the stream of its seed (the code):

  1. A wall of snowbanks on the border, and ice inside.
  2. Snowbanks and patches of snow at random squares. The style sets the count: a maze has more snowbanks.
  3. For the style rocks, a wall of rocks across the pond and a pick. For the style water, a band of water, snow beside it, and a plank.
  4. Thin ice for the modifier Thin ice, snowflakes, and the start S.
  5. The flag, on the square that needs the most moves to reach. For the styles rocks and water, the flag goes on a square that no way reaches without the item.

The snippet makes a pond of open ice in the same way, with a smaller solver: pushes only, with no items. Change the seed.

# Change the seed: each seed gives its own pond, the same on each computer.
set seed 642
# A random generator of its own: the same seed gives the same numbers.
proc seedRnd {n} { set ::rs [expr {($n * 2654435761 + 12345) % 2147483648}]; if {$::rs == 0} { set ::rs 1 } }
proc rnd {} {
    set ::rs [expr {($::rs * 1103515245 + 12345) % 2147483648}]
    expr {$::rs / 2147483648.0}
}
proc pick {n} { expr {int([rnd] * $n)} }
# A pond of open ice: a wall, snowbanks and snow at random places, and S.
proc put {c} {
    for {set k 0} {$k < 200} {incr k} {
        set x [expr {1 + [pick 10]}]; set y [expr {1 + [pick 7]}]
        if {[lindex $::g $y $x] eq "."} { lset ::g $y $x $c; return [list $x $y] }
    }
}
proc pond {seed} {
    seedRnd $seed
    set ::g {}
    for {set y 0} {$y < 9} {incr y} {
        set r {}
        for {set x 0} {$x < 12} {incr x} { lappend r [expr {$x == 0 || $y == 0 || $x == 11 || $y == 8 ? "#" : "."}] }
        lappend ::g $r
    }
    for {set k 0} {$k < 4 + [pick 4]} {incr k} { put "#" }
    for {set k 0} {$k < 1 + [pick 3]} {incr k} { put "=" }
    put S
}
# The solver: a search by breadth over the pushes. The ice square that
# needs the most pushes gets the flag, and that count is par.
proc solve {sx sy} {
    set depth($sx,$sy) 0
    set todo [list "$sx,$sy"]
    set far "$sx,$sy"
    while {[llength $todo]} {
        set next {}
        foreach p $todo {
            lassign [split $p ,] x y
            foreach {dx dy} {0 -1 0 1 -1 0 1 0} {
                set nx $x; set ny $y
                while {[lindex $::g [expr {$ny + $dy}] [expr {$nx + $dx}]] ne "#"} {
                    incr nx $dx; incr ny $dy
                    if {[lindex $::g $ny $nx] eq "="} break
                }
                if {[info exists depth($nx,$ny)]} continue
                set depth($nx,$ny) [expr {$depth($p) + 1}]
                if {[lindex $::g $ny $nx] eq "." && $depth($nx,$ny) > $depth($far)} { set far "$nx,$ny" }
                lappend next "$nx,$ny"
            }
        }
        set todo $next
    }
    list $far $depth($far)
}
lassign [pond $seed] sx sy
lassign [solve $sx $sy] flag par
lset g {*}[lreverse [split $flag ,]] F
canvas .c -width 336 -height 224 -background #0f1b2d -highlightthickness 0
pack .c
. configure -background #0f1b2d
set y 0
foreach row $g {
    set x 0
    foreach c $row {
        set fill [dict get {# #f6fafd = #ffffff . #d4ebf7 S #24324a F #e5484d} $c]
        .c create rectangle [expr {$x * 24 + 24}] [expr {$y * 20}] [expr {$x * 24 + 48}] [expr {$y * 20 + 20}] \
            -width [expr {$c eq "=" ? 2 : 0}] -fill $fill -outline #9dbad2
        incr x
    }
    incr y
}
.c create text 168 206 -text "Seed $seed: par $par" -fill #eef5fb -font {TkFixedFont 11 bold}
Levels from a seed
A pond of the generator: snowbanks, a patch of snow, the start, the flag, and the line "Seed 642: par 4"
Live

A solver proves each pond

A random pond can have no way to the flag. The solver gen::solve finds the shortest way, or it says none (the code). It is a search by breadth, as reach of step 3 is, over a larger state:

A move is one of the four pushes or A. gen::move gives the state after a move, with the same rules as push, crossed, and use of the game (the code). The length of the shortest way is par.

Keep only good ponds

A pond can have a way and still be dull. For a pond of "New set" in the picker, gen::fresh drops a pond whose way is shorter than 5 moves. It also drops a pond of the styles rocks and water whose way does not use A, because then the item has no part in the puzzle (the code). Then it tries the next seed of the stream, up to 40 times.

The list gens has the 45 shipped ponds of the generator: a name, a seed, a style, the modifiers, par, and a theme (the code). The game keeps only these numbers and makes each pond the first time that the player needs it. "New set" adds eight ponds from new seeds, and gen::fresh makes and proves each one when the picker needs its map.

Modifiers

A modifier changes a rule of a pond. The solver plays by the changed rule, so par stays true.

Modifier The rule
Thin ice A thin square cracks into water behind the skater.
Every flake The flag stops the skater only after each snowflake.
Move limit Par is the most moves. A move that uses the last move away from the flag goes back.

Iris Open Ice 1, a pond of the generator with thin ice: a purple theme, cracked squares, three snowflakes, and the card "Par 7, Thin ice"

Next: 7. Themes.