Anonymous user
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 .
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 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)▼
@.=.
▲ if p<3 then call piece 'k' /*a king of each color. */
▲ else call piece substr('bnpqr', random(1, 5), 1)
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
do
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 (x=='p' & r==1)|(x=='P' & r==8) then iterate /*any promoting pawn?*/
if ux=='K' then do rr=r-1 for 3
upper z; if z=='K' then iterate # /*is a king?*/
end /*rr*/
end /*ff*/
@.r.f=x;
▲ end /*#*/
▲/*────────────────────────────────────────────────────────────────────────────*/
▲ 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 'FEN='fen "w - - 0 1" /*show Forsyth-Edwards Notation.*/
▲ return</lang>
Some older REXXes don't have a '''changestr''' BIF, so one is included here: ───► [[CHANGESTR.REX]]. <br><br>
'''output''' showing five chess positions (
<br>specifying the arguments (for Regina REXX under Windows): 96 5
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ board 1 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
....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=
</pre>
|