begin globals short grid(8, 8) // 1-8 end globals local fn PlaceKings short r1, r2, c1, c2 while (YES) r1 = rnd(8) c1 = rnd(8) r2 = rnd(8) c2 = rnd(8) if ( abs( r1 - r2 ) > 1 or abs( c1 - c2) > 1 ) grid(r1, c1) = asc("K") grid(r2, c2) = asc("k") exit fn end if wend end fn local fn PlacePieces( pieces$ as Str255, isPawn as BOOL ) short n, r, c, numToPlace numToPlace = rnd( len$(pieces$) ) for n = 1 to numToPlace do r = rnd(8) c = rnd(8) if isPawn == YES and mid$( pieces$, n, 1 ) == "p" and r = 8 then exit fn if isPawn == YES and mid$( pieces$, n, 1 ) == "P" and r = 1 then exit fn until not( ( grid(r, c) != 0 ) or ( isPawn and ( r == 8 or r == 1 ) ) ) grid(r, c) = asc( mid$( pieces$, n, 1 ) ) next end fn local fn ToFen Str255 fen$ short ch, r, c, countEmpty = 0 CFStringRef pieceStr for r = 1 to 8 for c = 1 to 8 ch = grid(r, c) select (ch) case 107 : pieceStr = @" ♚" // Black King case 75 : pieceStr = @" ♔" // White King case 112 : pieceStr = @" ♟" // Black Pawn case 80 : pieceStr = @" ♙" // White Pawn case 114 : pieceStr = @" ♜" // Black Rook case 82 : pieceStr = @" ♖" // White Rook case 110 : pieceStr = @" ♞" // Black Knight case 78 : pieceStr = @" ♘" // White Knight case 98 : pieceStr = @" ♝" // Black Bishop case 66 : pieceStr = @" ♗" // White Bishop case 113 : pieceStr = @" ♛" // Black Queen case 81 : pieceStr = @" ♕" // White Queen end select if ( ch ) print pieceStr, else print @" .", end if if ( ch == 0 ) countEmpty++ else if ( countEmpty > 0 ) fen$ = fen$ + chr$(countEmpty + 48) countEmpty = 0 end if fen$ = fen$ + chr$(ch) end if next if ( countEmpty > 0 ) fen$ = fen$ + chr$(countEmpty + 48) countEmpty = 0 end if fen$ = fen$ + "/" print next fen$ = fen$ + " w - - 0 1" fen$ = mid$( fen$, 8, len$(fen$) ) print 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 randomize window 1, @"Random Chess Position", ( 0, 0, 700, 400 ) text @"Menlo", 32, fn ColorText, fn ColorClear, NSTextAlignmentLeft, 10 fn CreateFen HandleEvents