Generate random chess position: Difference between revisions

Line 685:
 
local fn PlaceKings
short r1, r2, c1, c2
'~'1
short r1, r2, c1, c2
while (YES)
 
r1 = rnd(8)
while (YES)
r1 c1 = rnd(8)
c1 r2 = rnd(8)
r2 c2 = rnd(8)
if ( abs( r1 - r2 ) > 1 or abs( c1 - c2) > 1 )
c2 = rnd(8)
if ( abs( r1 - r2 ) > 1 or absgrid(r1, c1 - c2) > 2= asc("K")
grid(r1r2, c1c2) = asc("Kk")
exit fn
grid(r2, c2) = asc("k")
end if
exit fn
wend
end if
wend
end fn
 
 
local fn PlacePieces( pieces$ as Str255, isPawn as BOOL )
short n, r, c, numToPlace
'~'1
short n, r, c, numToPlace
numToPlace = rnd( len$(pieces$) )
 
for n = 1 to numToPlace
numToPlace = rnd( len$(pieces$) )
do
for n = 1 to numToPlace
r = rnd(8)
do
r c = rnd(8)
if isPawn == YES and mid$( pieces$, n, 1 ) == "p" and r = 8 then exit fn
c = rnd(8)
if isPawn == YES and mid$( pieces$, n, 1 ) == "pP" and r = 81 then exit fn
if isPawn == YES anduntil mid$not( pieces$,( ngrid(r, 1c) != 0 ) or ( isPawn and ( r == "P"8 andor r == 1 then) exit) fn)
until not( ( grid(r, c) != 0 ) or asc( isPawn and mid$( rpieces$, == 8 or r ==n, 1 ) ) )
next
grid(r, c) = asc( mid$( pieces$, n, 1 ) )
next
end fn
 
 
local fn ToFen
Str255 fen$
'~'1
short ch, r, c, countEmpty = 0
Str255 fen$
CFStringRef pieceStr
short ch, r, c, countEmpty = 0
CFStringRef pieceStr
for r = 1 to 8
 
for rc = 1 to 8
ch = grid(r, c)
for c = 1 to 8
ch = grid(r, c)
select (ch)
 
select (ch)
case 107 : pieceStr = @" ♚" // Black King
 
case 107 75 : pieceStr = @" " // BlackWhite King
case 75 : pieceStr = @" ♔" // White King
case 112 : pieceStr = @" ♟" // Black Pawn
 
case 112 80 : pieceStr = @" " // BlackWhite Pawn
case 80 : pieceStr = @" ♙" // White Pawn
case 114 : pieceStr = @" ♜" // Black Rook
 
case 114 82 : pieceStr = @" " // BlackWhite Rook
case 82 : pieceStr = @" ♖" // White Rook
case 110 : pieceStr = @" ♞" // Black Knight
 
case 110 78 : pieceStr = @" " // BlackWhite Knight
case 78 : pieceStr = @" ♘" // White Knight
case 98 : pieceStr = @" ♝" // Black Bishop
 
case 9866 : pieceStr = @" " // BlackWhite Bishop
case 66 : pieceStr = @" ♗" // White Bishop
case 113 : pieceStr = @" ♛" // Black Queen
 
case 113 81 : pieceStr = @" " // BlackWhite Queen
case 81 : pieceStr = @" ♕" // White Queen
end select
 
end select
if ( ch )
 
print pieceStr,
if ( ch )
else
print pieceStr,
print @" .",
else
end if
print @" .",
if ( ch == 0 )
end if
countEmpty++
if ( ch == 0 )
else
countEmpty++
if ( countEmpty > 0 )
else
fen$ = fen$ + chr$(countEmpty + 48)
if ( countEmpty > 0 )
fen$ = fen$ + chr$( countEmpty += 48)0
end if
countEmpty = 0
fen$ = fen$ + chr$(ch)
end if
end if
fen$ = fen$ + chr$(ch)
next
end if
if ( countEmpty > 0 )
next
fen$ = fen$ + chr$(countEmpty + 48)
if ( countEmpty > 0 )
fen$ = fen$ + chr$( countEmpty += 48)0
end if
countEmpty = 0
fen$ = fen$ + "/"
end if
fen$ = fen$ + "/"print
next
print
fen$ = fen$ + " w - - 0 1"
next
fen$ = mid$( fen$, +8, " w - - 0len$(fen$) 1")
print
fen$ = mid$( fen$, 8, len$(fen$) )
text @"Menlo", 14, fn ColorText, fn ColorClear, NSTextAlignmentLeft, 10
print
print fen$
text @"Menlo", 14, fn ColorText, fn ColorClear, NSTextAlignmentLeft, 10
print fen$
end fn
 
local fn CreateFen
fn PlaceKings
fn PlacePieces( "PPPPPPPP", YES )
fn PlacePieces( "pppppppp", YES )
fn PlacePieces( "RNBQBNR", NO )
fn PlacePieces( "rnbqbnr", NO )
fn ToFen
end fn
 
715

edits