public final int gen_allLegalMoves(Move[] moves, int startIndex) {
		int nMoves = 0;

		nMoves = gen_noncaps(moves, startIndex);

		nMoves += gen_caps(moves, nMoves+startIndex);


		/* Filter out the moves that leaves the king in check */
		int totalLegalMoves = 0;
		for(int i = startIndex; i < nMoves+startIndex; i++) {
			int thisMove = moves[i].move;
			makeMove(thisMove);

			if(toMove == WHITE_TO_MOVE) {
				if(isAttacked(b_king.pieces[0], WHITE)) {
					moves[i].move = 0;
				} else {
					moves[i].move = 0;
					moves[startIndex+totalLegalMoves].move = thisMove;
					totalLegalMoves++;
				}
			} else {
				if(isAttacked(w_king.pieces[0], BLACK)) {
					moves[i].move = 0;
				} else {
					moves[i].move = 0;
					moves[startIndex+totalLegalMoves].move = thisMove;
					totalLegalMoves++;
				}
			}
			unmakeMove(thisMove);
			
		}
	
		return totalLegalMoves;
	} // END gen_allLegalMoves()