public final ArrayList<Move> pseudoLegalMoves(Position pos){
  ArrayList<Move> moveList=getMoveListObj();
  final boolean wtm=pos.whiteMove;
  for (int x=0; x < 8; x++) {
    for (int y=0; y < 8; y++) {
      int sq=Position.getSquare(x,y);
      int p=pos.getPiece(sq);
      if ((p == Piece.EMPTY) || (Piece.isWhite(p) != wtm)) {
        continue;
      }
      if ((p == Piece.WROOK) || (p == Piece.BROOK) || (p == Piece.WQUEEN)|| (p == Piece.BQUEEN)) {
        if (addDirection(moveList,pos,sq,7 - x,1))         return moveList;
        if (addDirection(moveList,pos,sq,7 - y,8))         return moveList;
        if (addDirection(moveList,pos,sq,x,-1))         return moveList;
        if (addDirection(moveList,pos,sq,y,-8))         return moveList;
      }
      if ((p == Piece.WBISHOP) || (p == Piece.BBISHOP) || (p == Piece.WQUEEN)|| (p == Piece.BQUEEN)) {
        if (addDirection(moveList,pos,sq,Math.min(7 - x,7 - y),9))         return moveList;
        if (addDirection(moveList,pos,sq,Math.min(x,7 - y),7))         return moveList;
        if (addDirection(moveList,pos,sq,Math.min(x,y),-9))         return moveList;
        if (addDirection(moveList,pos,sq,Math.min(7 - x,y),-7))         return moveList;
      }
      if ((p == Piece.WKNIGHT) || (p == Piece.BKNIGHT)) {
        if (x < 6 && y < 7 && addDirection(moveList,pos,sq,1,10))         return moveList;
        if (x < 7 && y < 6 && addDirection(moveList,pos,sq,1,17))         return moveList;
        if (x > 0 && y < 6 && addDirection(moveList,pos,sq,1,15))         return moveList;
        if (x > 1 && y < 7 && addDirection(moveList,pos,sq,1,6))         return moveList;
        if (x > 1 && y > 0 && addDirection(moveList,pos,sq,1,-10))         return moveList;
        if (x > 0 && y > 1 && addDirection(moveList,pos,sq,1,-17))         return moveList;
        if (x < 7 && y > 1 && addDirection(moveList,pos,sq,1,-15))         return moveList;
        if (x < 6 && y > 0 && addDirection(moveList,pos,sq,1,-6))         return moveList;
      }
      if ((p == Piece.WKING) || (p == Piece.BKING)) {
        if (x < 7 && addDirection(moveList,pos,sq,1,1))         return moveList;
        if (x < 7 && y < 7 && addDirection(moveList,pos,sq,1,9))         return moveList;
        if (y < 7 && addDirection(moveList,pos,sq,1,8))         return moveList;
        if (x > 0 && y < 7 && addDirection(moveList,pos,sq,1,7))         return moveList;
        if (x > 0 && addDirection(moveList,pos,sq,1,-1))         return moveList;
        if (x > 0 && y > 0 && addDirection(moveList,pos,sq,1,-9))         return moveList;
        if (y > 0 && addDirection(moveList,pos,sq,1,-8))         return moveList;
        if (x < 7 && y > 0 && addDirection(moveList,pos,sq,1,-7))         return moveList;
        int k0=wtm ? Position.getSquare(4,0) : Position.getSquare(4,7);
        if (Position.getSquare(x,y) == k0) {
          int aCastle=wtm ? Position.A1_CASTLE : Position.A8_CASTLE;
          int hCastle=wtm ? Position.H1_CASTLE : Position.H8_CASTLE;
          int rook=wtm ? Piece.WROOK : Piece.BROOK;
          if (((pos.getCastleMask() & (1 << hCastle)) != 0) && (pos.getPiece(k0 + 1) == Piece.EMPTY) && (pos.getPiece(k0 + 2) == Piece.EMPTY)&& (pos.getPiece(k0 + 3) == rook)&& !sqAttacked(pos,k0)&& !sqAttacked(pos,k0 + 1)) {
            moveList.add(getMoveObj(k0,k0 + 2,Piece.EMPTY));
          }
          if (((pos.getCastleMask() & (1 << aCastle)) != 0) && (pos.getPiece(k0 - 1) == Piece.EMPTY) && (pos.getPiece(k0 - 2) == Piece.EMPTY)&& (pos.getPiece(k0 - 3) == Piece.EMPTY)&& (pos.getPiece(k0 - 4) == rook)&& !sqAttacked(pos,k0)&& !sqAttacked(pos,k0 - 1)) {
            moveList.add(getMoveObj(k0,k0 - 2,Piece.EMPTY));
          }
        }
      }
      if ((p == Piece.WPAWN) || (p == Piece.BPAWN)) {
        int yDir=wtm ? 8 : -8;
        if (pos.getPiece(sq + yDir) == Piece.EMPTY) {
          addPawnMoves(moveList,sq,sq + yDir);
          if ((y == (wtm ? 1 : 6)) && (pos.getPiece(sq + 2 * yDir) == Piece.EMPTY)) {
            addPawnMoves(moveList,sq,sq + yDir * 2);
          }
        }
        if (x > 0) {
          int toSq=sq + yDir - 1;
          int cap=pos.getPiece(toSq);
          if (cap != Piece.EMPTY) {
            if (Piece.isWhite(cap) != wtm) {
              if (cap == (wtm ? Piece.BKING : Piece.WKING)) {
                returnMoveList(moveList);
                moveList=getMoveListObj();
                moveList.add(getMoveObj(sq,toSq,Piece.EMPTY));
                return moveList;
              }
 else {
                addPawnMoves(moveList,sq,toSq);
              }
            }
          }
 else           if (toSq == pos.getEpSquare()) {
            addPawnMoves(moveList,sq,toSq);
          }
        }
        if (x < 7) {
          int toSq=sq + yDir + 1;
          int cap=pos.getPiece(toSq);
          if (cap != Piece.EMPTY) {
            if (Piece.isWhite(cap) != wtm) {
              if (cap == (wtm ? Piece.BKING : Piece.WKING)) {
                returnMoveList(moveList);
                moveList=getMoveListObj();
                moveList.add(getMoveObj(sq,toSq,Piece.EMPTY));
                return moveList;
              }
 else {
                addPawnMoves(moveList,sq,toSq);
              }
            }
          }
 else           if (toSq == pos.getEpSquare()) {
            addPawnMoves(moveList,sq,toSq);
          }
        }
      }
    }
  }
  return moveList;
}

public final ArrayList < Move > pseudoLegalMoves ( Position pos ) { ArrayList < Move > moveList = getMoveListObj ( ) ; final boolean wtm = pos . whiteMove ; for ( int x = 0 ; x < 8 ; x ++ ) { for ( int y = 0 ; y < 8 ; y ++ ) { int sq = Position . getSquare ( x , y ) ; int p = pos . getPiece ( sq ) ; if ( ( p == Piece . EMPTY ) || ( Piece . isWhite ( p ) != wtm ) ) { continue ; } if ( ( p == Piece . WROOK ) || ( p == Piece . BROOK ) || ( p == Piece . WQUEEN ) || ( p == Piece . BQUEEN ) ) { if ( addDirection ( moveList , pos , sq , 7 - x , 1 ) ) return moveList ; if ( addDirection ( moveList , pos , sq , 7 - y , 8 ) ) return moveList ; if ( addDirection ( moveList , pos , sq , x , - 1 ) ) return moveList ; if ( addDirection ( moveList , pos , sq , y , - 8 ) ) return moveList ; } if ( ( p == Piece . WBISHOP ) || ( p == Piece . BBISHOP ) || ( p == Piece . WQUEEN ) || ( p == Piece . BQUEEN ) ) { if ( addDirection ( moveList , pos , sq , Math . min ( 7 - x , 7 - y ) , 9 ) ) return moveList ; if ( addDirection ( moveList , pos , sq , Math . min ( x , 7 - y ) , 7 ) ) return moveList ; if ( addDirection ( moveList , pos , sq , Math . min ( x , y ) , - 9 ) ) return moveList ; if ( addDirection ( moveList , pos , sq , Math . min ( 7 - x , y ) , - 7 ) ) return moveList ; } if ( ( p == Piece . WKNIGHT ) || ( p == Piece . BKNIGHT ) ) { if ( x < 6 && y < 7 && addDirection ( moveList , pos , sq , 1 , 10 ) ) return moveList ; if ( x < 7 && y < 6 && addDirection ( moveList , pos , sq , 1 , 17 ) ) return moveList ; if ( x > 0 && y < 6 && addDirection ( moveList , pos , sq , 1 , 15 ) ) return moveList ; if ( x > 1 && y < 7 && addDirection ( moveList , pos , sq , 1 , 6 ) ) return moveList ; if ( x > 1 && y > 0 && addDirection ( moveList , pos , sq , 1 , - 10 ) ) return moveList ; if ( x > 0 && y > 1 && addDirection ( moveList , pos , sq , 1 , - 17 ) ) return moveList ; if ( x < 7 && y > 1 && addDirection ( moveList , pos , sq , 1 , - 15 ) ) return moveList ; if ( x < 6 && y > 0 && addDirection ( moveList , pos , sq , 1 , - 6 ) ) return moveList ; } if ( ( p == Piece . WKING ) || ( p == Piece . BKING ) ) { if ( x < 7 && addDirection ( moveList , pos , sq , 1 , 1 ) ) return moveList ; if ( x < 7 && y < 7 && addDirection ( moveList , pos , sq , 1 , 9 ) ) return moveList ; if ( y < 7 && addDirection ( moveList , pos , sq , 1 , 8 ) ) return moveList ; if ( x > 0 && y < 7 && addDirection ( moveList , pos , sq , 1 , 7 ) ) return moveList ; if ( x > 0 && addDirection ( moveList , pos , sq , 1 , - 1 ) ) return moveList ; if ( x > 0 && y > 0 && addDirection ( moveList , pos , sq , 1 , - 9 ) ) return moveList ; if ( y > 0 && addDirection ( moveList , pos , sq , 1 , - 8 ) ) return moveList ; if ( x < 7 && y > 0 && addDirection ( moveList , pos , sq , 1 , - 7 ) ) return moveList ; int k0 = wtm ? Position . getSquare ( 4 , 0 ) : Position . getSquare ( 4 , 7 ) ; if ( Position . getSquare ( x , y ) == k0 ) { int aCastle = wtm ? Position . A1_CASTLE : Position . A8_CASTLE ; int hCastle = wtm ? Position . H1_CASTLE : Position . H8_CASTLE ; int rook = wtm ? Piece . WROOK : Piece . BROOK ; if ( ( ( pos . getCastleMask ( ) & ( 1 << hCastle ) ) != 0 ) && ( pos . getPiece ( k0 + 1 ) == Piece . EMPTY ) && ( pos . getPiece ( k0 + 2 ) == Piece . EMPTY ) && ( pos . getPiece ( k0 + 3 ) == rook ) && ! sqAttacked ( pos , k0 ) && ! sqAttacked ( pos , k0 + 1 ) ) { moveList . add ( getMoveObj ( k0 , k0 + 2 , Piece . EMPTY ) ) ; } if ( ( ( pos . getCastleMask ( ) & ( 1 << aCastle ) ) != 0 ) && ( pos . getPiece ( k0 - 1 ) == Piece . EMPTY ) && ( pos . getPiece ( k0 - 2 ) == Piece . EMPTY ) && ( pos . getPiece ( k0 - 3 ) == Piece . EMPTY ) && ( pos . getPiece ( k0 - 4 ) == rook ) && ! sqAttacked ( pos , k0 ) && ! sqAttacked ( pos , k0 - 1 ) ) { moveList . add ( getMoveObj ( k0 , k0 - 2 , Piece . EMPTY ) ) ; } } } if ( ( p == Piece . WPAWN ) || ( p == Piece . BPAWN ) ) { int yDir = wtm ? 8 : - 8 ; if ( pos . getPiece ( sq + yDir ) == Piece . EMPTY ) { addPawnMoves ( moveList , sq , sq + yDir ) ; if ( ( y == ( wtm ? 1 : 6 ) ) && ( pos . getPiece ( sq + 2 * yDir ) == Piece . EMPTY ) ) { addPawnMoves ( moveList , sq , sq + yDir * 2 ) ; } } if ( x > 0 ) { int toSq = sq + yDir - 1 ; int cap = pos . getPiece ( toSq ) ; if ( cap != Piece . EMPTY ) { if ( Piece . isWhite ( cap ) != wtm ) { if ( cap == ( wtm ? Piece . BKING : Piece . WKING ) ) { returnMoveList ( moveList ) ; moveList = getMoveListObj ( ) ; moveList . add ( getMoveObj ( sq , toSq , Piece . EMPTY ) ) ; return moveList ; } else { addPawnMoves ( moveList , sq , toSq ) ; } } } else if ( toSq == pos . getEpSquare ( ) ) { addPawnMoves ( moveList , sq , toSq ) ; } } if ( x < 7 ) { int toSq = sq + yDir + 1 ; int cap = pos . getPiece ( toSq ) ; if ( cap != Piece . EMPTY ) { if ( Piece . isWhite ( cap ) != wtm ) { if ( cap == ( wtm ? Piece . BKING : Piece . WKING ) ) { returnMoveList ( moveList ) ; moveList = getMoveListObj ( ) ; moveList . add ( getMoveObj ( sq , toSq , Piece . EMPTY ) ) ; return moveList ; } else { addPawnMoves ( moveList , sq , toSq ) ; } } } else if ( toSq == pos . getEpSquare ( ) ) { addPawnMoves ( moveList , sq , toSq ) ; } } } } } return moveList ; }
generate and return a list of pseudo - legal moves . pseudo - legal means that the moves don ' t necessarily defend from check threats .
