Skip to content

Commit

Permalink
Fix(#28): Remove bug that didn't sort the priorityActions before eval…
Browse files Browse the repository at this point in the history
…uating and deepcopied depth
  • Loading branch information
JonBergland committed Feb 6, 2024
1 parent d9b9c66 commit e6d27b3
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public Action getAction(StateHandler state) {

int depth = 1;

List<Action> priorityActions = state.getActions();
priorityActions.stream()
List<Action> priorityActions = state.getActions().stream()
.sorted(Comparator.comparingInt(action -> state.result(action).utility(agentPlayerId)))
.collect(Collectors.toList());

Collections.reverse(priorityActions);

while (!isTimeUp()) {
//Find the best move ordering
Expand All @@ -72,16 +73,18 @@ private float evaluateState(StateHandler state, float alpha, float beta, int dep
return state.utility(agentPlayerId);
}

List<Action> priorityActions = state.getActions();
priorityActions.stream()
List<Action> priorityActions = state.getActions().stream()
.sorted(Comparator.comparingInt(action -> state.result(action).utility(agentPlayerId)))
.collect(Collectors.toList());

float value;
int minusDepth = depth - 1;

if (isMaximizingPlayer) {
Collections.reverse(priorityActions);
value = alpha;
for (Action action : priorityActions) {
value = Math.max(value, evaluateState(state.result(action), value, beta, depth--, !isMaximizingPlayer));
value = Math.max(value, evaluateState(state.result(action), value, beta, minusDepth, !isMaximizingPlayer));
if (value >= beta) {
break;
}
Expand All @@ -90,10 +93,9 @@ private float evaluateState(StateHandler state, float alpha, float beta, int dep
return value;
}
else {
Collections.reverse(priorityActions);
value = beta;
for (Action action : priorityActions) {
value = Math.min(value, evaluateState(state.result(action), alpha, value, depth--, !isMaximizingPlayer));
value = Math.min(value, evaluateState(state.result(action), alpha, value, minusDepth, !isMaximizingPlayer));
if (alpha >= value) {
break;
}
Expand Down

0 comments on commit e6d27b3

Please sign in to comment.