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 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 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 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);
         }
     }
 }