Example #1
0
        public static LightList ConvertBuffer(BoardBuffer bf)
        {
            LightList ll = new LightList();

            for (int i = 0; i < bf.capCount; ++i)
            {
                ll.Add(bf.captures[i]);
            }

            for (int i = 0; i < bf.thrCount; ++i)
            {
                ll.Add(bf.threats[i]);
            }

            for (int i = 0; i < bf.forCount; ++i)
            {
                ll.Add(bf.forward[i]);
            }

            for (int i = 0; i < bf.oCount; ++i)
            {
                ll.Add(bf.other[i]);
            }

            for (int i = 0; i < bf.nsCount; ++i)
            {
                ll.Add(bf.notSafe[i]);
            }

            return ll;
        }
        public static void AddKingMoves(this byte[] board, bool white, int i, ref BoardBuffer moves, bool allowCheck)
        {
            byte[] temp;

            if (white)
                IsOpponent = IsLower;
            else
                IsOpponent = IsUpper;

            int idx = i + UPLEFT;
            if (idx < OUTOFBOUNDSHIGH)
            {
                if (idx % COLUMN != COL8 && board.IsValidMove(white, idx))
                {
                    temp = board.Move(i, idx);
                    if (allowCheck || !PieceNotSafe(temp, idx, white))
                    {
                        if (!white)
                        {
                            moves.AddForward(temp);
                        }
                        else
                            moves.Add(temp);
                    }
                }
                if (board.IsValidMove(white, ++idx))
                {
                    temp = board.Move(i, idx);
                    if (allowCheck || !PieceNotSafe(temp, idx, white))
                    {
                        if (!white)
                        {
                            moves.AddForward(temp);
                        }
                        else
                            moves.Add(temp);
                    }
                }
                if (++idx % COLUMN != COL1 && board.IsValidMove(white, idx))
                {
                    temp = board.Move(i, idx);
                    if (allowCheck || !PieceNotSafe(temp, idx, white))
                    {
                        if (!white)
                        {
                            moves.AddForward(temp);
                        }
                        else
                            moves.Add(temp);
                    }
                }
            }
            idx = i + RIGHT;
            if (idx % COLUMN != COL1 && board.IsValidMove(white, idx))
            {
                temp = board.Move(i, idx);
                if (allowCheck || !PieceNotSafe(temp, idx, white))
                    moves.Add(temp);
            }
            idx = i + LEFT;
            if (idx % COLUMN != COL8 && board.IsValidMove(white, idx))
            {
                temp = board.Move(i, idx);
                if (allowCheck || !PieceNotSafe(temp, idx, white))
                    moves.Add(temp);

            }
            idx = i + DOWNRIGHT;
            if (idx > -1)
            {
                if (i % COLUMN != COL8 && board.IsValidMove(white, idx))
                {
                    temp = board.Move(i, idx);
                    if (allowCheck || !PieceNotSafe(temp, idx, white))
                    {
                        if (white)
                        {
                            moves.AddForward(temp);
                        }
                        else
                            moves.Add(temp);
                    }
                }
                if (board.IsValidMove(white, --idx))
                {
                    temp = board.Move(i, idx);
                    if (allowCheck || !PieceNotSafe(temp, idx, white))
                    {
                        if (white)
                        {
                            moves.AddForward(temp);
                        }
                        else
                            moves.Add(temp);
                    }
                }
                if (i % COLUMN != COL1 && board.IsValidMove(white, --idx))
                {
                    temp = board.Move(i, idx);
                    if (allowCheck || !PieceNotSafe(temp, idx, white))
                    {
                        if (white)
                        {
                            moves.AddForward(temp);
                        }
                        else
                            moves.Add(temp);
                    }
                }
            }
        }
 public static void AddKnightMoves(this byte[] board, bool white, int i, ref BoardBuffer moves, bool allowCheck)
 {
     int originRow = i % COLUMN;
     int idx = i + UPUPRIGHT;
     byte[] temp;
     if (idx < OUTOFBOUNDSHIGH && originRow < COL8 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (!white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + UPUPLEFT;
     if (idx < OUTOFBOUNDSHIGH && originRow > COL1 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (!white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + UPRIGHTRIGHT;
     if (idx < OUTOFBOUNDSHIGH && originRow < COL7 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (!white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + UPLEFTLEFT;
     if (idx < OUTOFBOUNDSHIGH && originRow > COL2 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (!white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + DOWNDOWNLEFT;
     if (idx > OUTOFBOUNDSLOW && originRow > COL1 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + DOWNDOWNRIGHT;
     if (idx > OUTOFBOUNDSLOW && originRow < COL8 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + DOWNLEFTLEFT;
     if (idx > OUTOFBOUNDSLOW && originRow > COL2 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
     idx = i + DOWNRIGHTRIGHT;
     if (idx > OUTOFBOUNDSLOW && originRow < COL7 && board.IsValidMove(white, idx))
     {
         temp = board.Move(i, idx);
         if (allowCheck || !InCheck(temp, white))
         {
             if (white ? board[idx].IsLower() : board[idx].IsUpper())
                 moves.AddCapture(temp);
             else if (PieceNotSafe(temp, idx, white))
                 moves.AddUnSafe(temp);
             else if (KnightThreat(temp, idx))
                 moves.AddThreat(temp);
             else if (white)
                 moves.AddForward(temp);
             else
                 moves.Add(temp);
         }
     }
 }
 public static void AddDiagonalMaps(this byte[] board, bool white, int i, ref BoardBuffer moves, bool allowCheck)
 {
     byte[] temp;
     int idx = i + UPLEFT;
     while (idx < OUTOFBOUNDSHIGH && idx % COLUMN != COL8)
     {
         if (IsValidMove(board, white, idx))
         {
             temp = board.Move(i, idx);
             bool cap = board.TakesOpponentPiece(white, idx);
             if (allowCheck || !InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, idx, white))
                     moves.AddUnSafe(temp);
                 else if (DiagThreat(temp, idx))
                     moves.AddThreat(temp);
                 else if (!white)
                     moves.AddForward(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
         idx += UPLEFT;
     }
     idx = i + UPRIGHT;
     while (idx < OUTOFBOUNDSHIGH && idx % COLUMN != COL1)
     {
         if (IsValidMove(board, white, idx))
         {
             temp = board.Move(i, idx);
             bool cap = board.TakesOpponentPiece(white, idx);
             if (allowCheck || !InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, idx, white))
                     moves.AddUnSafe(temp);
                 else if (DiagThreat(temp, idx))
                     moves.AddThreat(temp);
                 else if (!white)
                     moves.AddForward(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
         idx += UPRIGHT;
     }
     idx = i + DOWNLEFT;
     while (idx > OUTOFBOUNDSLOW && idx % COLUMN != COL8)
     {
         if (board.IsValidMove(white, idx))
         {
             temp = board.Move(i, idx);
             bool cap = board.TakesOpponentPiece(white, idx);
             if (allowCheck || !InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, idx, white))
                     moves.AddUnSafe(temp);
                 else if (DiagThreat(temp, idx))
                     moves.AddThreat(temp);
                 else if (white)
                     moves.AddForward(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
         idx += DOWNLEFT;
     }
     idx = i + DOWNRIGHT;
     while (idx > OUTOFBOUNDSLOW && idx % COLUMN != 0)
     {
         if (board.IsValidMove(white, idx))
         {
             temp = board.Move(i, idx);
             bool cap = board.TakesOpponentPiece(white, idx);
             if (allowCheck || !InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, idx, white))
                     moves.AddUnSafe(temp);
                 else if (DiagThreat(temp, idx))
                     moves.AddThreat(temp);
                 else if (white)
                     moves.AddForward(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
         idx += DOWNRIGHT;
     }
 }
 public static void AddAdjacentMaps(this byte[] board, bool white, int i, ref BoardBuffer moves, bool allowCheck)
 {
     int idx = i + UP;
     byte[] temp;
     while (idx < OUTOFBOUNDSHIGH)
     {
         if (board.IsValidMove(white, idx))
         {
             temp = board.Move(i, idx);
             bool cap = board.TakesOpponentPiece(white, idx);
             if (!InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, idx, white))
                     moves.AddUnSafe(temp);
                 else if (AdjacentThreat(temp, idx))
                     moves.AddThreat(temp);
                 else if (!white)
                     moves.AddForward(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
         idx += UP;
     }
     idx = i + DOWN;
     while (idx > OUTOFBOUNDSLOW)
     {
         if (IsValidMove(board, white, idx))
         {
             temp = board.Move(i, idx);
             bool cap = board.TakesOpponentPiece(white, idx);
             if (!InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, idx, white))
                     moves.AddUnSafe(temp);
                 else if (AdjacentThreat(temp, idx))
                     moves.AddThreat(temp);
                 else if (white)
                     moves.AddForward(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
         idx += DOWN;
     }
     idx = i % COLUMN;
     int bse = i - idx;
     while (--idx > -1)
     {
         int ix = bse + idx;
         if (IsValidMove(board, white, ix))
         {
             temp = board.Move(i, ix);
             bool cap = board.TakesOpponentPiece(white, ix);
             if (!InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, ix, white))
                     moves.AddUnSafe(temp);
                 else if (AdjacentThreat(temp, ix))
                     moves.AddThreat(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
     }
     idx = i % COLUMN;
     bse = i - idx;
     while (++idx < 8)
     {
         int ix = bse + idx;
         if (IsValidMove(board, white, ix))
         {
             temp = board.Move(i, ix);
             bool cap = board.TakesOpponentPiece(white, ix);
             if (!InCheck(temp, white))
             {
                 if (cap)
                     moves.AddCapture(temp);
                 else if (PieceNotSafe(temp, ix, white))
                     moves.AddUnSafe(temp);
                 else if (AdjacentThreat(temp, ix))
                     moves.AddThreat(temp);
                 else
                     moves.Add(temp);
             }
             if (cap)
                 break;
         }
         else break;
     }
 }
        public static LightList GetAvailableMoves(byte[] board, ChessColor color, bool allowCheck)
        {
            bool white = color == ChessColor.White;
            LightList moves = new LightList();
            BoardBuffer buff = new BoardBuffer();
            //iterate thru entire board {64} including row delimiters {7}

            for (int i = 0; i < OUTOFBOUNDSHIGH; ++i)
            {
                if (!white)
                {
                    switch (board[i])
                    {
                        case 0x02:
                            board.AddPawnMoves(white, i, ref buff, allowCheck);
                            break;
                        case 0x04:
                            board.AddAdjacentMaps(white, i, ref buff, allowCheck);
                            break;
                        case 0x06:
                            board.AddDiagonalMaps(white, i, ref buff, allowCheck);
                            break;
                        case 0x08:
                            board.AddKnightMoves(white, i, ref buff, allowCheck);
                            break;
                        case 0x0A:
                            board.AddAdjacentMaps(white, i, ref buff, allowCheck);
                            board.AddDiagonalMaps(white, i, ref buff, allowCheck);
                            break;
                        case 0x0C:
                            board.AddKingMoves(white, i, ref buff, allowCheck);
                            break;
                        default: break;
                    }
                }
                else
                {
                    switch (board[i])
                    {
                        case 0x01:
                            board.AddPawnMoves(white, i, ref buff, allowCheck);
                            break;
                        case 0x03:
                            board.AddAdjacentMaps(white, i, ref buff, allowCheck);
                            break;
                        case 0x05:
                            board.AddDiagonalMaps(white, i, ref buff, allowCheck);
                            break;
                        case 0x07:
                            board.AddKnightMoves(white, i, ref buff, allowCheck);
                            break;
                        case 0x09:
                            board.AddAdjacentMaps(white, i, ref buff, allowCheck);
                            board.AddDiagonalMaps(white, i, ref buff, allowCheck);
                            break;
                        case 0x0B:
                            board.AddKingMoves(white, i, ref buff, allowCheck);
                            break;
                        default: break;
                    }
                }
            }

            return LightList.ConvertBuffer(buff);
        }
 public static void AddPawnMoves(this byte[] board, bool white, int i, ref BoardBuffer moves, bool allowCheck)
 {
     byte[] temp;
     if (!white)
     {
         if (i / COLUMN == 1)
         {
             if (board[i + UPUP] == _ && board[i + UP] == _)
             {
                 temp = board.MovePawn(i, i + UPUP, white);
                 if (allowCheck || allowCheck || !InCheck(temp, white))
                 {
                     if (PawnThreat(temp, i + UPUP))
                         moves.AddCapture(temp);
                     else
                         moves.AddForward(temp);
                 }
             }
         }
         if (board[i + UP] == _)
         {
             temp = board.MovePawn(i, i + UP, white);
             if (allowCheck || !InCheck(temp, white))
             {
                 if (PawnThreat(temp, i + UP))
                     moves.AddCapture(temp);
                 else
                     moves.AddForward(temp);
             }
         }
         if (board[i + UPLEFT].IsUpper() && (i+UPLEFT) % COLUMN != COL8)
         {
             temp = board.MovePawn(i, i + UPLEFT, white);
             if (allowCheck || !InCheck(temp, white))
                 moves.AddCapture(temp);
         }
         if (i < 54 && board[i + UPRIGHT].IsUpper() && (i+UPRIGHT) % COLUMN != COL1)
         {
             temp = board.MovePawn(i, i + UPRIGHT, white);
             if (allowCheck || !InCheck(temp, white))
                 moves.AddCapture(temp);
         }
     }
     else
     {
         if (i / COLUMN == COL7)
         {
             if (board[i + DOWNDOWN] == _ && board[i + DOWN] == _)
             {
                 temp = board.MovePawn(i, i + DOWNDOWN, white);
                 if (allowCheck || !InCheck(temp, white))
                 {
                     if (PawnThreat(temp, i + DOWNDOWN))
                         moves.AddCapture(temp);
                     else
                         moves.AddForward(temp);
                 }
             }
         }
         if (board[i + DOWN] == _)
         {
             temp = board.MovePawn(i, i + DOWN, white);
             if (allowCheck || !InCheck(temp, white))
             {
                 if (PawnThreat(temp, i + DOWN))
                     moves.AddCapture(temp);
                 else
                     moves.AddForward(temp);
             }
         }
         if (board[i + DOWNRIGHT].IsLower() && (i+DOWNRIGHT) % COLUMN != COL1)
         {
             temp = board.MovePawn(i, i + DOWNRIGHT, white);
             if (allowCheck || !InCheck(temp, white))
                 moves.AddCapture(temp);
         }
         if (i > 8 && board[i + DOWNLEFT].IsLower() && (i+DOWNLEFT) % COLUMN != COL8)
         {
             temp = board.MovePawn(i, i + DOWNLEFT, white);
             if (!InCheck(temp, white))
                 moves.AddCapture(temp);
         }
     }
 }