Merge branch 'master' of

https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
manon 2025-05-13 16:22:45 +02:00
parent fa417dc015
commit 4c7437b97f
7 changed files with 146 additions and 24 deletions

View File

@ -1,4 +1,5 @@
eclipse.preferences.version=1
encoding//src/Main.java=UTF-8
encoding//src/backend/AutoPlayer.java=UTF-8
encoding//src/backend/Board.java=UTF-8
encoding/<project>=windows-1252

View File

@ -33,4 +33,3 @@ public class Main {

View File

@ -1,17 +1,64 @@
package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer {
/**
* returns the best Move to try on provided board for active player
* @param board
* @return
*/
public Move computeBestMove(Board board) {
boolean isWhite = board.isTurnWhite();
ArrayList<Move> allMoves = new ArrayList<>();
ArrayList<Piece> pieces = board.getPieces();
return null;
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
if (piece.isWhite() == isWhite) {
ArrayList<int[]> moves = board.getValidMoves(piece);
for (int j = 0; j < moves.size(); j++) {
int[] moveCoords = moves.get(j);
int toX = moveCoords[0];
int toY = moveCoords[1];
Piece captured = board.getPieceAt(toX, toY);
Move move = new Move(piece, piece.getX(), piece.getY(), toX, toY, captured);
allMoves.add(move);
}
}
}
if (allMoves.isEmpty()) {
return null; // no legal moves maybe stalemate or checkmate
}
// === Basic Evaluation: Prefer capturing higher-value pieces ===
Move bestMove = allMoves.get(0);
int bestScore = evaluateCapture(bestMove.getCaptured());
for (int i = 1; i < allMoves.size(); i++) {
Move move = allMoves.get(i);
int score = evaluateCapture(move.getCaptured());
if (score > bestScore) {
bestMove = move;
bestScore = score;
}
}
return bestMove;
}
// Simple evaluation: return value of captured piece, or 0 if no capture
private int evaluateCapture(Piece captured) {
if (captured == null) return 0;
PieceType type = captured.getType();
if (type == PieceType.Pawn) return 1;
if (type == PieceType.Knight || type == PieceType.Bishop) return 3;
if (type == PieceType.Rook) return 5;
if (type == PieceType.Queen) return 9;
return 0; // king shouldn't be captured anyway
}
}

View File

@ -282,7 +282,41 @@ public class Board {
}
public void playMove(Move move) {
//TODO
if (move == null) return;
Piece piece = move.getPiece();
int toX = move.getToX();
int toY = move.getToY();
// Remove captured piece if any
Piece captured = move.getCaptured();
if (captured != null) {
pieces.remove(captured);
}
// Move the piece
piece.setX(toX);
piece.setY(toY);
// Switch turn
isWhiteTurn = !isWhiteTurn;
turnNumber++;
// Clear selection and highlights (if needed in GUI)
selectedX = -1;
selectedY = -1;
highlightedSquares.clear();
// Check status after move
for (int i = 0; i < 2; i++) {
boolean white = (i == 0);
if (isKingInCheck(white)) {
System.out.println((white ? "White" : "Black") + " is in check!");
if (isCheckmate(white)) {
System.out.println((white ? "White" : "Black") + " is in checkmate!");
}
}
}
}

View File

@ -23,7 +23,9 @@ public class Game extends Thread {
aiPlayer = new AutoPlayer();
}
public Board getBoard() {
return board;
}
public int getWidth() {
return board.getWidth();

View File

@ -1,9 +1,43 @@
package backend;
public class Move {
private Piece piece;
private int fromX;
private int fromY;
private int toX;
private int toY;
private Piece captured; // can be null
public Move(Piece piece, int fromX, int fromY, int toX, int toY, Piece captured) {
this.piece = piece;
this.fromX = fromX;
this.fromY = fromY;
this.toX = toX;
this.toY = toY;
this.captured = captured;
}
public Piece getPiece() {
return piece;
}
public int getFromX() {
return fromX;
}
public int getFromY() {
return fromY;
}
public int getToX() {
return toX;
}
public int getToY() {
return toY;
}
public Piece getCaptured() {
return captured;
}
}

View File

@ -46,6 +46,11 @@ public class MyInterface extends JFrame {
/**
* Create the frame.
*/
public void setGame(Game g) {
this.game = g;
}
public MyInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 650, 650);