Actualiser src/backend/Move.java

This commit is contained in:
Guillaume VALLENET 2025-04-25 16:33:33 +02:00
parent 98e5ce6346
commit 2c09b153ac
1 changed files with 84 additions and 4 deletions

View File

@ -1,15 +1,25 @@
package backend;
/**
* This class represents a chess move with starting position,
* ending position, and the pieces involved.
*/
public class Move {
// Starting position X coordinate (0-7)
private int fromX;
// Starting position Y coordinate (0-7)
private int fromY;
// Ending position X coordinate (0-7)
private int toX;
// Ending position Y coordinate (0-7)
private int toY;
// The chess piece that is moving
private Piece movingPiece;
// The chess piece that is being captured (if any)
private Piece capturedPiece;
/**
* Constructor for a move
* Constructor for a move with all details
* @param fromX Starting X-coordinate
* @param fromY Starting Y-coordinate
* @param toX Destination X-coordinate
@ -18,47 +28,117 @@ public class Move {
* @param capturedPiece The piece being captured (null if no capture)
*/
public Move(int fromX, int fromY, int toX, int toY, Piece movingPiece, Piece capturedPiece) {
// Save the starting X position
this.fromX = fromX;
// Save the starting Y position
this.fromY = fromY;
// Save the ending X position
this.toX = toX;
// Save the ending Y position
this.toY = toY;
// Save the piece that is moving
this.movingPiece = movingPiece;
// Save the piece that is being captured (if any)
this.capturedPiece = capturedPiece;
}
// Basic constructor for now - can be expanded later
/**
* Empty constructor that creates an invalid move
* Can be used as a placeholder or default value
*/
public Move() {
// Set all positions to -1 (invalid)
this.fromX = -1;
this.fromY = -1;
this.toX = -1;
this.toY = -1;
// Set both pieces to null
this.movingPiece = null;
this.capturedPiece = null;
}
// Getters
// Getter methods to access the private fields
/**
* Get the starting X position
*/
public int getFromX() { return fromX; }
/**
* Get the starting Y position
*/
public int getFromY() { return fromY; }
/**
* Get the ending X position
*/
public int getToX() { return toX; }
/**
* Get the ending Y position
*/
public int getToY() { return toY; }
/**
* Get the piece that is moving
*/
public Piece getMovingPiece() { return movingPiece; }
/**
* Get the piece that was captured (if any)
*/
public Piece getCapturedPiece() { return capturedPiece; }
// Setters if needed
// Setter methods to modify the private fields
/**
* Set the starting X position
*/
public void setFromX(int fromX) { this.fromX = fromX; }
/**
* Set the starting Y position
*/
public void setFromY(int fromY) { this.fromY = fromY; }
/**
* Set the ending X position
*/
public void setToX(int toX) { this.toX = toX; }
/**
* Set the ending Y position
*/
public void setToY(int toY) { this.toY = toY; }
/**
* Set the piece that is moving
*/
public void setMovingPiece(Piece movingPiece) { this.movingPiece = movingPiece; }
/**
* Set the piece that was captured
*/
public void setCapturedPiece(Piece capturedPiece) { this.capturedPiece = capturedPiece; }
/**
* Convert the move to a readable string format
* For example: "Pa2-a4" for pawn moving from a2 to a4
*/
@Override
public String toString() {
// If there's no moving piece, it's an empty move
if (movingPiece == null) {
return "Empty move";
}
// Convert board coordinates to chess notation
// Example: (0,0) becomes "a8", (1,7) becomes "b1"
String from = (char)('a' + fromX) + "" + (8 - fromY);
String to = (char)('a' + toX) + "" + (8 - toY);
// Return string with piece type and positions
// Example: "Pa2-a4" for pawn move
return movingPiece.getType().getSummary() + from + "-" + to;
}
}