Generate random chess position: Difference between revisions

Content added Content deleted
No edit summary
Line 677: Line 677:
=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>
<lang futurebasic>
include "NSLog.incl"

begin globals
begin globals
long grid(8, 8)
short grid(8, 8) // 1-8
end globals
end globals




local fn PlaceKings
local fn PlaceKings
'~'1
short r1, r2, c1, c2
short r1, r2, c1, c2

while (YES)
while (YES)
r1 = rnd(8)
c1 = rnd(8)
r1 = rnd(8)
r2 = rnd(8)
c1 = rnd(8)
c2 = rnd(8)
r2 = rnd(8)
c2 = rnd(8)
if ( r1 <> r2 and abs( r1 - r2) > 1 and abs( c1 - c2) > 1 )
grid(r1, c1) = asc("K")
if ( r1 != r2 and abs( r1 - r2 ) > 1 and abs( c1 - c2) > 1 )
grid(r2, c2) = asc("k")
grid(r1, c1) = asc("K")
grid(r2, c2) = asc("k")
exit fn
exit fn
end if
end if
wend
wend
end fn
end fn




local fn PlacePieces( pieces$ as Str255, isPawn as BOOL )
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)
c = rnd(8)
r = rnd(8)
c = rnd(8)
until not( ( grid(r, c) != 0 ) or ( isPawn and ( r == 7 or r == 0 ) ) )
grid(r, c) = asc( mid$( pieces$, n, 1 ) )
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
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
end fn




local fn ToFen
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 == 0 )
ch = grid(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
fen$ = fen$ + "/"
case 78 : pieceStr = @" " // White Knight

print
case 98 : pieceStr = @" ♝" // Black Bishop
next
fen$ = fen$ + " w - - 0 1"
case 66 : pieceStr = @" ♗" // White 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
end fn


local fn CreateFen
local fn CreateFen
fn PlaceKings
fn PlaceKings
fn PlacePieces( "PPPPPPPP", YES )
fn PlacePieces( "PPPPPPPP", YES )
fn PlacePieces( "pppppppp", YES )
fn PlacePieces( "pppppppp", YES )
fn PlacePieces( "RNBQBNR", NO )
fn PlacePieces( "RNBQBNR", NO )
fn PlacePieces( "rnbqbnr", NO )
fn PlacePieces( "rnbqbnr", NO )
fn ToFen
fn ToFen
end fn
end fn



randomize
randomize


window 1, @"Random Chess Position", ( 0, 0, 500, 200 )
window 1, @"Random Chess Position", ( 0, 0, 700, 400 )
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}}==
=={{header|Go}}==