Merge branch 'master' of

https://gitarero.ecam.fr/antoine.bahmed/OOP_1B3_Project.git
This commit is contained in:
HP 2025-05-16 10:30:34 +02:00
commit 85258bb365
3 changed files with 84 additions and 51 deletions

View File

@ -1,23 +1,23 @@
import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
import windowInterface.MyInterface;
public class Main {
public class Main {
public static void main(String[] args) {
// testing :
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
System.out.println(testBoard.toString());
public static void main(String[] args) {
// testing :
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
System.out.println(testBoard.toString());
// launches graphical interface :
MyInterface mjf = new MyInterface();
mjf.setVisible(true);
}
// launches graphical interface :
MyInterface mjf = new MyInterface();
mjf.setVisible(true);
}
}

View File

@ -4,13 +4,69 @@ import java.util.ArrayList;
public class AutoPlayer {
private static final int MAX_DEPTH = 2; // You can increase this for stronger AI
public Move computeBestMove(Board board) {
return minimax(board, MAX_DEPTH, board.isTurnWhite()).bestMove;
}
private static class ScoredMove {
Move bestMove;
int score;
ScoredMove(Move move, int score) {
this.bestMove = move;
this.score = score;
}
}
private ScoredMove minimax(Board board, int depth, boolean isMaximizing) {
if (depth == 0) {
return new ScoredMove(null, evaluateBoard(board));
}
ArrayList<Move> possibleMoves = generateAllMoves(board, isMaximizing);
if (possibleMoves.isEmpty()) {
return new ScoredMove(null, evaluateBoard(board));
}
Move bestMove = null;
int bestScore = isMaximizing ? Integer.MIN_VALUE : Integer.MAX_VALUE;
for (Move move : possibleMoves) {
Board boardCopy = new Board(board); // Deep copy
boardCopy.playMove(move);
int score = minimax(boardCopy, depth - 1, !isMaximizing).score;
if (isMaximizing && score > bestScore) {
bestScore = score;
bestMove = move;
} else if (!isMaximizing && score < bestScore) {
bestScore = score;
bestMove = move;
}
}
return new ScoredMove(bestMove, bestScore);
}
private int evaluateBoard(Board board) {
int score = 0;
for (Piece p : board.getPieces()) {
int value = pieceValue(p.getType());
score += p.isWhite() ? value : -value;
}
return score;
}
private ArrayList<Move> generateAllMoves(Board board, boolean isWhite) {
ArrayList<Move> allMoves = new ArrayList<>();
boolean isWhite = board.isTurnWhite();
for (Piece piece : board.getPieces()) {
if (piece.isWhite() != isWhite) continue;
ArrayList<int[]> legalMoves = getLegalMoves(board, piece);
ArrayList<int[]> legalMoves = board.computeLegalMoves(piece);
for (int[] move : legalMoves) {
Piece target = getPieceAt(board, move[0], move[1]);
Move candidate = new Move(
@ -23,26 +79,7 @@ public class AutoPlayer {
}
}
// Evaluate moves
Move bestMove = null;
int bestScore = Integer.MIN_VALUE;
for (Move move : allMoves) {
int score = evaluateMove(move);
if (score > bestScore) {
bestScore = score;
bestMove = move;
}
}
return bestMove;
}
private int evaluateMove(Move move) {
if (move.getCapturedPiece() != null) {
return pieceValue(move.getCapturedPiece().getType());
}
return 0;
return allMoves;
}
private int pieceValue(PieceType type) {
@ -57,11 +94,6 @@ public class AutoPlayer {
}
}
private ArrayList<int[]> getLegalMoves(Board board, Piece piece) {
// Reuse board's internal method
// But we must simulate access: simulate selection, compute, and reset
return board.computeLegalMoves(piece);
}
private Piece getPieceAt(Board board, int x, int y) {
for (Piece p : board.getPieces()) {

View File

@ -586,3 +586,4 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
}
}