Beispiel #1
0
        private Piece MakeMove(Position source, Position target)
        {
            ChessPiece p = (ChessPiece)_board.RemovePiece(source);

            p.IncreaseMoveCount();
            Piece capturedPiece = _board.RemovePiece(target);

            _board.PlacePiece(p, target);

            if (capturedPiece != null)
            {
                _piecesOnTheBoard.Remove(capturedPiece);
                _capturedPieces.Add(capturedPiece);
            }

            // #specialmove castling kingside rook
            if (p is King && target.Column == source.Column + 2)
            {
                Position   sourceT = new Position(source.Row, source.Column + 3);
                Position   targetT = new Position(source.Row, source.Column + 1);
                ChessPiece rook    = (ChessPiece)_board.RemovePiece(sourceT);
                rook.IncreaseMoveCount();
                _board.PlacePiece(rook, targetT);
            }

            // #specialmove castling queenside rook
            if (p is King && target.Column == source.Column - 2)
            {
                Position   sourceT = new Position(source.Row, source.Column - 4);
                Position   targetT = new Position(source.Row, source.Column - 1);
                ChessPiece rook    = (ChessPiece)_board.RemovePiece(sourceT);
                rook.IncreaseMoveCount();
                _board.PlacePiece(rook, targetT);
            }

            // #specialmove en passant
            if (p is Pawn)
            {
                if (source.Column != target.Column && capturedPiece == null)
                {
                    Position pawnPosition;
                    if (p.Color == Color.White)
                    {
                        pawnPosition = new Position(target.Row + 1, target.Column);
                    }
                    else
                    {
                        pawnPosition = new Position(target.Row - 1, target.Column);
                    }
                    capturedPiece = _board.RemovePiece(pawnPosition);
                    _capturedPieces.Add(capturedPiece);
                    _piecesOnTheBoard.Remove(capturedPiece);
                }
            }

            return(capturedPiece);
        }