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 . |
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*/ |
|||
⚫ | |||
/* [↓] maybe display any # of boards. */ |
|||
do boards=1 for CBs /* [↓] maybe display separator & title*/ |
|||
⚫ | |||
if CBs\==1 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.*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*────────────────────────────────────────────────────────────────────────────*/ |
/*────────────────────────────────────────────────────────────────────────────*/ |
||
cb: fen=; do r=8 for 8 by -1; $= /*board rank (so far).*/ |
|||
do |
do f=8 for 8 by -1; $=$ || @.r.f; end /*f*/ /*append file.*/ |
||
say $ /*display board rank. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
say /*a blank line (after the board).*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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?*/ |
|||
if ux=='K' then do rr=r-1 for 3 /*[↓] neighbor≡king?*/ |
|||
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 |
@.r.f=x; return /*place random piece.*/ |
||
end /*#*/</lang> |
|||
⚫ | |||
⚫ | |||
FEN: fen=; do r=8 for 8 by -1; $= |
|||
do f=8 for 8 by -1; $=$ || @.r.f; end /*f*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
say |
|||
⚫ | |||
⚫ | |||
Some older REXXes don't have a '''changestr''' BIF, so one is included here: ───► [[CHANGESTR.REX]]. <br><br> |
Some older REXXes don't have a '''changestr''' BIF, so one is included here: ───► [[CHANGESTR.REX]]. <br><br> |
||
'''output''' ( |
'''output''' showing five chess positions (starting with a specific position by seeding the random BIF with '''96'''), |
||
<br>specifying the arguments (for Regina REXX under Windows): 96 5 |
|||
<pre> |
<pre> |
||
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 1 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
|||
⚫ | |||
. |
........ |
||
........ |
|||
....K... |
|||
........ |
|||
. |
....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= |
FEN=qnqN3N/3B3N/n4P1Q/PP2b1Rn/KqB5/pB1knp2/q1n1qB2/7N w - - 0 1 |
||
</pre> |
</pre> |
||