Othello/src/othello/State.java

113 lines
2.4 KiB
Java
Raw Normal View History

2021-01-27 11:49:49 +01:00
package othello;
import java.awt.Point;
2021-01-27 13:00:52 +01:00
import java.util.ArrayList;
2021-01-27 11:49:49 +01:00
public class State {
2021-01-27 13:00:52 +01:00
private int[][] board;
private int player1;
private int player2;
private int currentPlayer;
2021-02-02 09:14:10 +01:00
private int n1;
private int n2;
2021-01-27 12:24:17 +01:00
public State(int[][] board, int p1, int p2, int n1, int n2) {
2021-01-27 12:24:17 +01:00
this.board = board;
this.player1 = p1;
this.player2 = p2;
currentPlayer = p1;
this.n1 = n1;
this.n2 = n2;
}
public State(int[][] board, int p1, int p2) {
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-01-27 11:49:49 +01:00
return false;
}
2021-01-27 18:27:28 +01:00
public ArrayList<Point> getMove(int player) {
2021-02-10 10:21:26 +01:00
ArrayList<Point> moves = new ArrayList<Point>();
// Clonage
// Parcours du plateau de jeu
for (int i=0; i<this.board.length;i++) {
for (int j=0; j<this.board.length; j++) {
if (this.board[i][j] == this.currentPlayer) {
// Recherche autour du pion du joueur courant
for (int k=-1; k<2;k++) {
for (int l=-1; l<2; l++) {
// La position du pion trouv<75> est exclue
if (k!=0 || l!=0) {
2021-02-10 10:21:26 +01:00
// Si une place libre est trouvée elle est ajoutée à la liste de coups
if ( ((i+k >= 0) && (i+k < 7 )) && ((j+l >= 0) && (j+l < 7 )) && (this.board[i+k][j+l]==0)) {
moves.add(new Point(i+k, j+l));
2021-02-10 10:43:39 +01:00
}
}
}
}
}
}
}
// Saut
return moves;
2021-01-27 11:49:49 +01:00
}
2021-01-27 18:27:28 +01:00
public int getScore(int player) {
2021-02-02 09:14:10 +01:00
if (currentPlayer == 1)
return n1/(n1+n2);
else
return n2/(n2+n1);
2021-01-27 11:49:49 +01:00
}
2021-01-27 13:00:52 +01:00
public State play(int x, int y) {
State copy = this.copy();
copy.board[x][y] = 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 {
copy.board[x+i][y+z] = copy.getCurrentPlayer();
increment+=1;
} catch (IndexOutOfBoundsException ignored) {}
2021-02-02 09:14:10 +01:00
}
}
if (currentPlayer == 1){
copy.n1 += increment;
2021-02-02 09:14:10 +01:00
}else{
copy.n2 += increment;
2021-02-02 09:14:10 +01:00
}
copy.switchPlayer();
return copy;
2021-01-27 11:49:49 +01:00
}
2021-01-27 13:00:52 +01:00
public int getCurrentPlayer() {
2021-01-27 12:24:17 +01:00
return currentPlayer;
}
2021-01-27 11:49:49 +01:00
2021-01-27 13:00:52 +01:00
public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
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++) {
copy.board[i][j] = this.board[i][j];
2021-01-27 13:00:52 +01:00
}
}
return copy;
2021-01-27 13:00:52 +01:00
}
public void switchPlayer () {
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
2021-01-27 13:00:52 +01:00
}
2021-01-27 11:49:49 +01:00
}