Adding equals

This commit is contained in:
Arthur 2021-02-23 17:49:22 +01:00
parent 3f58fd52ed
commit 4c52df803f
2 changed files with 16 additions and 7 deletions

View File

@ -146,5 +146,17 @@ public class State {
} }
return str.toString(); return str.toString();
} }
@Override
public boolean equals(Object state) {
boolean bool;
bool = ( state instanceof State);
bool = bool && (this.getCurrentPlayer() == ((State) state).getCurrentPlayer()) && (this.player1 == ((State) state).player1) && (this.player2 == ((State) state).player2);
bool = bool && (this.n1 == ((State)state).n1)&& (this.n2 == ((State)state).n2);
for (int i = 0; i < this.board.length; i++) {
for (int y = 0; y < this.board.length; y++){
bool = bool && (this.board[i][y] == ((State)state).board[i][y]);
}
}
return bool;
}
} }

View File

@ -16,7 +16,7 @@ public class NegamaxPlayer extends Player {
Pair<Point, Point> bestMove = null; Pair<Point, Point> bestMove = null;
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) { for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
State nextState = game.play(move); State nextState = game.play(move);
int value = -negamax(nextState, this.depth,Integer.MIN_VALUE,Integer.MAX_VALUE); int value = -negamax(nextState, this.depth);
if (value > bestValue) { if (value > bestValue) {
bestValue = value; bestValue = value;
bestMove = move; bestMove = move;
@ -25,7 +25,7 @@ public class NegamaxPlayer extends Player {
return bestMove; return bestMove;
} }
private int negamax(State state, int depth,int alpha,int beta) { private int negamax(State state, int depth) {
if(depth == 0 || state.isOver()) { if(depth == 0 || state.isOver()) {
return evaluate(state); return evaluate(state);
} }
@ -33,10 +33,7 @@ public class NegamaxPlayer extends Player {
int m = Integer.MIN_VALUE; int m = Integer.MIN_VALUE;
for (Pair<Point, Point> move : state.getMove(state.getCurrentPlayer())) { for (Pair<Point, Point> move : state.getMove(state.getCurrentPlayer())) {
State nextState = state.play(move); State nextState = state.play(move);
m= Math.max(m,-negamax(nextState,depth-1,alpha,beta)); m= Math.max(m,-negamax(nextState,depth-1));
alpha = Math.max(alpha, m);
if(alpha >= beta)
break;
} }
return m; return m;
} }