converted board from int to AbstractPlayer

This commit is contained in:
Quentin Legot 2021-02-12 09:18:03 +01:00
parent 042a5dc9ea
commit ac9ddcab27
3 changed files with 42 additions and 25 deletions

4
.gitignore vendored
View File

@ -2,4 +2,6 @@
.project .project
.classpath .classpath
.metadata/ .metadata/
.settings .settings
.idea
*.iml

View File

@ -2,24 +2,30 @@ package othello;
import java.util.ArrayList; import java.util.ArrayList;
import othello.players.AbstractPlayer;
import othello.players.RandomPlayer;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
int p1 = 1, p2 = 2; AbstractPlayer p1 = new RandomPlayer();
int[][] board = initialize(p1, p2); AbstractPlayer p2 = new RandomPlayer();
AbstractPlayer[][] board = initialize(p1, p2);
State game = new State(board, p1, p2); State game = new State(board, p1, p2);
board[1][5]=game.getCurrentPlayer(); // board[1][5]=game.getCurrentPlayer();
/*while(!game.isOver()) { while(!game.isOver()) {
int player = game.getCurrentPlayer(); AbstractPlayer player = game.getCurrentPlayer();
ArrayList<Pair<Point, Point>> moves = game.getMove(player); ArrayList<Pair<Point, Point>> moves = game.getMove(player);
} */ game.toString();
ArrayList<Pair<Point, Point>> moves = game.getMove(game.getCurrentPlayer()); player.play(moves);
System.out.println(moves.toString()); }
// ArrayList<Pair<Point, Point>> moves = game.getMove(game.getCurrentPlayer());
// System.out.println(moves.toString());
} }
public static int[][] initialize(int p1, int p2){ public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){
int[][] board = new int[7][7]; AbstractPlayer[][] board = new AbstractPlayer[7][7];
board[0][0] = p2; board[0][0] = p2;
board[0][6] = p1; board[0][6] = p1;
board[6][0] = p1; board[6][0] = p1;

View File

@ -2,16 +2,18 @@ package othello;
import java.util.ArrayList; import java.util.ArrayList;
import othello.players.AbstractPlayer;
public class State { public class State {
private int[][] board; private AbstractPlayer[][] board;
private int player1; private AbstractPlayer player1;
private int player2; private AbstractPlayer player2;
private int currentPlayer; private AbstractPlayer currentPlayer;
private int n1; private int n1;
private int n2; private int n2;
public State(int[][] board, int p1, int p2, int n1, int n2) { public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2, int n1, int n2) {
this.board = board; this.board = board;
this.player1 = p1; this.player1 = p1;
this.player2 = p2; this.player2 = p2;
@ -20,7 +22,7 @@ public class State {
this.n2 = n2; this.n2 = n2;
} }
public State(int[][] board, int p1, int p2) { public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2) {
this(board, p1, p2, 2, 2); this(board, p1, p2, 2, 2);
} }
@ -30,7 +32,7 @@ public class State {
return getMove(player1).isEmpty() && getMove(player2).isEmpty(); return getMove(player1).isEmpty() && getMove(player2).isEmpty();
} }
public ArrayList<Pair<Point, Point>> getMove(int player) { public ArrayList<Pair<Point, Point>> getMove(AbstractPlayer player) {
// Pair<Depart, Arrivee> // Pair<Depart, Arrivee>
ArrayList<Pair<Point, Point>> moves = new ArrayList<>(); ArrayList<Pair<Point, Point>> moves = new ArrayList<>();
// Parcours du plateau de jeu // Parcours du plateau de jeu
@ -44,11 +46,10 @@ public class State {
if (deltaY != 0 && deltaX != 0) { if (deltaY != 0 && deltaX != 0) {
// Si une place libre est trouv<EFBFBD>e elle est ajout<EFBFBD>e à la liste des coups // Si une place libre est trouv<EFBFBD>e elle est ajout<EFBFBD>e à la liste des coups
try { try {
if (this.board[y+deltaY][x+deltaX]==0) { if (this.board[y+deltaY][x+deltaX] == null) {
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+deltaY, x+deltaX))); moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+deltaY, x+deltaX)));
} } else {
if(this.board[y+deltaY][x+deltaX]!=0){ if(this.board[y+2*deltaY][x+2*deltaX] == null)
if(this.board[y+2*deltaY][x+2*deltaX] == 0)
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+2*deltaY, x+2*deltaX))); moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+2*deltaY, x+2*deltaX)));
} }
} catch(ArrayIndexOutOfBoundsException ignored) {} } catch(ArrayIndexOutOfBoundsException ignored) {}
@ -61,7 +62,7 @@ public class State {
return moves; return moves;
} }
public int getScore(int player) { public int getScore(AbstractPlayer player) {
return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1); return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1);
} }
@ -87,11 +88,11 @@ public class State {
copy.switchPlayer(); copy.switchPlayer();
return copy; return copy;
} }
public int getCurrentPlayer() { public AbstractPlayer getCurrentPlayer() {
return currentPlayer; return currentPlayer;
} }
public void setCurrentPlayer(int currentPlayer) { public void setCurrentPlayer(AbstractPlayer currentPlayer) {
this.currentPlayer = currentPlayer; this.currentPlayer = currentPlayer;
} }
@ -109,4 +110,12 @@ public class State {
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1); setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
} }
/**
* TODO: display the current state of the board
*/
@Override
public String toString() {
return null;
}
} }