fixed bugs and crash

This commit is contained in:
Quentin Legot 2021-02-12 09:57:05 +01:00
parent 10fae688ad
commit a62e3f77ba
3 changed files with 18 additions and 13 deletions

View File

@ -19,7 +19,7 @@ public class Main {
AbstractPlayer player = game.getCurrentPlayer(); AbstractPlayer player = game.getCurrentPlayer();
ArrayList<Pair<Point, Point>> moves = game.getMove(player); ArrayList<Pair<Point, Point>> moves = game.getMove(player);
System.out.println(game.toString()); System.out.println(game.toString());
game.play(player.play(moves)); game = game.play(player.play(moves));
} }
System.out.println("C'est " + game.getWinner() + " qui a gagné"); System.out.println("C'est " + game.getWinner() + " qui a gagné");
} }

View File

@ -13,16 +13,18 @@ public class Point {
public boolean isJump(Point other) { public boolean isJump(Point other) {
return Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2) == 4; return Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2) == 4;
} }
public int getX(){ public int getX(){
return x; return x;
} }
public int getY(){ public int getY(){
return y; return y;
} }
@Override @Override
public String toString () { public String toString () {
return "("+x+","+y+")"; return "(" + x + ", " + y + ")";
} }
} }

View File

@ -42,15 +42,17 @@ public class State {
// Recherche autour du pion du joueur courant // Recherche autour du pion du joueur courant
for (int deltaY = -1; deltaY < 2; deltaY++) { for (int deltaY = -1; deltaY < 2; deltaY++) {
for (int deltaX = -1; deltaX < 2; deltaX++) { for (int deltaX = -1; deltaX < 2; deltaX++) {
// La position du pion trouv<EFBFBD> est exclue // La position du pion trouvée est exclue
if (deltaY != 0 && deltaX != 0) { if (deltaY != 0 && deltaX != 0) {
// Si une place libre est trouv<EFBFBD>e elle est ajout<EFBFBD>e à la liste des coups // Si une place libre est trouvée elle est ajoutée à la liste des coups
try { try {
if (this.board[y+deltaY][x+deltaX] == null) { if (this.board[y+deltaY][x+deltaX] == null) {
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+deltaY, x+deltaX))); moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+deltaY, x+deltaX)));
} else { } else {
if(this.board[y+2*deltaY][x+2*deltaX] == null) Point current = new Point(y, x);
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+2*deltaY, x+2*deltaX))); Point other = new Point(y + 2 * deltaY, x + 2 * deltaX);
if(this.board[y+2*deltaY][x+2*deltaX] == null && current.isJump(other))
moves.add(new Pair<Point, Point>(current, other));
} }
} catch(ArrayIndexOutOfBoundsException ignored) {} } catch(ArrayIndexOutOfBoundsException ignored) {}
} }
@ -79,8 +81,8 @@ public class State {
State copy = this.copy(); State copy = this.copy();
copy.board[pair.getLeft().getX()][pair.getLeft().getY()] = copy.getCurrentPlayer(); copy.board[pair.getLeft().getX()][pair.getLeft().getY()] = copy.getCurrentPlayer();
int increment = 0; int increment = 0;
for(int i = -1; i<2;i++){ for(int i = -1; i < 2; i++){
for(int z = -1;z<2;z++){ for(int z = -1; z < 2; z++){
try { try {
if(copy.board[pair.getLeft().getX() + i][pair.getLeft().getY() + z] != copy.getCurrentPlayer()){ if(copy.board[pair.getLeft().getX() + i][pair.getLeft().getY() + z] != copy.getCurrentPlayer()){
increment++; increment++;
@ -89,7 +91,7 @@ public class State {
} catch (IndexOutOfBoundsException ignored) {} } catch (IndexOutOfBoundsException ignored) {}
} }
} }
if (currentPlayer == player1) if (copy.currentPlayer == player1)
copy.n1 += increment; copy.n1 += increment;
else else
copy.n2 += increment; copy.n2 += increment;
@ -106,12 +108,13 @@ public class State {
} }
public State copy () { public State copy () {
State copy = new State(this.board, this.player1, this.player2,this.n1,this.n2); State copy = new State(this.board, this.player1, this.player2, this.n1, this.n2);
for (int i=0; i<this.board.length;i++) { for (int i = 0; i < this.board.length; i++) {
for (int j=0; j<this.board.length; j++) { for (int j = 0; j < this.board.length; j++) {
copy.board[i][j] = this.board[i][j]; copy.board[i][j] = this.board[i][j];
} }
} }
copy.setCurrentPlayer(this.currentPlayer);
return copy; return copy;
} }