Merge branch 'master' of
https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
parent
fa417dc015
commit
4c7437b97f
|
|
@ -1,4 +1,5 @@
|
||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
encoding//src/Main.java=UTF-8
|
encoding//src/Main.java=UTF-8
|
||||||
|
encoding//src/backend/AutoPlayer.java=UTF-8
|
||||||
encoding//src/backend/Board.java=UTF-8
|
encoding//src/backend/Board.java=UTF-8
|
||||||
encoding/<project>=windows-1252
|
encoding/<project>=windows-1252
|
||||||
|
|
|
||||||
|
|
@ -33,4 +33,3 @@ public class Main {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,64 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the best Move to try on provided board for active player
|
|
||||||
* @param board
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Move computeBestMove(Board board) {
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -282,7 +282,41 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playMove(Move move) {
|
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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,9 @@ public class Game extends Thread {
|
||||||
aiPlayer = new AutoPlayer();
|
aiPlayer = new AutoPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Board getBoard() {
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return board.getWidth();
|
return board.getWidth();
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,43 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
|
|
||||||
public class Move {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -46,6 +46,11 @@ public class MyInterface extends JFrame {
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public void setGame(Game g) {
|
||||||
|
this.game = g;
|
||||||
|
}
|
||||||
|
|
||||||
public MyInterface() {
|
public MyInterface() {
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(10, 10, 650, 650);
|
setBounds(10, 10, 650, 650);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue