Generate random chess position: Difference between revisions
→{{header|FutureBasic}}
No edit summary |
|||
Line 677:
=={{header|FutureBasic}}==
<lang futurebasic>
include "NSLog.incl"
begin globals
end globals
local fn PlaceKings
'~'1
short r1, r2, c1, c2
while (YES)
c2 = rnd(8)
if ( r1 != r2 and
grid(r2, c2) = asc("k")
exit fn
end if
wend
end fn
local fn PlacePieces( pieces$ as Str255, isPawn as BOOL )
'~'1
short n, r, c, numToPlace
numToPlace = rnd( len$(pieces$) )
for n = 1 to numToPlace
do
c = rnd(8)
if
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
'~'1
Str255 fen$
short ch, r, c, countEmpty = 0
CFStringRef pieceStr
for r = 1 to 8
for c = 1 to 8
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 :
case 98 : pieceStr = @" ♝" // Black Bishop
case
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
end fn
randomize
window 1, @"Random Chess Position", ( 0, 0,
text @"Menlo", 32, fn ColorText, fn ColorClear, NSTextAlignmentLeft, 10
fn CreateFen
NSLogSetFont( fn FontWithName( @"Menlo", 24 ) )
NSLogSetTabInterval( 30 )
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
HandleEvents
</lang>
=={{header|Go}}==
|