Generate random chess position: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace; split some compound statement DO groups.
m (→‎{{header|REXX}}: added/changed comments and whitespace; split some compound statement DO groups.)
Line 1,688:
do boards=1 for abs(CBs) /* [↓] maybe display separator & title*/
if sign(CBs)\==CBs then do; say; say center(' board' boards" ", 79, '▒'); end
@.=.; !.=@. /*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' /*a king of each color. */
Line 1,697:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cb: fen=; do r=8 for 8 by -1; $= /*the board rank (so far).*/
do f=8 for 8 by -1; $= $ || @.r.f; end /*f*/ /*append the board file. */
end /*f*/
say $ /*display the board rank. */
do e=8 for 8 by -1; $= changestr( copies(., e), $, e); end /*e*/ . /*filler.≡filler*/
fen=fen || $ || left('/', r\==1) end /*append / if not 1st rank.e*/
end /*r*/ fen= fen || $ || left('/', r\==1) /*append [↑]/ if append $ strnot to1st FENrank.*/
end /*r*/ /* [↑] append $ str to FEN*/
say /*display a blank sep. line*/
say 'FEN='fen "w - - 0 1" /*Forsyth─Edwards Notation.*/
return /* [↑] display chessboard.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 and file.*/
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 /*[↓] neighbor ≡ king?*/
Line 1,718 ⟶ 1,720:
end /*rr*/ /*[↑] neighbor ≡ king?*/
end /*ff*/ /*[↑] we're all done. */
@.r.f= x x /*put random piece. */
!.r.f= ux; return /* " " " upper*/
end /*#*/ /*#: isn't incremented.*/</lang>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here: &nbsp; ───► &nbsp; [[CHANGESTR.REX]]. <br><br>