Generate random chess position: Difference between revisions

No edit summary
Line 677:
=={{header|FutureBasic}}==
<lang futurebasic>
include "NSLog.incl"
 
begin globals
longshort grid(8, 8) // 1-8
end globals
 
 
local fn PlaceKings
'~'1
short r1, r2, c1, c2
short r1, r2, c1, c2
 
while (YES)
while (YES)
r1 = rnd(8)
c1r1 = rnd(8)
r2c1 = rnd(8)
c2r2 = rnd(8)
c2 = rnd(8)
if ( r1 <> r2 and abs( r1 - r2) > 1 and abs( c1 - c2) > 1 )
if ( r1 != r2 and gridabs( r1, c1- r2 ) => asc1 and abs("K" c1 - c2) > 1 )
grid(r2r1, c2c1) = asc("kK")
grid(r2, c2) = asc("k")
exit fn
exit fn
end if
end if
wend
wend
end fn
 
 
local fn PlacePieces( pieces$ as Str255, isPawn as BOOL )
'~'1
long n, r, c, numToPlace
short n, r, c, numToPlace
 
numToPlace = rnd( len$(pieces$) )
numToPlace = rnd( len$(pieces$) )
for n = 0 to numToPlace-1
for n = 1 to numToPlace
do
do
r = rnd(8)
cr = rnd(8)
c = rnd(8)
until not( ( grid(r, c) != 0 ) or ( isPawn and ( r == 7 or r == 0 ) ) )
if grid(r, c)isPawn == asc(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
next
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$
Str255 fen$
long ch, r, c, countEmpty = 0
short ch, r, c, countEmpty = 0
CFStringRef pieceStr
for r = 0 to 8 - 1
 
for c = 0 to 8 - 1
for r = 1 to 8
ch = grid(r, c)
for c = 1 to 8
if ch then print chr$(32) + chr$(ch) + chr$(32), else print chr$(32) + "." + chr$(32),
if ( ch == 0grid(r, c)
 
countEmpty++
select (ch)
else
 
if ( countEmpty > 0 )
case 107 : pieceStr = @" ♚" // Black King
fen$ = fen$ + chr$(countEmpty + 48)
case 75 : pieceStr = @" ♔" // White King
countEmpty = 0
 
end if
case 112 : pieceStr = @" ♟" // Black Pawn
fen$ = fen$ + chr$(ch)
case 80 : pieceStr = @" ♙" // White Pawn
end if
 
next
case 114 : pieceStr = @" ♜" // Black Rook
if ( countEmpty > 0 )
case 82 : pieceStr = @" ♖" // White Rook
fen$ = fen$ + chr$(countEmpty + 48)
 
countEmpty = 0
case 110 : pieceStr = @" ♞" // Black Knight
end if
case 78 : fen$pieceStr = fen$ +@" " /"/ White Knight
 
print
case 98 : pieceStr = @" ♝" // Black Bishop
next
case fen$66 =: fen$pieceStr += @" w♗" -// -White 0 1"Bishop
 
print
case 113 : pieceStr = @" ♛" // Black Queen
print fen$
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, 500700, 200400 )
fn CreateFen
 
HandleEvents
</lang>
 
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}}==
715

edits