\input mftmac
$\9standard N queens programmed in \mf\par
$\9initialize; non-numerics must be declared\par
$\5{screenstrokes};\ \2{pickup}\5{pencircle}\6{scaled}.08\\{pt};$\par
$\2{boolean}r[],u[],d[],\\{loop},\\{done};$\par
$\2{string}s[];\ \2{path}p[],\\{queen},\\{checkit};$\par
$\2{message}\7"how many queens? ";$\par
$n=-1+\2{scantokens}\5{readstring};$\par
$\2{if}n<3\?:n:=3;\3{fi};\ \2{if}n>92\?:n:=92;\3{fi};$\par
$\2{message}\7"pause after each add (y/n)?";$\par
$s_{2}:=\5{readstring};\ m:=2\ast n;\ N:=n+1;\ w=\1{floor}(290/N);$\par
$\9paths and board\par
$\\{checkit}:=(-30,-30)\8{--}(0,-80)\8{--}(70,-10);$\par
$\9queen  originally: queen:=unitsquare scaled w;\par
$p_{1}:=(.25w,.15w)\8{--}(.15w,.85w)\8{--}(.3w,.5w)\8{--}(.4w,.85w)%
\8{--}(.5w,.5w);$\par
$p_{2}:=p_{1}\1{reflectedabout}((.5w,0),(.5w,w));\ \\{queen}:=p_{1}\8{..}%
\1{reverse}p_{2}\8{--}\1{cycle};$\par
$\9draw board\par
$\2{for}i:=0\4{upto}n+1\?:$\par
\quad$\2{draw}(0,w\ast i)\8{--}(w\ast N,w\ast i);\ \2{draw}(w\ast i,0)\8{--}(w%
\ast i,w\ast N);$\par
$\!\3{endfor};$\par
$\9procedures (ie macros)\par
$\9mark a square\par
$\2{def}\\{add}=b[j]:=i;\ r[i]:=\5{true};\ d[j+i]:=\5{true};\ u[j-i]:=%
\5{true};$\par
\quad$\2{fill}\\{queen}\6{shifted}(j\ast w,i\ast w);$\par
\quad$\2{if}s_{2}=\7"y"\?:$\par
\quad\quad$\2{message}\7"press enter to continue";\ s_{1}:=\5{readstring};$\par
\quad$\!\3{fi};$\par
$\!\3{enddef};$\par
$\9unmark a square\par
$\2{def}\\{rem}=r[i]:=\5{false};\ d[j+i]:=\5{false};\ u[j-i]:=\5{false};$\par
\quad$\2{unfill}\\{queen}\6{shifted}(j\ast w,i\ast w);$\par
$\!\3{enddef};$\par
$\9backtrack\par
$\2{def}\\{back}=j:=j-1;\ i:=b[j];$\par
\quad$\2{forever}\?:$\par
\quad\quad$\2{exitif}\1{not}((i=n)\6{and}(j>0));$\par
\quad\quad$\\{rem};\ j:=j-1;\ i:=b[j];$\par
\quad$\!\3{endfor};$\par
\quad$\2{if}i<n\?:$\par
\quad\quad$\\{rem};\ i:=i+1;$\par
\quad$\2{else}\?:$\par
\quad\quad$\\{done}:=\5{true};$\par
\quad$\!\3{fi};$\par
$\!\3{enddef};$\par
$\9draw check\par
$\2{def}\\{printit}=$\par
\quad$\2{pickup}\5{pencircle}\6{xscaled}.4\\{pt}\6{yscaled}.1\\{pt}%
\6{rotated}-20;$\par
\quad$\2{draw}\\{checkit};\ \2{message}\7"press enter to continue ";$\par
\quad$s_{1}:=\5{readstring};\ \2{undraw}\\{checkit};$\par
$\!\3{enddef};$\par
$\9main program\par
$i:=0;\ j:=0;\ \\{done}:=\5{false};$\par
$\2{for}k:=0\4{upto}n\?:r[k]:=\5{false};\3{endfor};$\par
$\2{for}k:=-n\4{upto}n\?:u[k]:=\5{false};\3{endfor};$\par
$\2{for}k:=0\4{upto}m\?:d[k]:=\5{false};\3{endfor};$\par
$\2{forever}\?:$\par
\quad$\\{loop}:=\5{true};$\par
\quad$\2{if}\1{not}(r[i]\6{or}d[j+i]\6{or}u[j-i])\?:$\par
\quad\quad$\\{add};$\par
\quad\quad$\2{if}j=n\?:$\par
\quad\quad\quad$\\{printit};\ \2{message}\7"show another solution (y/n)?";$\par
\quad\quad\quad$s_{3}:=\5{readstring};$\par
\quad\quad\quad$\2{if}s_{3}=\7"y"\?:$\par
\quad\quad\quad\quad$s_{2}:=\7"n";\ \\{rem};\ \\{back};$\par
\quad\quad\quad$\2{else}\?:$\par
\quad\quad\quad\quad$\\{done}:=\5{true};$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad$\2{else}\?:$\par
\quad\quad\quad$j:=j+1;\ i:=0;\ \\{loop}:=\5{false};$\par
\quad\quad$\!\3{fi};$\par
\quad$\!\3{fi};$\par
\quad$\2{if}\\{loop}\?:$\par
\quad\quad$\2{if}i=n\?:$\par
\quad\quad\quad$\\{back};$\par
\quad\quad$\2{else}\?:i:=i+1;$\par
\quad\quad$\!\3{fi};$\par
\quad$\!\3{fi};$\par
\quad$\2{exitif}\\{done};$\par
$\!\3{endfor};$\par
\BL
$\2{if}s_{3}=\7"y"\?:\2{message}\7"no more solutions";\3{fi};$\par
$\!\3{end};$\par
\BL
\BL
