Jump to content

Generate random chess position: Difference between revisions

→‎{{header|REXX}}: added capability to generate any number of chessboards, simplified searching the neighborhood for kings.
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:
 
=={{header|REXX}}==
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.*/
parse arg seed CBs . /*obtain optional argumentarguments from the CL.*/
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) /* [↓] generatemaybe display randomany # of chessmenboards. */
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/*initialize kingthe or other piece.chessboard to be empty*/
call FEN do p=1 for random(2,32) /*displaygenerate thea random FEN fornumber theof chessboardchessmen.*/
if p<3 then call piece 'k' /*a king of each color. */
else call piece substr('bnpqr', random(1, 5), 1)
say $ end /*p*/ /* [↑] place a piece. /*display board rank.*/
@.=. call cb /*initializedisplay the chessboard to defaultand its FEN.*/
end end /*rboards*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
piececb: parsefen=; arg x;do if p//2r=8 thenfor upper8 x by -1; arg$= ux /*use whiteboard ifrank odd(so Pfar).*/
do #f=08 for 8 by 0-1; r$=random(1,8);$ || @.r.f=random(1,8); end /*randomf*/ rank & /*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</lang>
/*────────────────────────────────────────────────────────────────────────────*/
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 (x=='p' & r==1)|(x=='P' & r==8) then iterate /*any promoting pawn?*/
 
if ux=='K' then do rr=-1 for 3 /*[↓] neighbor≡king?*/
if ux=='K' then do rr=r-1 for 3 do ff=-1 for/*[↓] 3; r_=r+rr; f_=f+ff /*neighbor.neighbor≡king?*/
zdo ff=@.r_.f_;f-1 for upper z3; if z=='K' @.rr.ff then/*obtain iteratethe #neighbor*/
upper z; if z=='K' then iterate # /*is a king?*/
end /*rr*/
end /*ff*/
@.r.f=x; return /*place random piece.*/
returnend /*#*/</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>
'''output''' &nbsp; showing five chess positions (usingstarting somewith randoma chessspecific position by seeding the random BIF with &nbsp; '''96'''):,
<br>specifying the arguments (for Regina REXX under Windows): &nbsp; 96 &nbsp; 5
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 1 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
....p...
.n..Nb.N....
qr.Q.p......
B....K..P.
N..b..P....
.nQR.N.p.k...
.K.pp......
.P.Q...k...
....p....
 
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=4p3qnqN3N/1n2Nb1N3B3N/qr1Q1p2n4P1Q/B5P1PP2b1Rn/N2b2P1KqB5/1nQR1N1ppB1knp2/1K1pp3q1n1qB2/1P1Q3k7N w - - 0 1
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.