Anonymous user
Generate random chess position: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→{{header|REXX}}: fixed a typo.) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 213:
This version also allows any number of chessboards to be displayed.
<lang rexx>/*REXX
parse arg seed CBs . /*obtain optional arguments from the CL*/
if datatype(seed
if CBs
/* [↓] maybe display any # of boards. */
do boards=1 for abs(CBs) /* [↓] maybe display separator & title*/
if
@.=. /*initialize the chessboard to be empty*/
do p=1 for random(2, 32) /*generate a random number of chessmen.*/
if p<3 then call piece 'k'
else call piece substr('bnpqr', random(1, 5), 1)
end /*p*/
call cb /*display the ChessBoard and its FEN.*/
end /*boards*/ /* [↑] CB ≡ ─ ─ */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cb: fen=; do r=8 for 8 by -1;
do f=8 for 8 by -1;
say $ /*display the board rank. */
do e=8 for 8 by -1;
fen=fen || $ || left('/', r\==1) /*append
end /*r*/ /* [↑] append $ str to FEN*/
say /*display a blank
say 'FEN='fen "w - - 0 1"
return /* [↑]
/*──────────────────────────────────────────────────────────────────────────────────────*/
piece: parse arg x; if p//2 then upper x; arg ux /*use white if odd P.*/
if CBs<0 & p>2 then if random(1) then upper x /*CBs>0? Use balanced.*/
/*[↓] # isn't changed.*/
do #=0 by 0; r=random(1, 8); f=random(1, 8) /*random rank
if @.r.f\==. then iterate /*is position occupied?
if (x=='p' & r==1) | (x=='P' & r==8) then iterate /*any promoting pawn? */
/*[↑] skip these pawns*/
if ux=='K' then do rr=r-1 for 3 /*[↓]
do ff=f-1 for 3;
end /*rr*/ /*[↑]
end /*ff*/ /*[↑] we're all done. */
@.r.f=x; return /*place random piece. */
end /*#*/ /*#: not incremented. */</lang>
Some older REXXes don't have a '''changestr''' BIF, so one is included here: ───► [[CHANGESTR.REX]]. <br><br>
'''output''' showing five chess positions (starting with a specific position by seeding the '''random''' BIF with '''96'''),
|