Skip to content

Commit 5723fbc

Browse files
committed
Transposition tables with zobrist hashing added
1 parent 0a80e06 commit 5723fbc

File tree

6 files changed

+181
-106
lines changed

6 files changed

+181
-106
lines changed

src/main/java/Engine.java

+43-34
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import game.Move;
44

55
import java.util.Comparator;
6+
import java.util.HashMap;
67
import java.util.List;
8+
import java.util.Map;
79
import java.util.concurrent.CompletableFuture;
810
import java.util.concurrent.ExecutionException;
911
import java.util.concurrent.TimeUnit;
@@ -12,6 +14,7 @@
1214

1315
public class Engine {
1416
public static int nodesEvaluated;
17+
public Map<Long, Result> transpositionTable = new HashMap<>();
1518

1619
public int countAllMoves(final Board board, final int depth) {
1720
return countAllMoves(board, depth, 1000);
@@ -66,43 +69,61 @@ public OutCome alphaBeta(final Board board, final int depth, double alpha, doubl
6669
final List<Move> legalMoves = board.getLegalMoves();
6770
nodesEvaluated++;
6871
if (legalMoves.isEmpty() || depth == 0) {
69-
return new OutCome(board, null, -board.evaluation(legalMoves.size()));
72+
final double score;
73+
if (transpositionTable.containsKey(board.zobristHash)) {
74+
score = transpositionTable.get(board.zobristHash).eval;
75+
} else {
76+
score = board.evaluation(legalMoves.size());
77+
transpositionTable.put(board.zobristHash, new Result(score, 0));
78+
}
79+
return new OutCome(board, null, -score);
7080
}
7181
final List<OutCome> outComes = legalMoves.stream().map(move -> {
7282
final Board changedBoard = board.copy();
7383
changedBoard.makeMove(move);
74-
return new OutCome(changedBoard, move, changedBoard.evaluation());
75-
}).sorted(Comparator.comparingDouble(outCome -> -outCome.getScore())).collect(Collectors.toList());
76-
OutCome bestMove = null;
84+
final double score;
85+
if (transpositionTable.containsKey(changedBoard.zobristHash)) {
86+
score = transpositionTable.get(changedBoard.zobristHash).eval;
87+
} else {
88+
score = changedBoard.evaluation(changedBoard.getLegalMoves().size());
89+
transpositionTable.put(changedBoard.zobristHash, new Result(score, 0));
90+
}
91+
return new OutCome(changedBoard, move, score);
92+
}).sorted(Comparator.comparingDouble(OutCome::getScore)).collect(Collectors.toList());
93+
OutCome bestOutCome = null;
7794
for (final OutCome outCome : outComes) {
7895
final OutCome eval = alphaBeta(outCome.getBoard(), depth - 1, alpha, beta, printAt);
7996
if (depth == printAt) {
8097
System.out.println(getString(outCome.getMove()) + ": " + eval.getScore() + " " + outCome +
8198
" " + outCome.getBoard().fenRepresentation());
8299
}
83-
if (bestMove == null || bestMove.getScore() > -eval.getScore()) {
84-
bestMove = new OutCome(board, outCome.getMove(), -eval.getScore());
85-
if (bestMove.getScore() < 0 && bestMove.getScore() + Integer.MAX_VALUE < 0.0001) {
86-
return bestMove;
100+
if (bestOutCome == null || bestOutCome.getScore() > -eval.getScore()) {
101+
bestOutCome = new OutCome(board, outCome.getMove(), -eval.getScore());
102+
if (bestOutCome.getScore() < 0 && bestOutCome.getScore() + Integer.MAX_VALUE < 0.0001) {
103+
return bestOutCome;
87104
}
88105
}
89106
if (board.playerToMove.equals(Color.WHITE)) {
90-
if (alpha < -bestMove.getScore()) {
91-
alpha = -bestMove.getScore();
107+
if (alpha < -bestOutCome.getScore()) {
108+
alpha = -bestOutCome.getScore();
92109
}
93110
} else {
94-
if (beta > bestMove.getScore()) {
95-
beta = bestMove.getScore();
111+
if (beta > bestOutCome.getScore()) {
112+
beta = bestOutCome.getScore();
96113
}
97114
}
98-
if (alpha > beta) {
115+
if (alpha >= beta) {
99116
break;
100117
}
101118
}
102-
return bestMove;
119+
if (!transpositionTable.containsKey(board.zobristHash) || transpositionTable.get(board.zobristHash).depth < depth) {
120+
transpositionTable.put(board.zobristHash, new Result(-bestOutCome.getScore(), depth));
121+
}
122+
return bestOutCome;
103123
}
104124

105125
public OutCome iterativeDeepening(final Board board, final long time) {
126+
transpositionTable = new HashMap<>();
106127
final long start = System.currentTimeMillis();
107128
int depth = 1;
108129
OutCome evaluation = alphaBeta(board, depth, Integer.MIN_VALUE, Integer.MAX_VALUE, 1000);
@@ -183,26 +204,14 @@ public String toString() {
183204
'}';
184205
}
185206
}
186-
/*
187-
188-
189207

190208

191-
b4g4
192-
b4h4
209+
class Result {
210+
final double eval;
211+
final int depth;
193212

194-
195-
196-
197-
198-
199-
200-
201-
202-
203-
204-
b4g4
205-
b4h4
206-
b4g4
207-
b4h4
208-
*/
213+
public Result(double eval, int depth) {
214+
this.eval = eval;
215+
this.depth = depth;
216+
}
217+
}

src/main/java/commons/Line.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class Line {
1010
public final PieceType minorPieceType;
1111

1212
public Line(final Cell first, final Cell second) {
13-
final int rowDistance = Math.abs(first.row - second.row), colDistance = Math.abs(first.col - second.col);
13+
int rowDistance = Math.abs(first.row - second.row);
14+
int colDistance = Math.abs(first.col - second.col);
1415
isStraight = rowDistance == 0 || colDistance == 0 || rowDistance == colDistance;
1516
rowDiff = Integer.compare(first.row, second.row);
1617
colDiff = Integer.compare(first.col, second.col);

0 commit comments

Comments
 (0)