2021-01-27 11:49:49 +01:00
|
|
|
|
package othello;
|
|
|
|
|
|
2021-01-27 13:00:52 +01:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
import othello.players.AbstractPlayer;
|
|
|
|
|
|
2021-01-27 11:49:49 +01:00
|
|
|
|
public class State {
|
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
private AbstractPlayer[][] board;
|
|
|
|
|
private AbstractPlayer player1;
|
|
|
|
|
private AbstractPlayer player2;
|
|
|
|
|
private AbstractPlayer currentPlayer;
|
2021-02-02 09:14:10 +01:00
|
|
|
|
private int n1;
|
|
|
|
|
private int n2;
|
2021-01-27 12:24:17 +01:00
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2, int n1, int n2) {
|
2021-01-27 12:24:17 +01:00
|
|
|
|
this.board = board;
|
|
|
|
|
this.player1 = p1;
|
|
|
|
|
this.player2 = p2;
|
|
|
|
|
currentPlayer = p1;
|
2021-02-10 11:21:33 +01:00
|
|
|
|
this.n1 = n1;
|
|
|
|
|
this.n2 = n2;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2) {
|
2021-02-10 11:21:33 +01:00
|
|
|
|
this(board, p1, p2, 2, 2);
|
2021-01-27 12:24:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 11:49:49 +01:00
|
|
|
|
public boolean isOver() {
|
2021-02-10 12:01:51 +01:00
|
|
|
|
if(n1 == 0 || n2 == 0)
|
|
|
|
|
return true;
|
|
|
|
|
return getMove(player1).isEmpty() && getMove(player2).isEmpty();
|
2021-01-27 11:49:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
public ArrayList<Pair<Point, Point>> getMove(AbstractPlayer player) {
|
2021-02-10 12:01:51 +01:00
|
|
|
|
// Pair<Depart, Arrivee>
|
|
|
|
|
ArrayList<Pair<Point, Point>> moves = new ArrayList<>();
|
2021-01-27 16:55:50 +01:00
|
|
|
|
// Parcours du plateau de jeu
|
2021-02-10 12:01:51 +01:00
|
|
|
|
for (int y = 0; y < this.board.length; y++) {
|
|
|
|
|
for (int x = 0; x < this.board[y].length; x++) {
|
|
|
|
|
if (this.board[y][x] == player) {
|
2021-01-27 16:55:50 +01:00
|
|
|
|
// Recherche autour du pion du joueur courant
|
2021-02-10 12:26:47 +01:00
|
|
|
|
for (int deltaY = -1; deltaY < 2; deltaY++) {
|
|
|
|
|
for (int deltaX = -1; deltaX < 2; deltaX++) {
|
2021-02-10 12:01:51 +01:00
|
|
|
|
// La position du pion trouv<75> est exclue
|
|
|
|
|
if (deltaY != 0 && deltaX != 0) {
|
|
|
|
|
// Si une place libre est trouv<75>e elle est ajout<75>e à la liste des coups
|
|
|
|
|
try {
|
2021-02-12 09:18:03 +01:00
|
|
|
|
if (this.board[y+deltaY][x+deltaX] == null) {
|
2021-02-10 12:01:51 +01:00
|
|
|
|
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+deltaY, x+deltaX)));
|
2021-02-12 09:18:03 +01:00
|
|
|
|
} else {
|
|
|
|
|
if(this.board[y+2*deltaY][x+2*deltaX] == null)
|
2021-02-10 12:26:47 +01:00
|
|
|
|
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+2*deltaY, x+2*deltaX)));
|
|
|
|
|
}
|
2021-02-10 12:01:51 +01:00
|
|
|
|
} catch(ArrayIndexOutOfBoundsException ignored) {}
|
2021-01-27 16:55:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return moves;
|
2021-01-27 11:49:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
public int getScore(AbstractPlayer player) {
|
2021-02-10 12:01:51 +01:00
|
|
|
|
return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1);
|
2021-01-27 11:49:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:02:53 +01:00
|
|
|
|
public State play(Pair<Point,Point> pair) {
|
2021-02-10 11:21:33 +01:00
|
|
|
|
State copy = this.copy();
|
2021-02-12 09:02:53 +01:00
|
|
|
|
copy.board[pair.getLeft().getX()][pair.getLeft().getY()] = copy.getCurrentPlayer();
|
2021-02-02 09:14:10 +01:00
|
|
|
|
int increment = 0;
|
|
|
|
|
for(int i = -1; i<2;i++){
|
|
|
|
|
for(int z = -1;z<2;z++){
|
|
|
|
|
try {
|
2021-02-12 09:02:53 +01:00
|
|
|
|
if(copy.board[pair.getLeft().getX() + i][pair.getLeft().getY() + z] != copy.getCurrentPlayer()){
|
2021-02-10 13:01:47 +01:00
|
|
|
|
increment++;
|
2021-02-12 09:02:53 +01:00
|
|
|
|
copy.board[pair.getLeft().getX() + i][pair.getLeft().getY() + z] = copy.getCurrentPlayer();
|
|
|
|
|
}
|
2021-02-10 11:21:33 +01:00
|
|
|
|
} catch (IndexOutOfBoundsException ignored) {}
|
2021-02-02 09:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-10 12:01:51 +01:00
|
|
|
|
if (currentPlayer == player1)
|
2021-02-10 11:21:33 +01:00
|
|
|
|
copy.n1 += increment;
|
2021-02-10 12:01:51 +01:00
|
|
|
|
else
|
2021-02-10 11:21:33 +01:00
|
|
|
|
copy.n2 += increment;
|
2021-02-10 12:01:51 +01:00
|
|
|
|
|
2021-02-10 11:21:33 +01:00
|
|
|
|
copy.switchPlayer();
|
|
|
|
|
return copy;
|
2021-01-27 11:49:49 +01:00
|
|
|
|
}
|
2021-02-12 09:18:03 +01:00
|
|
|
|
public AbstractPlayer getCurrentPlayer() {
|
2021-01-27 12:24:17 +01:00
|
|
|
|
return currentPlayer;
|
|
|
|
|
}
|
2021-01-27 11:49:49 +01:00
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
public void setCurrentPlayer(AbstractPlayer currentPlayer) {
|
2021-01-27 13:00:52 +01:00
|
|
|
|
this.currentPlayer = currentPlayer;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 11:21:33 +01:00
|
|
|
|
public State copy () {
|
|
|
|
|
State copy = new State(this.board, this.player1, this.player2,this.n1,this.n2);
|
2021-01-27 13:00:52 +01:00
|
|
|
|
for (int i=0; i<this.board.length;i++) {
|
|
|
|
|
for (int j=0; j<this.board.length; j++) {
|
2021-02-10 11:21:33 +01:00
|
|
|
|
copy.board[i][j] = this.board[i][j];
|
2021-01-27 13:00:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-10 11:21:33 +01:00
|
|
|
|
return copy;
|
2021-01-27 13:00:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 12:01:51 +01:00
|
|
|
|
public void switchPlayer() {
|
2021-02-10 11:21:33 +01:00
|
|
|
|
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
|
2021-01-27 13:00:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* TODO: display the current state of the board
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 11:49:49 +01:00
|
|
|
|
}
|