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) $)