Generate random chess position: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: alternated literal quotes.) |
m (→{{header|REXX}}: changed REXX program to allow generation of an unbalanced total number of pieces (by color).) |
||
Line 102: | Line 102: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This REXX version generates balanced number of pieces (both sides have an equal number of total pieces, |
This REXX version normally generates balanced number of pieces (both sides have an equal number of total pieces, |
||
<br>but not necessarily of the same kind). |
<br>but not necessarily of the same kind). |
||
If the number of chessboard specified is negative, then the number of pieces for each color will randomized. |
|||
⚫ | |||
⚫ | |||
<lang rexx>/*REXX pgm generates a chess position (rand pieces & positions) in FEN format.*/ |
<lang rexx>/*REXX pgm generates a chess position (rand pieces & positions) in FEN format.*/ |
||
parse arg seed CBs . /*obtain optional arguments from the CL*/ |
parse arg seed CBs . /*obtain optional arguments from the CL*/ |
||
Line 111: | Line 113: | ||
if CBs =='' | CBs =',' then CBs=1 /*CBs: number of generated chessboards*/ |
if CBs =='' | CBs =',' then CBs=1 /*CBs: number of generated chessboards*/ |
||
/* [↓] maybe display any # of boards. */ |
/* [↓] maybe display any # of boards. */ |
||
do boards=1 for CBs |
do boards=1 for abs(CBs) /* [↓] maybe display separator & title*/ |
||
if CBs\==1 then do; |
if abs(CBs)\==1 then do; say; say center(' board' boards" ",79,'▒'); end |
||
@.=. /*initialize the chessboard to be empty*/ |
@.=. /*initialize the chessboard to be empty*/ |
||
do p=1 for random(2,32) /*generate a random number of chessmen.*/ |
do p=1 for random(2,32) /*generate a random number of chessmen.*/ |
||
Line 132: | Line 134: | ||
return |
return |
||
/*────────────────────────────────────────────────────────────────────────────*/ |
/*────────────────────────────────────────────────────────────────────────────*/ |
||
piece: parse arg x; if p//2 then upper x; |
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*/ |
|||
do #=0 by 0; r=random(1, 8); f=random(1, 8) /*random rank & file.*/ |
do #=0 by 0; r=random(1, 8); f=random(1, 8) /*random rank & file.*/ |
||
if @.r.f\==. then iterate /*position occupied? */ |
if @.r.f\==. then iterate /*position occupied? */ |