This is my basic move ordering function:
private List<Move> sortMoves(List<Move> moves, int depth)
{
//Create new sorted list
List<Move> sorted = new ArrayList<Move>();
//Exit if original list is empty
if (moves.size() == 0)
return sorted;
//Temporary lists to hold killer moves and remaining moves
List<Move> primary = new ArrayList<Move>();
List<Move> secondary = new ArrayList<Move>();
List<Move> rest = new ArrayList<Move>();
//Loop through the moves and add killer moves first
for(int i = 0, n = moves.size(); i < n; i++)
{
if (killers.primary[depth] != null && moves.get(i).equals(killers.primary[depth]))
primary.add(moves.get(i));
else if (killers.secondary[depth] != null && moves.get(i).equals(killers.secondary[depth]))
secondary.add(moves.get(i));
else
rest.add(moves.get(i));
}
//Add the moves in descending order of priority
sorted.addAll(primary);
sorted.addAll(secondary);
sorted.addAll(rest);
//Return the sorted moves
return sorted;
}
The moves parameter contains the list of generated moves (which I already sort upon generation by adding a weight to them). The killers include primary and secondary killers for a particular depth.
The code works and I didn't profile it, but I believe it can really be improved upon. Any suggestions?
Thanks,
C