LOG320_Lab4/src/main/java/laboratoire4/PusherBoard.java

108 lines
3.0 KiB
Java

package laboratoire4;
public class PusherBoard {
private final Player player;
private Pawn[][] board;
public PusherBoard(Player player, String[] boardValues) {
this.player = player;
newGame(boardValues);
}
public void newGame(String[] boardValues) {
this.board = new Pawn[8][8];
int col = 0, row = 0;
for (String boardValue : boardValues) {
int v = Integer.parseInt(boardValue);
if (v != 0) {
Player pawnPlayer = (v == 1 || v == 2) ? Player.RED : Player.BLACK;
Pawn pawn;
if (v % 2 == 0) { // 2 et 4 sont les pushers
pawn = new Pusher(pawnPlayer, row, col);
} else {
pawn = new Pushed(pawnPlayer, row, col);
}
board[row][col] = pawn;
}
col++;
if (col == board.length) {
col = 0;
row++;
}
}
}
public String runNextMove() {
MiniMaxResult result = MiniMax.miniMax(this);
Pawn pawn = board[result.getRow()][result.getCol()];
System.out.println(result.getScore());
String initialPosition = pawn.getPosition();
move(pawn, result.getMovement());
String nextPosition = pawn.getPosition();
return initialPosition + "-" + nextPosition;
}
public void move(String move) {
String[] split = move.trim().split(" - ");
String from = split[0];
String to = split[1];
int fromCol = (int) from.charAt(0) - 65;
int fromRow = Integer.parseInt(String.valueOf(from.charAt(1))) - 1;
int toCol = (int) to.charAt(0) - 65;
Pawn.PawnMovement movement = Pawn.PawnMovement.from(toCol - fromCol);
move(fromRow, fromCol, movement);
}
public void move(int row, int col, Pawn.PawnMovement movement) {
Pawn pawn = board[row][col];
if (pawn == null) {
return;
}
move(pawn, movement);
}
private void move(Pawn pawn, Pawn.PawnMovement movement) {
// if (!pawn.isMoveValid(board, movement)) {
// return;
// }
int toRow = pawn.getRow() + pawn.getDirection();
int toCol = pawn.getCol() + movement.getMove();
board[pawn.getRow()][pawn.getCol()] = null;
board[toRow][toCol] = pawn;
pawn.move(movement);
}
public Player getPlayer() {
return player;
}
public Pawn[][] getBoard() {
return board;
}
public static void printBoard(Pawn[][] board) {
for (int i = 7; i >= 0; i--) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] != null) {
System.out.print(board[i][j] + " | ");
} else {
System.out.print(" | ");
}
}
System.out.println();
System.out.println("----------------------------------------------------------------------------------------");
}
}
}