RosettaCodeData/Task/Generate-random-chess-position/Yabasic/generate-random-chess-position.basic
2023-07-01 13:44:08 -04:00

69 lines
1.6 KiB
Text

dim grid(8, 8)
sub placeKings()
local r1, r2, c1, c2
do
r1 = int(ran(8))
c1 = int(ran(8))
r2 = int(ran(8))
c2 = int(ran(8))
if (r1 <> r2 and abs(r1 - r2) > 1 and abs(c1 - c2) > 1) then
grid(r1, c1) = asc("K")
grid(r2, c2) = asc("k")
return
end if
loop
end sub
sub placePieces(pieces$, isPawn)
local n, r, c, numToPlace
numToPlace = int(ran(len(pieces$)))
for n = 0 to numToPlace-1
repeat
r = int(ran(8))
c = int(ran(8))
until(not(grid(r, c) or (isPawn and (r = 7 or r = 0))))
grid(r, c) = asc(mid$(pieces$, n, 1))
next
end sub
sub toFen()
local fen$, ch, r, c, countEmpty
for r = 0 to 8-1
for c = 0 to 8-1
ch = grid(r, c)
if ch then print chr$(ch); else print "."; end if
if not ch then
countEmpty = countEmpty + 1
else
if countEmpty then
fen$ = fen$ + chr$(countEmpty + 48)
countEmpty = 0
end if
fen$ = fen$ + chr$(ch)
end if
next
if countEmpty then
fen$ = fen$ + chr$(countEmpty + 48)
countEmpty = 0
end if
fen$ = fen$ + "/"
print
next
fen$ = fen$ + " w - - 0 1"
print fen$
end sub
sub createFen()
placeKings()
placePieces("PPPPPPPP", TRUE)
placePieces("pppppppp", TRUE)
placePieces("RNBQBNR", FALSE)
placePieces("rnbqbnr", FALSE)
toFen()
end sub
createFen()