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.




// Flipping Bits game.
// Transform a start grid to an end grid by flipping rows or columns.
 
size = 3
 
board = []
goal = []
for i in range(1,size)
    row = []
    for j in range(1,size)
        row.push (rnd > 0.5)
    end for
    board.push row
    goal.push row[0:]
end for
 
flipRow = function(n)
    for j in range(0, size-1)
        board[n-1][j] = not board[n-1][j]
    end for
end function
 
flipCol = function(n)
    for i in range(0, size-1)
        board[i][n-1] = not board[i][n-1]
    end for
end function
 
flipAny = function(s)
    s = s[0].upper
    if s >= "A" then flipCol s.code - 64 else flipRow val(s)
end function
 
for scramble in range(20)
    if rnd < 0.5 then flipRow ceil(rnd*size) else flipCol ceil(rnd*size)
end for
 
solved = function()
    for i in range(0, size-1)
        for j in range(0, size-1)
            if board[i][j] != goal[i][j] then return false
        end for
    end for
    return true
end function
 
moveCount = 0
while true
    print "  CURRENT:" + " "*(4+size*3) + "GOAL:"
    for i in range(1,size)
        s = i + " " + str(board[i-1])
        s = s + " "*(3+size*3) + str(goal[i-1])
        print s
    end for
    s = "   "
    for i in range(1,size)
        s = s + char(64+i) + "  "
    end for
    print s
    if solved then break
    moveCount = moveCount + 1
    inp = input("Move " + moveCount + "? ")
    flipAny(inp)
end while
print "You did it!"