Jump to content

Generate random chess position: Difference between revisions

m
Line 704:
replaceSquareByPos ((ChessPiece King Black), p2) b)
 
placePawns :: PieceColor -> ChessBoard -> IO ChessBoard
placePawns c b = num >>= go b
where go :: ChessBoard -> Int -> IO ChessBoard
go b'' 0 = pure b''
go b'' n = do
ppos <- randomPos b''
pawn@(ChessPiece _ color) <- randomPawn
if promoted c == snd p || isPosOccupied p b''
if promoted || enpassant ccolor == snd ppos || firstPosisPosOccupied c == sndpos pb''
|| enpassant color == snd pos || firstPos color == snd pos
then go b'' n
else
go (replaceSquareByPos ((ChessPiece Pawn c)pawn, ppos) b'') (pred n)
promoted White = 8
promoted Black = 1
Line 723 ⟶ 724:
 
num :: IO Int
num = getStdRandom (randomR (1,816))
 
randomPawn :: IO Square
randomPawn = ChessPiece Pawn <$> rColor
where rColor :: IO PieceColor
rColor = getStdRandom (random)
 
placeRemaining :: ChessBoard -> IO ChessBoard
Line 786 ⟶ 792:
main =
placeKings emptyBoard >>=
placePawns White >>=
placePawns Black >>=
placeRemaining >>=
draw</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.