Beispiel #1
0
 public PolyglotBoard()
 {
     _state         = new PieceType[8, 8];
     _castlingFlags = CastlingFlags.Everything;
     _colorToMove   = ColorType.White;
     _enPassantFile = -1;
 }
Beispiel #2
0
        public void MakeMove(string move)
        {
            var(fromFile, fromRank) = (move[0] - 'a', move[1] - '1');
            var(toFile, toRank)     = (move[2] - 'a', move[3] - '1');
            var pieceType    = _state[fromFile, fromRank];
            var oldEnPassant = _enPassantFile;

            // Pieces
            _state[toFile, toRank]     = _state[fromFile, fromRank];
            _state[fromFile, fromRank] = PieceType.None;
            _enPassantFile             = -1;

            // Castling
            if (pieceType == PieceType.WhiteKing)
            {
                _castlingFlags &= ~CastlingFlags.WhiteCastling;

                if (Math.Abs(fromFile - toFile) == 2)
                {
                    // Short castling
                    if (fromFile < toFile)
                    {
                        _state[7, 0] = PieceType.None;
                        _state[5, 0] = PieceType.WhiteRook;
                    }
                    // Long castling
                    else
                    {
                        _state[0, 0] = PieceType.None;
                        _state[3, 0] = PieceType.WhiteRook;
                    }
                }
            }
            else if (pieceType == PieceType.BlackKing)
            {
                _castlingFlags &= ~CastlingFlags.BlackCastling;

                if (Math.Abs(fromFile - toFile) == 2)
                {
                    // Short castling
                    if (fromFile < toFile)
                    {
                        _state[7, 7] = PieceType.None;
                        _state[5, 7] = PieceType.BlackRook;
                    }
                    // Long castling
                    else
                    {
                        _state[0, 7] = PieceType.None;
                        _state[3, 7] = PieceType.BlackRook;
                    }
                }
            }
            else if (pieceType == PieceType.WhiteRook && fromFile == 0 && fromRank == 0)
            {
                _castlingFlags &= ~CastlingFlags.WhiteLong;
            }
            else if (pieceType == PieceType.WhiteRook && fromFile == 7 && fromRank == 0)
            {
                _castlingFlags &= ~CastlingFlags.WhiteShort;
            }
            else if (pieceType == PieceType.BlackRook && fromFile == 0 && fromRank == 7)
            {
                _castlingFlags &= ~CastlingFlags.BlackLong;
            }
            else if (pieceType == PieceType.BlackRook && fromFile == 7 && fromRank == 7)
            {
                _castlingFlags &= ~CastlingFlags.BlackShort;
            }

            // En passant
            if (pieceType == PieceType.WhitePawn || pieceType == PieceType.BlackPawn)
            {
                if (Math.Abs(fromRank - toRank) == 2)
                {
                    var targetPieceType = _colorToMove == ColorType.White ? PieceType.BlackPawn : PieceType.WhitePawn;
                    if (toFile > 0 && _state[toFile - 1, toRank] == targetPieceType ||
                        toFile < 7 && _state[toFile + 1, toRank] == targetPieceType)
                    {
                        _enPassantFile = toFile;
                    }
                }
                else if (Math.Abs(fromFile - toFile) == 1 && toFile == oldEnPassant)
                {
                    if (_colorToMove == ColorType.White)
                    {
                        _state[toFile, toRank - 1] = PieceType.None;
                    }
                    else if (_colorToMove == ColorType.Black)
                    {
                        _state[toFile, toRank + 1] = PieceType.None;
                    }
                }
            }

            // Color
            _colorToMove = _colorToMove == ColorType.White ? ColorType.Black : ColorType.White;
        }