Merge branch 'master' of
https://gitarero.ecam.fr/antoine.bahmed/OOP_1B3_Project.git
This commit is contained in:
commit
85258bb365
|
|
@ -1,23 +1,23 @@
|
||||||
import backend.Board;
|
import backend.Board;
|
||||||
import backend.Move;
|
import backend.Move;
|
||||||
import backend.Piece;
|
import backend.Piece;
|
||||||
import backend.PieceType;
|
import backend.PieceType;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// testing :
|
// testing :
|
||||||
Board testBoard = new Board(8, 8);
|
Board testBoard = new Board(8, 8);
|
||||||
testBoard.populateBoard();
|
testBoard.populateBoard();
|
||||||
System.out.println(testBoard.toString());
|
System.out.println(testBoard.toString());
|
||||||
|
|
||||||
// launches graphical interface :
|
// launches graphical interface :
|
||||||
MyInterface mjf = new MyInterface();
|
MyInterface mjf = new MyInterface();
|
||||||
mjf.setVisible(true);
|
mjf.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,69 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
|
|
||||||
|
private static final int MAX_DEPTH = 2; // You can increase this for stronger AI
|
||||||
|
|
||||||
public Move computeBestMove(Board board) {
|
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<>();
|
ArrayList<Move> allMoves = new ArrayList<>();
|
||||||
boolean isWhite = board.isTurnWhite();
|
|
||||||
|
|
||||||
for (Piece piece : board.getPieces()) {
|
for (Piece piece : board.getPieces()) {
|
||||||
if (piece.isWhite() != isWhite) continue;
|
if (piece.isWhite() != isWhite) continue;
|
||||||
ArrayList<int[]> legalMoves = getLegalMoves(board, piece);
|
|
||||||
|
ArrayList<int[]> legalMoves = board.computeLegalMoves(piece);
|
||||||
for (int[] move : legalMoves) {
|
for (int[] move : legalMoves) {
|
||||||
Piece target = getPieceAt(board, move[0], move[1]);
|
Piece target = getPieceAt(board, move[0], move[1]);
|
||||||
Move candidate = new Move(
|
Move candidate = new Move(
|
||||||
|
|
@ -23,26 +79,7 @@ public class AutoPlayer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate moves
|
return allMoves;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int pieceValue(PieceType type) {
|
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) {
|
private Piece getPieceAt(Board board, int x, int y) {
|
||||||
for (Piece p : board.getPieces()) {
|
for (Piece p : board.getPieces()) {
|
||||||
|
|
|
||||||
|
|
@ -485,7 +485,7 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (type == PieceType.Rook) {
|
if (type == PieceType.Rook) {
|
||||||
// Directions : haut, bas, gauche, droite
|
// Directions : haut, bas, gauche, droite
|
||||||
|
|
@ -585,4 +585,5 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue