Jump fully functionnal

This commit is contained in:
Arthur 2021-02-23 14:57:31 +01:00
parent 92a20850fe
commit 095c3eaab2
2 changed files with 12 additions and 13 deletions

View File

@ -1,14 +1,14 @@
package othello; package othello;
import othello.players.NegamaxPlayer;
import othello.players.Player; import othello.players.Player;
import othello.players.RandomPlayer;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Player p1 = new NegamaxPlayer(100); Player p1 = new RandomPlayer(10);
Player p2 = new NegamaxPlayer(100); Player p2 = new RandomPlayer(10);
Player[][] board = initialize(p1, p2); Player[][] board = initialize(p1, p2);
State game = new State(board, p1, p2); State game = new State(board, p1, p2);
System.out.println("joueur 1: " + p1); System.out.println("joueur 1: " + p1);

View File

@ -29,10 +29,6 @@ public class State {
return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty()); return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty());
} }
public Player[][] getBoard(){
return this.board;
}
public LinkedList<Pair<Point, Point>> getMove(Player player) { public LinkedList<Pair<Point, Point>> getMove(Player player) {
// Pair<Depart, Arrivee> // Pair<Depart, Arrivee>
LinkedList<Pair<Point, Point>> moves = new LinkedList<>(); LinkedList<Pair<Point, Point>> moves = new LinkedList<>();
@ -51,7 +47,7 @@ public class State {
moves.add(new Pair<>(current, new Point(y + deltaY, x + deltaX))); moves.add(new Pair<>(current, new Point(y + deltaY, x + deltaX)));
} }
Point other = new Point(y + 2 * deltaY, x + 2 * deltaX); Point other = new Point(y + 2 * deltaY, x + 2 * deltaX);
if(this.board[other.getY()][other.getX()] == null && current.isJump(other,getBoard())) if(this.board[other.getY()][other.getX()] == null && current.isJump(other,this.board))
moves.add(new Pair<>(current, other)); moves.add(new Pair<>(current, other));
} catch(ArrayIndexOutOfBoundsException ignored) {} } catch(ArrayIndexOutOfBoundsException ignored) {}
} }
@ -78,20 +74,23 @@ public class State {
public State play(Pair<Point,Point> move) { public State play(Pair<Point,Point> move) {
State copy = this.copy(); State copy = this.copy();
copy.board[move.getRight().getY()][move.getRight().getX()] = copy.getCurrentPlayer(); copy.board[move.getRight().getY()][move.getRight().getX()] = copy.getCurrentPlayer();
if(move.getLeft().isJump(move.getRight(),copy.board)){
copy.board[move.getLeft().getY()][move.getLeft().getX()] = null;
}
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 {
copy.board[move.getRight().getY() + i][move.getRight().getX() + z] = copy.getCurrentPlayer(); copy.board[move.getRight().getY() + i][move.getRight().getX() + z] = copy.getCurrentPlayer();
} catch (IndexOutOfBoundsException ignored) {} }catch (IndexOutOfBoundsException ignored) {}
} }
} }
int ni = 0, nj = 0; int ni = 0, nj = 0;
for (Player[] players : board) { for (Player[] players : copy.board) {
for (Player player : players) { for (Player player : players) {
if (player == player1) { if (player == copy.player1) {
ni++; ni++;
} }
else if (player == player2) { else if (player == copy.player2) {
nj++; nj++;
} }
} }