-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotMinMax.java
More file actions
64 lines (61 loc) · 2.65 KB
/
BotMinMax.java
File metadata and controls
64 lines (61 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.ArrayList;
public class BotMinMax extends Bot {
protected BotMinMax(CheckersGridAccessor accessor, PlayerColor playerColor, Integer maxDepth) {
super(accessor, playerColor, maxDepth);
}
@Override
public ArrayList<MoveWithEstimation> getAllMovesWithEstimations(CheckersGridHandler checkersGridHandler) {
ArrayList<MoveWithEstimation> movesWithEstimation = new ArrayList<>();
var possibleMoves = checkersGridHandler.getAllCurrentPossibleMoves();
for (var move : possibleMoves) {
var copied = checkersGridHandler.getCheckersGrid().copy();
copied.executeMove(move);
var estimation = minOrMax(copied, maxDepth - 1, MinMaxEnum.MIN);
movesWithEstimation.add(new MoveWithEstimation(move, estimation));
// System.out.println("LAST: " + move + ": " + estimation);
}
return movesWithEstimation;
}
public int minOrMax(CheckersGrid checkersGrid, int depth, MinMaxEnum minMaxEnum) {
if (depth == 0) {
return accessor.accessCheckersGrid(checkersGrid, playerColor, null);
}
var allPossibleMoves = checkersGrid.getAllCurrentPossibleMoves();
if (allPossibleMoves.isEmpty()) {
var currentEstimation = accessor.accessCheckersGrid(checkersGrid, playerColor, minMaxEnum);
return switch (minMaxEnum) {
case MIN -> currentEstimation - maxDepth + depth;
case MAX -> currentEstimation + maxDepth - depth;
};
}
int chosenEstimation = 0;
switch (minMaxEnum) {
case MIN -> chosenEstimation = Integer.MAX_VALUE;
case MAX -> chosenEstimation = Integer.MIN_VALUE;
}
switch (minMaxEnum) {
case MIN -> {
for (var move: allPossibleMoves) {
var copied = checkersGrid.copy();
copied.executeMove(move);
var estimation = minOrMax(copied, depth - 1, MinMaxEnum.MAX);
if (chosenEstimation > estimation) {
chosenEstimation = estimation;
}
}
}
case MAX -> {
for (var move: allPossibleMoves) {
var copied = checkersGrid.copy();
copied.executeMove(move);
var estimation = minOrMax(copied, depth - 1, MinMaxEnum.MIN);
if (chosenEstimation < estimation) {
chosenEstimation = estimation;
}
}
}
}
lastMoveCount += 1;
return chosenEstimation;
}
}