Esempio n. 1
0
 public CacheKey(ChessRepresentation chessRepresentation, ChessPlayer chessPlayer, MoveGeneratorStyle generatorStyle)
 {
     ChessRepresentation = chessRepresentation.Clone();
     ChessPlayer         = chessPlayer;
     GeneratorStyle      = generatorStyle;
 }
Esempio n. 2
0
        private ChessRepresentation ApplyMove(ChessRepresentation representationParam, BaseMove move, bool validateMove)
        {
            if (validateMove && !ValidateMove(representationParam, move))
            {
                throw new ChessIllegalMoveException();
            }

            var representation = representationParam.Clone();

            // Removing en passant flags if the move wasn't a special move. (Usually DrawOffer)
            if (move is BaseChessMove)
            {
                var pawns = Positions.PositionList.Select(x => representation[x])
                            .Where(x => x != null)
                            .Where(x => x.Owner != representation.CurrentPlayer)
                            .OfType <Pawn>()
                            .Select(x => x)
                            .ToArray();

                foreach (var pawn in pawns)
                {
                    pawn.IsEnPassantCapturable = false;
                }
            }

            switch (move)
            {
            case SpecialMove _:
                break;

            case KingCastlingMove castlingMove:
                representation.Move(castlingMove.From, castlingMove.To);
                representation.Move(castlingMove.RookFrom, castlingMove.RookTo);
                representation[castlingMove.To].HasMoved     = true;
                representation[castlingMove.RookTo].HasMoved = true;
                break;

            case PawnEnPassantMove pawnEnPassantMove:
                representation.Move(pawnEnPassantMove.From, pawnEnPassantMove.To);
                representation[pawnEnPassantMove.CapturePosition] = null;
                representation[pawnEnPassantMove.To].HasMoved     = true;
                break;

            case PawnPromotionalMove pawnPromotionalMove:
                representation.Move(pawnPromotionalMove.From, pawnPromotionalMove.To);
                representation[pawnPromotionalMove.To]          = ChessPieces.Create(pawnPromotionalMove.PromoteTo, representation[pawnPromotionalMove.To].Owner);
                representation[pawnPromotionalMove.To].HasMoved = true;
                break;

            case BaseChessMove chessMove:
                var movingPiece = representation[chessMove.From];
                representation.Move(chessMove.From, chessMove.To);
                representation[chessMove.To].HasMoved = true;

                // Setting en passant flag if needed
                if (movingPiece.Kind == PieceKind.Pawn && Math.Abs(chessMove.From.Row - chessMove.To.Row) == 2 && chessMove.From.Column == chessMove.To.Column)
                {
                    ((Pawn)representation[chessMove.To]).IsEnPassantCapturable = true;
                }
                break;
            }

            representation.TogglePlayer();
            representation.History.Add(move);

            return(representation);
        }