Esempio n. 1
0
        private static void GenerateMove(ChessPositionIndex squareFrom, ChessPositionIndex squareTo, ChessPieceType pieceType, ChessPieceType promotionPieceType, List <ChessMove> moveList)
        {
            ChessMove move = new ChessMove(squareFrom, squareTo, pieceType);

            move.PromotionPieceType = promotionPieceType;
            moveList.Add(move);
        }
Esempio n. 2
0
 public ChessMoveInfo()
 {
     this.MovedBy            = ChessPieceColor.None;
     this.CapturedPiecePos   = ChessPositionIndex.None;
     this.SecondaryFromIndex = ChessPositionIndex.None;
     this.SecondaryToIndex   = ChessPositionIndex.None;
 }
Esempio n. 3
0
        public void CapturePiece(ChessPositionIndex index)
        {
            ChessPiece piece = new ChessPiece();

            if (GetPiece(index, ref piece))
            {
                capturedPieces.Add(piece);
            }
            SetEmpty(index);
        }
Esempio n. 4
0
        public bool IsTypeAndColor(ChessPositionIndex index, ChessPieceType type, ChessPieceColor color)
        {
            bool       res   = false;
            ChessPiece piece = new ChessPiece();

            if (GetPiece(index, ref piece))
            {
                res = (piece.PieceColor == color && piece.PieceType == type);
            }
            return(res);
        }
Esempio n. 5
0
        public bool IsPiece(ChessPositionIndex index, ChessPieceType pieceType, ChessPieceColor pieceColor)
        {
            bool       res   = false;
            ChessPiece piece = new ChessPiece();

            if (GetPiece(index, ref piece))
            {
                if (piece.PieceColor == pieceColor && piece.PieceType == pieceType)
                {
                    res = true;
                }
            }
            return(res);
        }
Esempio n. 6
0
        internal void GetKing(ChessPieceColor chessPieceColor, out ChessPositionIndex kingIndex)
        {
            kingIndex = ChessPositionIndex.A1;
            Byte   piece = (Byte)((Byte)ChessPieceType.King | (Byte)chessPieceColor);
            UInt64 pos   = 1;

            for (ChessPositionIndex index = ChessPositionIndex.A1; index <= ChessPositionIndex.H8; index++, pos <<= 1)
            {
                if ((GetBoardState(chessPieceColor).King & pos) != 0)
                {
                    kingIndex = index;
                    break;
                }
            }
        }
Esempio n. 7
0
        public static List <ChessMove> GetPossibleMovesFrom(ChessBoard board, ChessPositionIndex index)
        {
            List <ChessMove> moveList = new List <ChessMove>();
            List <ChessMove> moves    = new List <ChessMove>();
            ChessPiece       piece    = new ChessPiece();

            if (board.GetPiece(index, ref piece))
            {
                switch (piece.PieceType)
                {
                case ChessPieceType.Pawn:
                    AddOnePawnMoves(board, piece.PieceColor, (uint)index, moveList);
                    break;

                case ChessPieceType.Knight:
                    AddOneKnightMoves(board, piece.PieceColor, (uint)index, moveList);
                    break;

                case ChessPieceType.Bishop:
                    AddOneBishopMoves(board, piece.PieceColor, (uint)index, moveList);
                    break;

                case ChessPieceType.Rook:
                    AddOneRookMoves(board, piece.PieceColor, (uint)index, moveList);
                    break;

                case ChessPieceType.Queen:
                    AddOneQueenMoves(board, piece.PieceColor, (uint)index, moveList);
                    break;

                case ChessPieceType.King:
                    AddKingMoves(board, piece.PieceColor, moveList, 0);
                    break;
                }

                // Check so the moves do not put own king in check.
                foreach (ChessMove move in moveList)
                {
                    ChessBoard resBoard = new ChessBoard(board);
                    resBoard.ApplyMove(piece.PieceColor, move, null);
                    if (!IsInCheck(resBoard, piece.PieceColor))
                    {
                        moves.Add(move);
                    }
                }
            }
            return(moves);
        }
Esempio n. 8
0
        public bool GetPiece(ChessPositionIndex index, out ChessPieceType pieceType, out ChessPieceColor pieceColor)
        {
            bool   res = false;
            UInt64 pos = (UInt64)1 << (int)index;

            pieceType  = ChessPieceType.None;
            pieceColor = ChessPieceColor.None;

            for (ChessPieceColor color = ChessPieceColor.White; color <= ChessPieceColor.Black; color++)
            {
                ChessBoardColorState boardState = GetBoardState(color);

                if ((boardState.Pieces & pos) != 0)
                {
                    pieceColor = color;
                    if ((boardState.Pawns & pos) != 0)
                    {
                        pieceType = ChessPieceType.Pawn;
                    }
                    else if ((boardState.Knights & pos) != 0)
                    {
                        pieceType = ChessPieceType.Knight;
                    }
                    else if ((boardState.Bishops & pos) != 0)
                    {
                        pieceType = ChessPieceType.Bishop;
                    }
                    else if ((boardState.Rooks & pos) != 0)
                    {
                        pieceType = ChessPieceType.Rook;
                    }
                    else if ((boardState.Queens & pos) != 0)
                    {
                        pieceType = ChessPieceType.Queen;
                    }
                    else if ((boardState.King & pos) != 0)
                    {
                        pieceType = ChessPieceType.King;
                    }

                    res = true;
                    break;
                }
            }

            return(res);
        }
Esempio n. 9
0
        public bool GetPiece(ChessPositionIndex index, ref ChessPiece piece)
        {
            ChessPieceType  pieceType;
            ChessPieceColor pieceColor;
            bool            res = GetPiece(index, out pieceType, out pieceColor);

            if (res)
            {
                piece.PieceType  = pieceType;
                piece.PieceColor = pieceColor;
            }
            else
            {
                piece.PieceType  = ChessPieceType.None;
                piece.PieceColor = ChessPieceColor.None;
            }
            return(res);
        }
Esempio n. 10
0
        private void MoveRookWhenCastling(ChessPieceColor color, ChessPositionIndex fromIndex, ChessPositionIndex toIndex, ChessMoveInfo moveInfo)
        {
            ulong rookFromPos = Precomputed.IndexToBitBoard[(int)fromIndex];
            ulong rookToPos   = Precomputed.IndexToBitBoard[(int)toIndex];

            ChessBoardColorState boardState = GetBoardState(color);

            boardState.Pieces &= ~rookFromPos;
            boardState.Pieces |= rookToPos;

            boardState.Rooks &= ~rookFromPos;
            boardState.Rooks |= rookToPos;

            if (moveInfo != null)
            {
                moveInfo.SecondaryFromIndex = fromIndex;
                moveInfo.SecondaryToIndex   = toIndex;
            }
        }
Esempio n. 11
0
        internal void SetEmpty(ChessPositionIndex index)
        {
            UInt64 pos = ~((UInt64)1 << (int)index);

            whiteBoardState.Pieces  &= pos;
            whiteBoardState.Pawns   &= pos;
            whiteBoardState.Knights &= pos;
            whiteBoardState.Bishops &= pos;
            whiteBoardState.Rooks   &= pos;
            whiteBoardState.Queens  &= pos;
            whiteBoardState.King    &= pos;

            blackBoardState.Pieces  &= pos;
            blackBoardState.Pawns   &= pos;
            blackBoardState.Knights &= pos;
            blackBoardState.Bishops &= pos;
            blackBoardState.Rooks   &= pos;
            blackBoardState.Queens  &= pos;
            blackBoardState.King    &= pos;
        }
Esempio n. 12
0
        public void SetPiece(ChessPositionIndex index, ChessPieceType pieceType, ChessPieceColor pieceColor)
        {
            UInt64 pos = (UInt64)1 << (int)index;

            if (pieceColor == ChessPieceColor.None)
            {
                SetEmpty(index);
            }
            else
            {
                ChessBoardColorState boardState = GetBoardState(pieceColor);

                boardState.Pieces |= pos;
                switch (pieceType)
                {
                case ChessPieceType.Pawn:
                    boardState.Pawns |= pos;
                    break;

                case ChessPieceType.Knight:
                    boardState.Knights |= pos;
                    break;

                case ChessPieceType.Bishop:
                    boardState.Bishops |= pos;
                    break;

                case ChessPieceType.Rook:
                    boardState.Rooks |= pos;
                    break;

                case ChessPieceType.Queen:
                    boardState.Queens |= pos;
                    break;

                case ChessPieceType.King:
                    boardState.King |= pos;
                    break;
                }
            }
        }
Esempio n. 13
0
        public bool MakeMove(int fromX, int fromY, int toX, int toY, ChessPieceType promotionPieceType, bool validateOnly)
        {
            if (fromX < 0 || fromX > 7 || fromY < 0 || fromY > 7 ||
                toX < 0 || toX > 7 || toY < 0 || toY > 7)
            {
                throw new Exception("Invalid index");
            }

            ChessPositionIndex fromIndex = (ChessPositionIndex)fromX + 8 * fromY;
            ChessPositionIndex toIndex   = (ChessPositionIndex)toX + 8 * toY;

            ChessPiece piece = new ChessPiece();

            if (!Board.GetPiece(fromIndex, ref piece))
            {
                throw new Exception("Piece not found");
            }

            ChessMove move = new ChessMove(fromIndex, toIndex, piece.PieceType);

            move.PromotionPieceType = promotionPieceType;

            return(MakeMove(move, validateOnly));
        }
Esempio n. 14
0
        public bool IsEmpty(ChessPositionIndex index)
        {
            UInt64 pos = (UInt64)1 << (int)index;

            return((whiteBoardState.Pieces & pos) == 0 && (blackBoardState.Pieces & pos) == 0);
        }
Esempio n. 15
0
 public static int GetColumnFromIndex(ChessPositionIndex index)
 {
     return((int)index % 8);
 }
Esempio n. 16
0
 public static int GetRowFromIndex(ChessPositionIndex index)
 {
     return((int)index / 8);
 }
Esempio n. 17
0
 public int GetRow(ChessPositionIndex index)
 {
     return((int)index / 8);
 }
Esempio n. 18
0
 public bool Equals(ChessPositionIndex fromIndex, ChessPositionIndex toIndex)
 {
     return(this.FromIndex == fromIndex && this.ToIndex == toIndex);
 }
Esempio n. 19
0
 public ChessMove(ChessPositionIndex fromIndex, ChessPositionIndex toIndex, ChessPieceType pieceType)
 {
     this.FromIndex = fromIndex;
     this.ToIndex   = toIndex;
     this.PieceType = pieceType;
 }
Esempio n. 20
0
 public int GetColumn(ChessPositionIndex index)
 {
     return((int)index % 8);
 }
Esempio n. 21
0
        public bool IsColor(ChessPositionIndex index, ChessPieceColor color)
        {
            UInt64 pos = (UInt64)1 << (int)index;

            return((GetBoardState(color).Pieces & pos) != 0);
        }