Generate random chess position: Difference between revisions

(→‎ZX Spectrum Basic: transposed board labels)
 
Line 1,454:
7 . . R . . . . K
4Q3/4Q3/3p4/7R/1rrR4/2P5/5k2/2R4K w - - 0 1
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
The program presented here, like others on this page, does not ensure
that white and black are not in check.
 
Since jq does not include a PRN generator, an external source of entropy such as /dev/urandom is assumed.
A suitable invocation of jq would be along the lines of:
<pre>
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -nr chess.jq
</pre>
<syntaxhighlight lang="jq">
### Pseuo-random numbers
 
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def sample:
if length == 0 # e.g. null or []
then null
else .[length|prn]
end;
 
def randFloat:
(1000|prn) / 1000;
 
### Chess
 
def isEmpty: . == "." or . == "•";
 
def grid:
[range(0;4) | (".","•") ] as $black
| [range(0;4) | ("•", ".")] as $white
| [range(0;4) | ($black, $white)] ;
 
# input: {grid}
def placeKings:
.done = false
| until(.done;
[8 | prn, prn, prn, prn] as [$r1, $c1, $r2, $c2]
| if $r1 != $r2 and (($r1 - $r2)|length) > 1 and (($c1 - $c2)|length) > 1
then .grid[$r1][$c1] = "K"
| .grid[$r2][$c2] = "k"
| .done = true
else .
end )
| del(.done);
 
# input: {grid}
def placePieces($pieces; $isPawn):
($pieces|length|prn) as $numToPlace
| .n = -1
| until(.n == $numToPlace;
.n += 1
| .done = false
| until(.done;
.r = (8 | prn)
| .c = (8 | prn)
| if (.grid[.r][.c] | isEmpty) and (($isPawn and (.r == 7 or .r == 0))|not)
then .done = true
else .
end )
| .grid[.r][.c] = $pieces[.n:.n+1] ) ;
 
def toFen:
{ grid,
fen: "",
countEmpty: 0 }
| reduce range (0;8) as $r (.;
reduce range(0;8) as $c (.;
.grid[$r][$c] as $ch
| .emit += " \($ch) "
| if ($ch | isEmpty)
then .countEmpty += 1
else
if .countEmpty > 0
then .fen += (.countEmpty|tostring)
| .countEmpty = 0
end
| .fen += $ch
end )
| if .countEmpty > 0
then .fen += (.countEmpty|tostring)
| .countEmpty = 0
end
| .fen += "/"
| .emit += "\n" )
| .emit,
.fen + " w - - 0 1" ;
 
def createFen:
{grid: grid}
| placeKings
| placePieces("PPPPPPPP"; true)
| placePieces("pppppppp"; true)
| placePieces("RNBQBNR"; false)
| placePieces("rnbqbnr"; false)
| toFen;
 
createFen
</syntaxhighlight>
{{output}}
<pre>
. • . • b R . •
• p q . • . • p
. K P • . p . •
• . • . • . P p
. P . • . P . •
• b n . • . • .
p p . k . p r p
• . • . • . • .
 
4bR2/1pq4p/1KP2p2/6Pp/1P3P2/1bn5/pp1k1prp/8/ w - - 0 1
</pre>
 
2,455

edits