Compare commits

...

2 Commits

Author SHA1 Message Date
Gaspard VANCOMPERNOLLE ad8ddd6418 Merge branch 'master' of
https://gitarero.ecam.fr/g.vancompernolle/OOP_2B3_Project.git

Conflicts:
	src/backend/Board.java
2025-05-07 15:21:46 +02:00
Gaspard VANCOMPERNOLLE bdf56dc250 auto player ia 2025-05-07 15:15:10 +02:00
3 changed files with 77 additions and 16 deletions

View File

@ -1,17 +1,39 @@
package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer {
private Random rand = new Random();
public Move computeBestMove(Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
boolean whiteTurn = board.isTurnWhite();
/**
* returns the best Move to try on provided board for active player
* @param board
* @return
*/
public Move computeBestMove(Board board) {
for (Piece p : board.getPieces()) {
if (p.isWhite() == whiteTurn) {
ArrayList<int[]> moves = Move.getPossibleMoves(board, p);
for (int[] pos : moves) {
int toX = pos[0];
int toY = pos[1];
Piece captured = board.getPieceAt(toX, toY);
Move move = new Move(p, p.getX(), p.getY(), toX, toY, captured);
possibleMoves.add(move);
}
}
}
return null;
}
// Si aucune possibilité => null
if (possibleMoves.isEmpty()) return null;
// Cherche un coup qui capture une pièce
for (Move m : possibleMoves) {
if (m.getCapturedPiece() != null) {
return m;
}
}
// Sinon, retourne un coup aléatoire
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
}
}

View File

@ -257,6 +257,39 @@ public class Board {
public void playMove(Move move) {
if (move == null) return;
<<<<<<< HEAD
Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY());
if (pieceToMove == null) return;
// Supprimer la pièce d'origine
pieces.remove(pieceToMove);
// Supprimer la pièce capturée (si elle existe)
Piece captured = getPieceAt(move.getToX(), move.getToY());
if (captured != null) {
pieces.remove(captured);
}
// Ajouter la nouvelle position
Piece newPiece = new Piece(
pieceToMove.getType(),
pieceToMove.isWhite(),
move.getToX(),
move.getToY()
);
pieces.add(newPiece);
// Enregistrer dans l'historique pour undo
moveHistory.add(move);
// Fin de tour
turnNumber++;
// Désélection et surlignage off
selectedX = -1;
selectedY = -1;
highlightedSquares.clear();
=======
// Remove the moved piece from its old position
Piece from = move.getMovedPiece();
pieces.removeIf(p -> p.getX() == from.getX() && p.getY() == from.getY());
@ -272,8 +305,13 @@ public class Board {
moveHistory.add(move);
turnNumber++;
>>>>>>> branch 'master' of https://gitarero.ecam.fr/g.vancompernolle/OOP_2B3_Project.git
}
<<<<<<< HEAD
=======
>>>>>>> branch 'master' of https://gitarero.ecam.fr/g.vancompernolle/OOP_2B3_Project.git
public Piece getPieceAt(int x, int y) {
for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) {

View File

@ -48,9 +48,10 @@ public class Game extends Thread {
}
private void aiPlayerTurn() {
if(isAITurn()) {
board.playMove(aiPlayer.computeBestMove(new Board(board)));
}
if(isAITurn()) {
Move m = aiPlayer.computeBestMove(board);
board.playMove(m);
}
}
public void clickCoords(int x, int y) {