Othello/src/othello/State.java

128 lines
2.8 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
2021-02-02 09:14:10 +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;
2021-02-02 09:14:10 +01:00
this.n1 = n1+2;
this.n2 = n2+2;
2021-01-27 12:24:17 +01:00
}
2021-01-27 11:49:49 +01:00
public boolean isOver() {
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++) {
2021-02-10 10:22:22 +01:00
<<<<<<< HEAD
2021-02-10 10:21:26 +01:00
// La position du pion trouvé 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)) {
2021-02-10 10:22:22 +01:00
=======
2021-02-10 09:49:32 +01:00
<<<<<<< HEAD
2021-02-10 09:48:23 +01:00
if (k!=0 || l!=0) {
2021-02-10 09:49:32 +01:00
=======
2021-02-02 09:14:10 +01:00
// La position du pion trouv<75> est exclue
if (k!=0 || l!=0) {
2021-02-02 09:14:10 +01:00
// Si une place libre est trouv<75>e elle est ajout<75> <20> la liste de coups
2021-01-27 18:27:28 +01:00
System.out.println("close");
if ( (this.board[i+k][j+l]==0) && (i+k >= 0) && (i+k < 7 ) && (j+l >= 0) && (j+l < 7 ) ) {
System.out.println("jadd");
2021-02-10 09:49:32 +01:00
>>>>>>> 2263d0614b420497f888985e269e89cd19670bab
2021-02-10 10:22:22 +01:00
>>>>>>> a28cb1daa55fe0e031900c11927481008e642a1b
moves.add(new Point(i+k, j+l));
2021-02-10 09:48:23 +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 copie = this.copie();
copie.board[x][y] = copie.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 {
copie.board[x+i][y+z] = copie.getCurrentPlayer();
increment+=1;
} catch (Exception e) {
}
}
}
if (currentPlayer == 1){
copie.n1 += increment;
}else{
copie.n2 += increment;
}
2021-01-27 13:00:52 +01:00
copie.switchPlayer();
return copie;
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 copie () {
2021-02-02 09:14:10 +01:00
State copie = 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++) {
copie.board[i][j] = this.board[i][j];
}
}
return copie;
}
public void switchPlayer () {
if (getCurrentPlayer()==this.player1) {
setCurrentPlayer(player2);
}
else {
setCurrentPlayer(player1);
}
}
2021-01-27 11:49:49 +01:00
}