4
4
import com .shynee .main .chess .Move ;
5
5
import com .shynee .main .chess .Piece ;
6
6
7
+ import java .util .Comparator ;
7
8
import java .util .List ;
8
9
9
10
/**
10
11
* MoveOrdering -- Static class used to order moves by how likely they are to be a good move
11
12
* This should theoretically help the speed of the search and prioritize certain moves in a "quiet" position
12
13
*/
13
- public class MoveOrdering {
14
+ public class MoveOrdering implements Comparator < Move > {
14
15
15
16
// Increase capture score so that a capture of any kind is always the best move
16
17
private final static int pieceCaptureMultiplier = 10 ;
17
18
private final static int maxMoveCount = 218 ;
18
19
20
+ private int [] moveScores ;
21
+
19
22
/**
20
23
* Orders moves based on how likely they are to be a good move.
21
24
* Should increase search efficiency and prioritize certain moves in a "quiet" position.
@@ -24,12 +27,14 @@ public class MoveOrdering {
24
27
* @param moves List of moves to be ordered
25
28
* @return New list of moves sorted by how likely each move is to be a good move.
26
29
*/
27
- public static List <Move > orderMoves (ChessBoard board , List <Move > moves ){
28
- int [] moveScores = new int [maxMoveCount ];
30
+ public List <Move > orderMoves (ChessBoard board , List <Move > moves ){
31
+ moveScores = new int [maxMoveCount ];
29
32
30
33
for (int i = 0 ; i < moves .size (); i ++){
31
34
int score = 0 ;
35
+
32
36
Move move = moves .get (i );
37
+ move .setSortIndex (i );
33
38
34
39
Piece movePiece = board .getSquare (move .piecePos ).getPiece ();
35
40
Piece targetPiece = board .getSquare (move .squarePos ).getPiece ();
@@ -47,40 +52,13 @@ public static List<Move> orderMoves(ChessBoard board, List<Move> moves){
47
52
moveScores [i ] = score ;
48
53
}
49
54
50
- return sort (moves , moveScores );
51
- }
52
-
53
- /**
54
- * Sorts a list of moves by performing a selection sort on an int[].
55
- * Since the values of the List<Move> correspond to the values of the int[],
56
- * we can swap them when we perform the swap for the selection sort.
57
- *
58
- * @param moves List of moves we want to be sorted.
59
- * @param moveScores Int[] whose values correspond to the values of the List<Move>
60
- * @return The list of moves sorted in descending order
61
- */
62
- private static List <Move > sort (List <Move > moves , int [] moveScores ){
63
- for (int i = 0 ; i < moves .size () - 1 ; i ++) {
64
- int maxIndex = i ;
65
- for (int j = i +1 ; j < moves .size (); j ++){
66
- if (moveScores [j ] > moveScores [maxIndex ]){
67
- maxIndex = j ;
68
- }
69
- }
70
-
71
- swap (moves , moveScores , i , maxIndex );
72
- }
55
+ moves .sort (this );
73
56
74
57
return moves ;
75
58
}
76
59
77
- private static void swap (List <Move > moves , int [] moveScores , int posOne , int posTwo ){
78
- Move tempM = moves .get (posOne );
79
- moves .set (posOne , moves .get (posTwo ));
80
- moves .set (posTwo , tempM );
81
-
82
- int tempI = moveScores [posOne ];
83
- moveScores [posOne ] = moveScores [posTwo ];
84
- moveScores [posTwo ] = tempI ;
60
+ @ Override
61
+ public int compare (Move o1 , Move o2 ) {
62
+ return Integer .compare (moveScores [o2 .getSortIndex ()], moveScores [o1 .getSortIndex ()]);
85
63
}
86
64
}
0 commit comments