Othello/src/othello/State.java

118 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
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) {
ArrayList<Point> moves = null;
// 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
2021-01-27 18:27:28 +01:00
System.out.println("recherche");
for (int k=-1; k<2;k++) {
for (int l=-1; l<2; l++) {
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");
moves.add(new Point(i+k, j+l));
}
}
}
}
}
}
}
// 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
}