Skip to content

Commit d6acd55

Browse files
committed
Optimized MoveOrdering with Comparator
1 parent 60f669c commit d6acd55

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

core/src/com/shynee/main/chess/AI/MoveOrdering.java

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
import com.shynee.main.chess.Move;
55
import com.shynee.main.chess.Piece;
66

7+
import java.util.Comparator;
78
import java.util.List;
89

910
/**
1011
* MoveOrdering -- Static class used to order moves by how likely they are to be a good move
1112
* This should theoretically help the speed of the search and prioritize certain moves in a "quiet" position
1213
*/
13-
public class MoveOrdering {
14+
public class MoveOrdering implements Comparator<Move>{
1415

1516
// Increase capture score so that a capture of any kind is always the best move
1617
private final static int pieceCaptureMultiplier = 10;
1718
private final static int maxMoveCount = 218;
1819

20+
private int[] moveScores;
21+
1922
/**
2023
* Orders moves based on how likely they are to be a good move.
2124
* Should increase search efficiency and prioritize certain moves in a "quiet" position.
@@ -24,12 +27,14 @@ public class MoveOrdering {
2427
* @param moves List of moves to be ordered
2528
* @return New list of moves sorted by how likely each move is to be a good move.
2629
*/
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];
2932

3033
for (int i = 0; i < moves.size(); i++){
3134
int score = 0;
35+
3236
Move move = moves.get(i);
37+
move.setSortIndex(i);
3338

3439
Piece movePiece = board.getSquare(move.piecePos).getPiece();
3540
Piece targetPiece = board.getSquare(move.squarePos).getPiece();
@@ -47,40 +52,13 @@ public static List<Move> orderMoves(ChessBoard board, List<Move> moves){
4752
moveScores[i] = score;
4853
}
4954

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

7457
return moves;
7558
}
7659

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()]);
8563
}
8664
}

core/src/com/shynee/main/chess/AI/Search.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
public class Search {
1212

13-
private ChessBoard board;
13+
private final ChessBoard board;
14+
private final MoveOrdering moveOrdering;
15+
private final TranspositionTable tt;
16+
1417
private Move bestMoveInIteration;
1518
private Move bestMove = null;
1619

@@ -26,11 +29,10 @@ public class Search {
2629
// Uniquely identifiable number that will not conflict with search
2730
private final int mateScore = 100000;
2831

29-
private final TranspositionTable tt;
30-
3132
public Search(ChessBoard board){
3233
this.board = board;
3334
this.tt = new TranspositionTable(board, 64000);
35+
this.moveOrdering = new MoveOrdering();
3436
}
3537

3638
/**
@@ -127,7 +129,7 @@ private int search(int depth, int alpha, int beta, int plyFromRoot){
127129
return 0; //Stalemate
128130
}
129131

130-
legalMoves = MoveOrdering.orderMoves(board, legalMoves);
132+
legalMoves = moveOrdering.orderMoves(board, legalMoves);
131133
for (Move legalMove : legalMoves) {
132134
board.makeMove(legalMove, true);
133135
int eval = -search(depth-1, -beta, -alpha, plyFromRoot+1);
@@ -172,7 +174,7 @@ private int quiescenceSearch(int alpha, int beta){
172174

173175
boolean color = board.colorToMove();
174176
List<Move> legalCaptures = board.getMoveCalculator().getLegalCaptures(board, color);
175-
MoveOrdering.orderMoves(board, legalCaptures);
177+
moveOrdering.orderMoves(board, legalCaptures);
176178

177179
for (Move move : legalCaptures) {
178180

core/src/com/shynee/main/chess/Move.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.shynee.main.chess;
22

3-
public class Move {
3+
public class Move{
44

55
public int piecePos;
66
public int squarePos;
77
public int directionOffset;
88
public boolean isCastle;
99
public boolean isPromotion;
1010

11+
private int sortIndex;
12+
1113
public Move(int oldSquarePos, int newSquarePos, int directionOffset){
1214
this.piecePos = oldSquarePos;
1315
this.squarePos = newSquarePos;
@@ -19,8 +21,17 @@ public Move setCastle(){
1921
return this;
2022
}
2123

22-
public Move setPromotion(){
24+
public Move setPromotion() {
2325
this.isPromotion = true;
2426
return this;
2527
}
28+
29+
public void setSortIndex(int index){
30+
this.sortIndex = index;
31+
}
32+
33+
public int getSortIndex(){
34+
return sortIndex;
35+
}
36+
2637
}

core/src/com/shynee/main/scenes/ChessScene.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void start(){
4848

4949
GameObject aiPlayer = new GameObject("AiPLayer");
5050
aiPlayer.addComponent(new AIController(!playerColor, board, book));
51-
addGameObject(aiPlayer);
51+
//addGameObject(aiPlayer);
5252
}
5353

5454
@Override

0 commit comments

Comments
 (0)