Merge branch 'master' of
https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
Louise BERTELOOT 2025-05-21 20:45:43 +02:00
commit cf8a92b092
7 changed files with 39 additions and 170 deletions

View File

@ -1,5 +1,5 @@
7 2
false true
Rook,0,0,false Rook,0,0,false
Knight,1,0,false Knight,1,0,false
Bishop,2,0,false Bishop,2,0,false
@ -9,7 +9,7 @@ Bishop,5,0,false
Knight,6,0,false Knight,6,0,false
Rook,7,0,false Rook,7,0,false
Pawn,0,1,false Pawn,0,1,false
Pawn,1,1,false Pawn,1,3,false
Pawn,2,1,false Pawn,2,1,false
Pawn,3,1,false Pawn,3,1,false
Pawn,4,1,false Pawn,4,1,false

View File

@ -20,9 +20,26 @@ public class AutoPlayer {
int[] moveCoords = moves.get(j); int[] moveCoords = moves.get(j);
int toX = moveCoords[0]; int toX = moveCoords[0];
int toY = moveCoords[1]; int toY = moveCoords[1];
int fromX = piece.getX();
int fromY = piece.getY();
Piece captured = board.getPieceAt(toX, toY); Piece captured = board.getPieceAt(toX, toY);
Move move = new Move(piece, piece.getX(), piece.getY(), toX, toY, captured); // Detect en passant
if (piece.getType() == PieceType.Pawn && board.isEnPassant() && toX == board.getXCoordinatePawn() &&
toY == (piece.isWhite() ? board.getYCoordinatePawn() - 1 : board.getYCoordinatePawn() + 1)) {
captured = board.getPieceAt(board.getXCoordinatePawn(), board.getYCoordinatePawn());
}
// --- Castling detection ---
if (piece.getType() == PieceType.King && Math.abs(toX - fromX) == 2 && toY == fromY) {
System.out.println("AI considering castling move.");
// No need to set captured, just allow the move
}
Move move = new Move(piece, piece.getX(), piece.getY(), toX, toY, captured);
allMoves.add(move); allMoves.add(move);
} }
} }

View File

@ -16,8 +16,13 @@ public class Board {
private int xCoordinatePawn; private int xCoordinatePawn;
private int yCoordinatePawn; private int yCoordinatePawn;
private boolean enPassant; private boolean enPassant;
<<<<<<< HEAD
private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore
=======
private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
private ArrayList<Board> previousStates; private ArrayList<Board> previousStates;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
@ -381,7 +386,10 @@ public class Board {
} }
public void playMove(Move move) { public void playMove(Move move) {
if (move == null) return; if (move == null || move.getPiece() == null) {
System.out.println("Invalid move: " + move);
return;
}
Piece piece = move.getPiece(); Piece piece = move.getPiece();
int toX = move.getToX(); int toX = move.getToX();

View File

@ -10,7 +10,7 @@ public class Game extends Thread {
private MyInterface mjf; private MyInterface mjf;
private int COL_NUM = 8; private int COL_NUM = 8;
private int LINE_NUM = 8; private int LINE_NUM = 8;
private int loopDelay = 250; private int loopDelay = 1000;
boolean[] activationAIFlags; boolean[] activationAIFlags;
public Game(MyInterface mjfParam) { public Game(MyInterface mjfParam) {
@ -52,9 +52,10 @@ public class Game extends Thread {
} }
private void aiPlayerTurn() { private void aiPlayerTurn() {
if(isAITurn()) { if (isAITurn()) {
board.playMove(aiPlayer.computeBestMove(new Board(board))); Move move = aiPlayer.computeBestMove(board); // Use the actual board
} board.playMove(move); // Move from the actual board
}
} }
public void clickCoords(int x, int y) { public void clickCoords(int x, int y) {

View File

@ -6,16 +6,6 @@ public class Piece {
private boolean isWhite; private boolean isWhite;
private PieceType type; private PieceType type;
/*private boolean enPassantEligible = false;
public boolean isEnPassantEligible() {
return enPassantEligible;
}
public void setEnPassantEligible(boolean eligible) {
this.enPassantEligible = eligible;
}
*/
public Piece(int x, int y, boolean isWhite, PieceType type) { public Piece(int x, int y, boolean isWhite, PieceType type) {
this.x=x; this.x=x;
this.y=y; this.y=y;

View File

@ -1,147 +0,0 @@
/*package backend;
import java.util.ArrayList;
public class SpecialMoves {
private Piece piece;
private Board board;
private boolean isWhite;
public SpecialMoves(Piece piece, Board board) {
this.piece = piece;
this.board = board;
this.isWhite = piece.isWhite();
}
private int x = piece.getX();
private int y = piece.getY();
private boolean pawnDoubleStep = board.isPawnDoubleStep();
private int xCoordinatePawn = board.getXCoordinatePawn();
private int yCoordinatePawn = board.getYCoordinatePawn();
for (int n = 2; n < 4; n++) {
int i = x + pieceMoves[n][0]
int j = y + pieceMoves[n][1];
Move move = new Move(x, y, i, j);
// Diagonal capture
if (inBoard(i, j) &&
board.getPiece(j, i) != null &&
board.getPiece(j, i).isWhite() != isWhite &&
!checkIf(move, coordinates[0], coordinates[1], isWhite)) {
moves.add(new int[]{i, j});
}
// En Passant
else if (inBoard(i, j) &&
board.getPiece(j, i) == null &&
pawnDoubleStep &&
i == xCoordinatePawn &&
y == yCoordinatePawn &&
Math.abs(x - xCoordinatePawn) == 1 &&
board.getPiece(y, xCoordinatePawn) != null &&
board.getPiece(y, xCoordinatePawn).getType() == PieceType.Pawn &&
board.getPiece(y, xCoordinatePawn).isWhite() != isWhite &&
!checkIf(move, coordinates[0], coordinates[1], isWhite)) {
moves.add(new int[]{i, j});
}
}
return moves;
}
private boolean inBoard(int x, int y) {
return x >= 0 && x < 8 && y >= 0 && y < 8;
}
private boolean checkIf(Move move, int x, int y, boolean isWhite) {
// Stub replace with your actual move-check logic
return false;
}
}
/* public SpecialMoves(Piece piece, Board board) {
this.piece = piece;
this.board = board;
} //???
public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) {
if (type == PieceType.Pawn || isWhite == true || y == 6) {
int[][] offsets = {{1, -2}, {-1, -2}};
}
if (type == PieceType.Pawn || isWhite == false || y == 1) {
int[][] offsets = {{1, 2}, {-1, 2}};
}*/
/* public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) {
if (type == PieceType.Pawn || isWhite == true || y == 3) {
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
int[][] offsets = {{1, 0}, {-1, 0}};
}
if (type == PieceType.Pawn && isWhite == false && y == 4) {
int[][] offsets = {{1, 0}, {-1, 0}};
}
}
public boolean isEnpassant() {
boolean isWhite = piece.isWhite();
<<<<<<< HEAD
if (boolean isWhite == true && PieceType getType() == PieceType.Pawn) { //no idea honestly
=======
if (isWhite == true || PieceType getType() == PieceType.Pawn) { //no idea honestly
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
if ()
}
if (boolean isWhite == false && //PieceType type == Pawn) {
if ()
}
}
}
package backend;
public class SpecialMoves {
private Piece piece;
private Board board;
public SpecialMoves(Piece piece, Board board) {
this.piece = piece;
this.board = board;
}
// Call this when trying to move a pawn to check for en passant
public boolean isEnpassant(int fromX, int fromY, int toX, int toY) {
if (piece.getType() != PieceType.Pawn) return false;
boolean isWhite = piece.isWhite();
// En passant capture happens diagonally
int dx = toX - fromX;
int dy = toY - fromY;
if (Math.abs(dx) != 1 || (isWhite && dy != -1) || (!isWhite && dy != 1)) {
return false;
}
// Check the adjacent piece
Piece sidePawn = board.getPieceAt(toX, fromY);
if (sidePawn == null || sidePawn.getType() != PieceType.Pawn || sidePawn.isWhite() == isWhite) {
*/

View File

@ -85,18 +85,18 @@ public class JPanelChessBoard extends JPanel {
if (myGame == null) { if (myGame == null) {
// Display title before game starts // Display title before game starts
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.setFont(g.getFont().deriveFont(48f)); // larger font size g.setFont(g.getFont().deriveFont(48f));
String title = "Chess Project"; String title = "Chess Project";
int stringWidth = g.getFontMetrics().stringWidth(title); int stringWidth = g.getFontMetrics().stringWidth(title);
int x = (getWidth() - stringWidth) / 2; int x = (getWidth() - stringWidth) / 2;
int y = getHeight() / 2; int y = getHeight() / 2;
g.drawString(title, x, y); g.drawString(title, x, y);
g.setFont(g.getFont().deriveFont(24f)); // Smaller font for subtitle g.setFont(g.getFont().deriveFont(24f));
String subtitle = "Groupe 2A5"; String subtitle = "Groupe 2A5";
int subtitleWidth = g.getFontMetrics().stringWidth(subtitle); int subtitleWidth = g.getFontMetrics().stringWidth(subtitle);
int x2 = (getWidth() - subtitleWidth) / 2; int x2 = (getWidth() - subtitleWidth) / 2;
int y2 = y + 40; // 40 pixels below the title, adjust spacing if needed int y2 = y + 40;
g.drawString(subtitle, x2, y2); g.drawString(subtitle, x2, y2);
return; return;