Generate random chess position: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: updated a sample output.)
(→‎{{header|REXX}}: added capability to generate any number of chessboards, simplified searching the neighborhood for kings.)
Line 102: Line 102:


=={{header|REXX}}==
=={{header|REXX}}==
This REXX version generates balanced pieces   (both sides have an equal number of total pieces).
This REXX version generates balanced number of pieces   (both sides have an equal number of total pieces,
<br>but not necessarily of the same kind).

It also allows any number of chessboards to be displayed.
<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 . /*obtain optional argument from the CL.*/
parse arg seed CBs . /*obtain optional arguments from the CL*/
if seed\=='' & seed\="," then call random ,,seed /*RANDOM repeatability? */
if seed\=='' & seed\="," then call random ,,seed /*RANDOM repeatability? */
if CBs =='' | CBs =',' then CBs=1 /*CBs: number of generated chessboards*/
@.=. /*initialize the chessboard to default.*/
do p=1 for random(2,32) /* [↓] generate random # of chessmen. */
/* [↓] maybe display any # of boards. */
do boards=1 for CBs /* [↓] maybe display separator & title*/
if p<3 then call piece 'k'
if CBs\==1 then do; say; say center(' board' boards" ", 79, "▒"); end
else call piece substr('bnpqr',random(1,5),1)
end /*p*/ /* [↑] place a king or other piece. */
@.=. /*initialize the chessboard to be empty*/
call FEN /*display the FEN for the chessboard.*/
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. */
else call piece substr('bnpqr', random(1, 5), 1)
end /*p*/ /* [↑] place a piece. */
call cb /*display the chessboard and its FEN.*/
end /*boards*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
piece: parse arg x; if p//2 then upper x; arg ux /*use white if odd P.*/
cb: fen=; do r=8 for 8 by -1; $= /*board rank (so far).*/
do #=0 by 0; r=random(1,8); f=random(1,8) /*random rank & file.*/
do f=8 for 8 by -1; $=$ || @.r.f; end /*f*/ /*append file.*/
say $ /*display board rank. */
do e=8 for 8 by -1; $=changestr(copies(., e), $, e); end /*e*/
fen=fen || $ || left('/', r\==1) /*append / if not the 1st rank.*/
end /*r*/
say /*a blank line (after the board).*/
say 'FEN='fen "w - - 0 1" /*show Forsyth-Edwards Notation.*/
return
/*────────────────────────────────────────────────────────────────────────────*/
piece: parse arg x; if p//2 then upper x; arg ux /*use white if odd P.*/
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? */
if (x=='p' & r==1)|(x=='P' & r==8) then iterate /*any promoting pawn?*/
if (x=='p' & r==1)|(x=='P' & r==8) then iterate /*any promoting pawn?*/

if ux=='K' then do rr=-1 for 3 /*[↓] neighbor≡king?*/
do ff=-1 for 3; r_=r+rr; f_=f+ff /*neighbor.*/
if ux=='K' then do rr=r-1 for 3 /*[↓] neighbor≡king?*/
z=@.r_.f_; upper z; if z=='K' then iterate #
do ff=f-1 for 3; z=@.rr.ff /*obtain the neighbor*/
upper z; if z=='K' then iterate # /*is a king?*/
end /*rr*/
end /*rr*/
end /*ff*/
end /*ff*/
@.r.f=x /*place random piece.*/
@.r.f=x; return /*place random piece.*/
return
end /*#*/</lang>
end /*#*/
/*────────────────────────────────────────────────────────────────────────────*/
FEN: fen=; do r=8 for 8 by -1; $=
do f=8 for 8 by -1; $=$ || @.r.f; end /*f*/
say $ /*display board rank.*/
do e=8 for 8 by -1; $=changestr(copies(.,e),$,e); end /*e*/
fen=fen || $ || left('/',r\==1) /*append / if not the 1st rank.*/
end /*r*/
say
say 'FEN='fen "w - - 0 1" /*show Forsyth-Edwards Notation.*/
return</lang>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here: &nbsp; ───► &nbsp; [[CHANGESTR.REX]]. <br><br>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here: &nbsp; ───► &nbsp; [[CHANGESTR.REX]]. <br><br>
'''output''' &nbsp; (using some random chess position):
'''output''' &nbsp; showing five chess positions (starting with a specific position by seeding the random BIF with &nbsp; '''96'''),
<br>specifying the arguments (for Regina REXX under Windows): &nbsp; 96 &nbsp; 5
<pre>
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 1 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
....p...
.n..Nb.N
........
qr.Q.p..
........
B.....P.
....K...
N..b..P.
........
.nQR.N.p
....k...
.K.pp...
........
.P.Q...k
........
........

FEN=8/8/4K3/8/4k3/8/8/8 w - - 0 1

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 2 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
...Q..r.
r.kbn...
...pqn.p
..N.N.qQ
p....N.n
..N.....
...NB...
.R.Q.KBP

FEN=3Q2r1/r1kbn3/3pqn1p/2N1N1qQ/p4N1n/2N5/3NB3/1R1Q1KBP w - - 0 1

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 3 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
.....B.n
N...p.p.
.q..n...
.K..R...
.BP...P.
q....k..
........
R.N..n..

FEN=5B1n/N3p1p1/1q2n3/1K2R3/1BP3P1/q4k2/8/R1N2n2 w - - 0 1

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 4 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
......K.
pN..q...
...qN...
....n...
.....Q..
........
....k...
P.r....B

FEN=6K1/pN2q3/3qN3/4n3/5Q2/8/4k3/P1r4B w - - 0 1

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 5 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
qnqN...N
...B...N
n....P.Q
PP..b.Rn
KqB.....
pB.knp..
q.n.qB..
.......N


FEN=4p3/1n2Nb1N/qr1Q1p2/B5P1/N2b2P1/1nQR1N1p/1K1pp3/1P1Q3k w - - 0 1
FEN=qnqN3N/3B3N/n4P1Q/PP2b1Rn/KqB5/pB1knp2/q1n1qB2/7N w - - 0 1
</pre>
</pre>