diff --git a/src/backend/Board.java b/src/backend/Board.java index c003f94..79d5b5c 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -243,4 +243,4 @@ public class Board { public Board(Board board) { // TODO } -} \ No newline at end of file +} diff --git a/src/backend/Game.java b/src/backend/Game.java index 3b21ad6..e9736ec 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -4,125 +4,125 @@ import windowInterface.MyInterface; public class Game extends Thread { - private AutoPlayer aiPlayer; - private Board board; + private AutoPlayer aiPlayer; + private Board board; - private MyInterface mjf; - private int COL_NUM = 8; - private int LINE_NUM = 8; - private int loopDelay = 250; - boolean[] activationAIFlags; - - public Game(MyInterface mjfParam) { - mjf = mjfParam; - board = new Board(COL_NUM, LINE_NUM); - loopDelay = 250; - LINE_NUM = 8; - COL_NUM = 8; - activationAIFlags = new boolean[2]; - aiPlayer = new AutoPlayer(); - } + private MyInterface mjf; + private int COL_NUM = 8; + private int LINE_NUM = 8; + private int loopDelay = 250; + boolean[] activationAIFlags; - public int getWidth() { - return board.getWidth(); - } + public Game(MyInterface mjfParam) { + mjf = mjfParam; + board = new Board(COL_NUM, LINE_NUM); + loopDelay = 250; + LINE_NUM = 8; + COL_NUM = 8; + activationAIFlags = new boolean[2]; + aiPlayer = new AutoPlayer(); + } - public int getHeight() { - return board.getHeight(); - } + public int getWidth() { + return board.getWidth(); + } - public void run() { - while(true) { - aiPlayerTurn(); - mjf.update(board.getTurnNumber(), board.isTurnWhite()); - try { - Thread.sleep(loopDelay); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - private boolean isAITurn() { - return activationAIFlags[board.isTurnWhite()?1:0]; - } + public int getHeight() { + return board.getHeight(); + } - private void aiPlayerTurn() { - if(isAITurn()) { - board.playMove(aiPlayer.computeBestMove(new Board(board))); - } - } + public void run() { + while(true) { + aiPlayerTurn(); + mjf.update(board.getTurnNumber(), board.isTurnWhite()); + try { + Thread.sleep(loopDelay); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } - public void clickCoords(int x, int y) { - int width = this.getWidth(); - int height = this.getHeight(); - if(0>x || 0>y || x>width || y>height) { - System.out.println("Click out of bounds"); - return; - } - if(!isAITurn()) { - - if (board.getSelectedX() == -1 && board.getSelectedY() == -1) { - Piece piece = null; - for (Piece p : board.getPieces()) { - if (p.getX() == x && p.getY() == y) { - piece = p; - break; - } - } - if (piece != null) { - - boolean isWhiteTurn = board.isTurnWhite(); - if (piece.isWhite() != isWhiteTurn) { - System.out.println("Cannot select a" + (piece.isWhite() ? "white" : "black") + "piece on" + (isWhiteTurn ? "White" : "Black") + "'s turn"); - return; - } - } - - } - board.userTouch(x, y); - } - } + private boolean isAITurn() { + return activationAIFlags[board.isTurnWhite()?1:0]; + } - public void setPiece(boolean isWhite, PieceType type, int x, int y) { - board.setPiece(isWhite, type, x, y); - } + private void aiPlayerTurn() { + if(isAITurn()) { + board.playMove(aiPlayer.computeBestMove(new Board(board))); + } + } - public String[] getFileRepresentation() { - return board.toFileRep(); - } + public void clickCoords(int x, int y) { + int width = this.getWidth(); + int height = this.getHeight(); + if(0>x || 0>y || x>width || y>height) { + System.out.println("Click out of bounds"); + return; + } + if(!isAITurn()) { - public void setLoopDelay(int delay) { - this.loopDelay = delay; - } + if (board.getSelectedX() == -1 && board.getSelectedY() == -1) { + Piece piece = null; + for (Piece p : board.getPieces()) { + if (p.getX() == x && p.getY() == y) { + piece = p; + break; + } + } + if (piece != null) { - public void setDefaultSetup() { - board.cleanBoard(); - board.populateBoard(); - } + boolean isWhiteTurn = board.isTurnWhite(); + if (piece.isWhite() != isWhiteTurn) { + System.out.println("Cannot select a" + (piece.isWhite() ? "white" : "black") + "piece on" + (isWhiteTurn ? "White" : "Black") + "'s turn"); + return; + } + } - public void setBoard(String[] array) { - board = new Board(array); - } + } + board.userTouch(x, y); + } + } - public Iterable getPieces() { - return board.getPieces(); - } + public void setPiece(boolean isWhite, PieceType type, int x, int y) { + board.setPiece(isWhite, type, x, y); + } - public boolean isSelected(int x, int y) { - return board.isSelected(x, y); - } + public String[] getFileRepresentation() { + return board.toFileRep(); + } - public boolean isHighlighted(int x, int y) { - return board.isHighlighted(x, y); - } + public void setLoopDelay(int delay) { + this.loopDelay = delay; + } - public void undoLastMove() { - board.undoLastMove(); - } + public void setDefaultSetup() { + board.cleanBoard(); + board.populateBoard(); + } - public void toggleAI(boolean isWhite) { - this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0]; - } + public void setBoard(String[] array) { + board = new Board(array); + } + + public Iterable getPieces() { + return board.getPieces(); + } + + public boolean isSelected(int x, int y) { + return board.isSelected(x, y); + } + + public boolean isHighlighted(int x, int y) { + return board.isHighlighted(x, y); + } + + public void undoLastMove() { + board.undoLastMove(); + } + + public void toggleAI(boolean isWhite) { + this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0]; + } } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 34ce044..a461398 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -1,160 +1,163 @@ package backend; +import java.util.ArrayList; + public class Piece { - - private int x; - private int y; - private PieceType type; - private boolean isWhite; - - public Piece(int x, int y, PieceType type, boolean isWhite) { - this.x = x; - this.y = y; - this.type = type; - this.isWhite = isWhite; - } - - public int getX() { - return this.x; - } - public int getY() { - return this.y; - } - - public PieceType getType() { - return this.type; - } - - public boolean isWhite() { - return this.isWhite; - } - - public boolean canMoveTo(int toX, int toY, Board board) { - - if (x == toX && y == toY) { - return false; - } - - int dx = Math.abs(toX - x); - int dy = Math.abs(toY -y); - - switch (type) { - case Pawn: - return canPawnMoveTo(toX, toY, board); - case Rook: - return canRookMoveTo(toX, toY, dx, dy, board); + private int x; + private int y; + private PieceType type; + private boolean isWhite; + + + public Piece(int x, int y, PieceType type, boolean isWhite) { + this.x = x; + this.y = y; + this.type = type; + this.isWhite = isWhite; + // this.hasMoved = false; // Initialiser si ajouté + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public PieceType getType() { + return this.type; + } + + public boolean isWhite() { + return this.isWhite; + } + + // Méthode pour mettre à jour la position (utilisée dans Board.playMove) + public void setPosition(int newX, int newY) { + this.x = newX; + this.y = newY; + // this.hasMoved = true; // Mettre à jour si la variable hasMoved existe + } + + + + + public boolean canMoveTo(int toX, int toY, Board board) { + + if (x == toX && y == toY) { + return false; + } + + + if (toX < 0 || toX >= board.getWidth() || toY < 0 || toY >= board.getHeight()) { + return false; + } + + Piece pieceAtDestination = getPieceAt(board, toX, toY); + + + if (pieceAtDestination != null && pieceAtDestination.isWhite() == this.isWhite) { + return false; + } + + // Calculs spécifiques par type de pièce + int dx = Math.abs(toX - x); + int dy = Math.abs(toY - y); // Note: dy calculé ici, mais canPawnMoveTo recalcule avec signe + + switch (type) { + case Pawn: + + return canPawnMoveTo(toX, toY, board, pieceAtDestination); + case Rook: + + return (dx == 0 || dy == 0) && isPathClear(x, y, toX, toY, board); case Knight: - return canKnightMoveTo(dx, dy); + + return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); case Bishop: - return canBishopMoveTo(toX, toY, dx, dy, board); + + return (dx == dy) && isPathClear(x, y, toX, toY, board); case Queen: - return canQueenMoveTo(toX, toY, dx, dy, board); + + return (dx == 0 || dy == 0 || dx == dy) && isPathClear(x, y, toX, toY, board); case King: - return canKingMoveTo(dx, dy); + + return dx <= 1 && dy <= 1; default: - return false; // should never happen - } - } - - private boolean canPawnMoveTo(int toX, int toY, Board board) { - int dx = toX - x; - int dy = toY - y; - int fowardDirection = isWhite ? -1 : 1; - int startingRow = isWhite ? 6 : 1; - - Piece pieceAtDestination = null; - for (Piece p : board.getPieces()) { - if (p.getX() == toX && p.getY() == toY) { - pieceAtDestination = p; - break; - - } - } - - if (dx == 0) { - if (pieceAtDestination != null) { - return true; - } - - if (dy == fowardDirection) { - return true; - } - - if (dy == 2 * fowardDirection && y == startingRow) { - int intermediateY = y + fowardDirection; - for (Piece p : board.getPieces()) { - if (p.getX() == x && p.getY() == intermediateY) { - return false; //path is blocked - } - } - return true; - } - } - - if (Math.abs(dx) == 1 && dy == fowardDirection) { - if (pieceAtDestination != null && pieceAtDestination.isWhite() != isWhite) { - return true; - } - } - return false; -} - - private boolean canRookMoveTo(int toX, int toY, int dx, int dy, Board board) { - // Rook moves horizontally or vertically - if (dx != 0 && dy != 0) { - return false; // Not a horizontal or vertical move + return false; } - // Check the path for blocking pieces - return isPathClear(x, y, toX, toY, board); } - private boolean canKnightMoveTo(int dx, int dy) { - // Knight moves in an L-shape: (2,1) or (1,2) - return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); - } + private boolean canPawnMoveTo(int toX, int toY, Board board, Piece pieceAtDestination) { + int deltaX = toX - x; + int deltaY = toY - y; + int forwardDirection = isWhite ? -1 : 1; + int startingRow = isWhite ? 6 : 1; - private boolean canBishopMoveTo(int toX, int toY, int dx, int dy, Board board) { - // Bishop moves diagonally - if (dx != dy) { - return false; // Not a diagonal move + + if (deltaX == 0 && deltaY == forwardDirection && pieceAtDestination == null) { + + return true; } - // Check the path for blocking pieces - return isPathClear(x, y, toX, toY, board); - } - private boolean canQueenMoveTo(int toX, int toY, int dx, int dy, Board board) { - // Queen moves horizontally, vertically, or diagonally - if (dx != 0 && dy != 0 && dx != dy) { - return false; // Not a horizontal, vertical, or diagonal move + + if (deltaX == 0 && deltaY == 2 * forwardDirection && y == startingRow && pieceAtDestination == null) { + + int intermediateY = y + forwardDirection; + if (getPieceAt(board, x, intermediateY) == null) { + return true; + } } - // Check the path for blocking pieces - return isPathClear(x, y, toX, toY, board); + + + if (Math.abs(deltaX) == 1 && deltaY == forwardDirection && pieceAtDestination != null ) { + + return true; + } + + + + return false; } - private boolean canKingMoveTo(int dx, int dy) { - // King moves one square in any direction - return dx <= 1 && dy <= 1; + + private Piece getPieceAt(Board board, int targetX, int targetY) { + for (Piece p : board.getPieces()) { + if (p.getX() == targetX && p.getY() == targetY) { + return p; + } + } + return null; } - // Helper method to check if the path between (fromX, fromY) and (toX, toY) is clear - private boolean isPathClear(int fromX, int fromY, int toX, int toY, Board board) { + + private boolean isPathClear(int fromX, int fromY, int toX, int toY, Board board) { int dx = toX - fromX; int dy = toY - fromY; int steps = Math.max(Math.abs(dx), Math.abs(dy)); - int stepX = dx == 0 ? 0 : dx / Math.abs(dx); // Direction of x movement - int stepY = dy == 0 ? 0 : dy / Math.abs(dy); // Direction of y movement - // Check each square along the path, excluding the starting and ending positions - for (int i = 1; i < steps; i++) { - int checkX = fromX + i * stepX; - int checkY = fromY + i * stepY; - for (Piece p : board.getPieces()) { - if (p.getX() == checkX && p.getY() == checkY) { - return false; // Path is blocked - } - } - } - return true; - } -} + + if (steps <= 1) { + return true; + } + + + int stepX = Integer.signum(dx); + int stepY = Integer.signum(dy); + + + for (int i = 1; i < steps; i++) { + int checkX = fromX + i * stepX; + int checkY = fromY + i * stepY; + if (getPieceAt(board, checkX, checkY) != null) { + + return false; + } + } + + + return true; + } +} \ No newline at end of file diff --git a/src/backend/PieceType.java b/src/backend/PieceType.java index baceab1..4c8a7ca 100644 --- a/src/backend/PieceType.java +++ b/src/backend/PieceType.java @@ -1,28 +1,27 @@ package backend; public enum PieceType { - Pawn, Rook, Knight, Bishop, Queen, King; + Pawn, Rook, Knight, Bishop, Queen, King; - public String getSummary() { - if(this == PieceType.Knight) { - return "N"; - } - return this.name().substring(0, 1); - } - - public static PieceType fromSummary(char c) { - if(c=='P') { - return PieceType.Pawn; - }else if(c=='N') { - return PieceType.Knight; - }else if(c=='B') { - return PieceType.Bishop; - }else if(c=='R') { - return PieceType.Rook; - }else if(c=='K') { - return PieceType.King; - } - return PieceType.Queen; - } - + public String getSummary() { + if(this == PieceType.Knight) { + return "N"; + } + return this.name().substring(0, 1); + } + + public static PieceType fromSummary(char c) { + if(c=='P') { + return PieceType.Pawn; + }else if(c=='N') { + return PieceType.Knight; + }else if(c=='B') { + return PieceType.Bishop; + }else if(c=='R') { + return PieceType.Rook; + }else if(c=='K') { + return PieceType.King; + } + return PieceType.Queen; + } } diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index b7e2549..23b78f5 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -160,7 +160,6 @@ public class MyInterface extends JFrame { public void clicButtonStart() { this.instantiateSimu(); - game.setDefaultSetup(); } public void clickButtonAdder() {