added State.toString and getWinner + fixed infinite loop

This commit is contained in:
Quentin Legot 2021-02-12 09:38:18 +01:00
parent ac9ddcab27
commit 10fae688ad
2 changed files with 30 additions and 8 deletions

View File

@ -13,15 +13,15 @@ public class Main {
AbstractPlayer p2 = new RandomPlayer(); AbstractPlayer p2 = new RandomPlayer();
AbstractPlayer[][] board = initialize(p1, p2); AbstractPlayer[][] board = initialize(p1, p2);
State game = new State(board, p1, p2); State game = new State(board, p1, p2);
// board[1][5]=game.getCurrentPlayer(); System.out.println("joueur 1: " + p1);
System.out.println("joueur 2: " + p2);
while(!game.isOver()) { while(!game.isOver()) {
AbstractPlayer 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(); System.out.println(game.toString());
player.play(moves); game.play(player.play(moves));
} }
// ArrayList<Pair<Point, Point>> moves = game.getMove(game.getCurrentPlayer()); System.out.println("C'est " + game.getWinner() + " qui a gagné");
// System.out.println(moves.toString());
} }
public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){ public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){

View File

@ -63,7 +63,16 @@ public class State {
} }
public int getScore(AbstractPlayer player) { public int getScore(AbstractPlayer player) {
return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1); return player == player1 ? n1/(n1+n2) : n2/(n2+n1);
}
public AbstractPlayer getWinner() {
int scoreP1 = getScore(player1), scoreP2 = getScore(player2);
if(scoreP1 > scoreP2)
return player1;
else if(scoreP2 > scoreP1)
return player2;
return null;
} }
public State play(Pair<Point,Point> pair) { public State play(Pair<Point,Point> pair) {
@ -115,7 +124,20 @@ public class State {
*/ */
@Override @Override
public String toString() { public String toString() {
return null; StringBuilder str = new StringBuilder();
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board.length; x++) {
if(board[y][x] == player1)
str.append("O");
else if(board[y][x] == player2)
str.append("X");
else
str.append(".");
str.append(" ");
}
str.append("\r\n");
}
return str.toString();
} }
} }