The game

Given an   N×N   square array of zeroes or ones in an initial configuration,   and a target configuration of zeroes and ones.



The game is to transform one to the other in as few moves as possible by inverting whole numbered rows or whole lettered
columns at once   (as one move).

In an inversion.   any  1  becomes  0,   and any  0  becomes  1  for that whole row or column.



Task

Create a program to score for the Flipping bits game.

 The game should create an original random target configuration and a starting configuration.
 Ensure that the starting position is never the target position.
 The target position must be guaranteed as reachable from the starting position.   (One possible way to do this is to generate the start position by legal flips from a random target position.   The flips will always be reversible back to the target from the given start position).
 The number of moves taken so far should be shown.


Show an example of a short game here, on this page, for a   3×3   array of bits.




get "libhdr"
static $( randstate = ? $)
 
let rand() = valof 
$(  randstate := random(randstate)
    resultis randstate >> 7
$)
 
let showboard(size, board, goal) be
$(  writes(" == BOARD == ")
    for i=1 to 19 do wrch(' ')
    writes(" == GOAL ==*N")
 
    for i=1 to 2
    $(  writes("  ")
        for j=1 to size do writef(" %N",j)
        test i=1 for j=1 to 30-2*size do wrch(' ') or wrch('*N')
    $)
 
    for row=0 to size-1 for i=1 to 2
    $(  writef("%C.", 'A'+row)
        for col=0 to size-1 do 
            writef(" %N", (i=1->board,goal)!(row*size+col))
        test i=1 for j=1 to 30-2*size do wrch(' ') or wrch('*N')
    $)
$)
 
let readmove(size) = valof
$(  let ch = ? and x = ?
    writes("Enter row or column, or Q to quit: ")
    ch := rdch()
    unless ch = '*N' x := rdch() repeatuntil x = '*N'
    ch := ch | 32;
    if ch = 'q' | ch = endstreamch finish
    if 0 <= (ch-'1') < size | 0 <= (ch-'a') < size resultis ch
$) repeat
 
let flip(size, board, n, col) be 
    test col 
        do flipcol(size, board, n) 
        or fliprow(size, board, n)
and flipcol(size, board, n) be
    for i=0 to size-1 do
        board!(i*size+n) := 1-board!(i*size+n)
and fliprow(size, board, n) be
    for i=0 to size-1 do
        board!(n*size+i) := 1-board!(n*size+i)
 
let makegoal(size, goal) be
    for i=0 to size*size-1 do goal!i := rand() & 1
 
let makeboard(size, board, goal) be
$(  for i=0 to size*size-1 do board!i := goal!i
    for i=0 to 10+2*(rand() & 15) do
        flip(size, board, rand() rem size, rand() & 1)
$)
 
let win(size, board, goal) = valof  
$(  for i=0 to size*size-1 
        unless board!i = goal!i resultis false
    resultis true
$)
 
let play(size, board, goal) be
$(  let moves = 0 and move = ?
    $(  showboard(size, board, goal)
        wrch('*N')
        if win(size, board, goal) 
        $(  writef("You won in %N moves!*N", moves)
            return
        $)
        moves := moves + 1
        move := readmove(size)
        test '0' <= move <= '9'
            do flipcol(size, board, move-'1')
            or fliprow(size, board, move-'a')
    $) repeat 
$)
 
let start() be
$(  let board = vec 63 and goal = vec 63
    let size = ?
    writef("****** FLIP THE BITS *******N")
    $(  writef("Size (3-8)? ")
        size := readn()
    $) repeatuntil 3 <= size <= 8
    writef("Random number seed? ")
    randstate := readn()
    wrch('*N')
    makegoal(size, goal)
    makeboard(size, board, goal)
    play(size, board, goal)
$)