converted board from int to AbstractPlayer
This commit is contained in:
parent
042a5dc9ea
commit
ac9ddcab27
4
.gitignore
vendored
4
.gitignore
vendored
@ -2,4 +2,6 @@
|
||||
.project
|
||||
.classpath
|
||||
.metadata/
|
||||
.settings
|
||||
.settings
|
||||
.idea
|
||||
*.iml
|
||||
|
@ -2,24 +2,30 @@ package othello;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import othello.players.AbstractPlayer;
|
||||
import othello.players.RandomPlayer;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int p1 = 1, p2 = 2;
|
||||
int[][] board = initialize(p1, p2);
|
||||
AbstractPlayer p1 = new RandomPlayer();
|
||||
AbstractPlayer p2 = new RandomPlayer();
|
||||
AbstractPlayer[][] board = initialize(p1, p2);
|
||||
State game = new State(board, p1, p2);
|
||||
board[1][5]=game.getCurrentPlayer();
|
||||
/*while(!game.isOver()) {
|
||||
int player = game.getCurrentPlayer();
|
||||
// board[1][5]=game.getCurrentPlayer();
|
||||
while(!game.isOver()) {
|
||||
AbstractPlayer player = game.getCurrentPlayer();
|
||||
ArrayList<Pair<Point, Point>> moves = game.getMove(player);
|
||||
} */
|
||||
ArrayList<Pair<Point, Point>> moves = game.getMove(game.getCurrentPlayer());
|
||||
System.out.println(moves.toString());
|
||||
game.toString();
|
||||
player.play(moves);
|
||||
}
|
||||
// ArrayList<Pair<Point, Point>> moves = game.getMove(game.getCurrentPlayer());
|
||||
// System.out.println(moves.toString());
|
||||
}
|
||||
|
||||
public static int[][] initialize(int p1, int p2){
|
||||
int[][] board = new int[7][7];
|
||||
public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){
|
||||
AbstractPlayer[][] board = new AbstractPlayer[7][7];
|
||||
board[0][0] = p2;
|
||||
board[0][6] = p1;
|
||||
board[6][0] = p1;
|
||||
|
@ -2,16 +2,18 @@ package othello;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import othello.players.AbstractPlayer;
|
||||
|
||||
public class State {
|
||||
|
||||
private int[][] board;
|
||||
private int player1;
|
||||
private int player2;
|
||||
private int currentPlayer;
|
||||
private AbstractPlayer[][] board;
|
||||
private AbstractPlayer player1;
|
||||
private AbstractPlayer player2;
|
||||
private AbstractPlayer currentPlayer;
|
||||
private int n1;
|
||||
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.player1 = p1;
|
||||
this.player2 = p2;
|
||||
@ -20,7 +22,7 @@ public class State {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -30,7 +32,7 @@ public class State {
|
||||
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>
|
||||
ArrayList<Pair<Point, Point>> moves = new ArrayList<>();
|
||||
// Parcours du plateau de jeu
|
||||
@ -44,11 +46,10 @@ public class State {
|
||||
if (deltaY != 0 && deltaX != 0) {
|
||||
// Si une place libre est trouv<EFBFBD>e elle est ajout<EFBFBD>e à la liste des coups
|
||||
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)));
|
||||
}
|
||||
if(this.board[y+deltaY][x+deltaX]!=0){
|
||||
if(this.board[y+2*deltaY][x+2*deltaX] == 0)
|
||||
} else {
|
||||
if(this.board[y+2*deltaY][x+2*deltaX] == null)
|
||||
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+2*deltaY, x+2*deltaX)));
|
||||
}
|
||||
} catch(ArrayIndexOutOfBoundsException ignored) {}
|
||||
@ -61,7 +62,7 @@ public class State {
|
||||
return moves;
|
||||
}
|
||||
|
||||
public int getScore(int player) {
|
||||
public int getScore(AbstractPlayer player) {
|
||||
return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1);
|
||||
}
|
||||
|
||||
@ -87,11 +88,11 @@ public class State {
|
||||
copy.switchPlayer();
|
||||
return copy;
|
||||
}
|
||||
public int getCurrentPlayer() {
|
||||
public AbstractPlayer getCurrentPlayer() {
|
||||
return currentPlayer;
|
||||
}
|
||||
|
||||
public void setCurrentPlayer(int currentPlayer) {
|
||||
public void setCurrentPlayer(AbstractPlayer currentPlayer) {
|
||||
this.currentPlayer = currentPlayer;
|
||||
}
|
||||
|
||||
@ -109,4 +110,12 @@ public class State {
|
||||
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: display the current state of the board
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user