filling in move

This commit is contained in:
ricoc 2025-04-18 15:09:50 +02:00
parent 06f94c4582
commit 32333cf768
2 changed files with 93 additions and 2 deletions

View File

@ -15,7 +15,7 @@ public class Board {
public Board(int colNum, int lineNum) {
this.width = colNum;
this.height = lineNum;
this.board = new Piece[width][height]; //first empty boar
this.board = new Piece[width][height]; //first empty board
clearConsole();
System.out.println(toString()); //print the chess at the beginning of the game
}

View File

@ -1,5 +1,96 @@
package backend;
import java.util.Optional;
/**
* Represents a chess move, including the starting and ending positions,
* the moving piece, and an optional captured piece.
*/
public class Move {
private final int startX;
private final int startY;
private final int endX;
private final int endY;
private final Piece movingPiece;
private final Optional<Piece> capturedPiece;
/**
* Constructor for a move without a captured piece.
*
* @param startX the starting x-coordinate
* @param startY the starting y-coordinate
* @param endX the ending x-coordinate
* @param endY the ending y-coordinate
* @param movingPiece the piece being moved
*/
public Move(int startX, int startY, int endX, int endY, Piece movingPiece) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.movingPiece = movingPiece;
this.capturedPiece = Optional.empty();
}
/**
* Constructor for a move involving a captured piece.
*
* @param startX the starting x-coordinate
* @param startY the starting y-coordinate
* @param endX the ending x-coordinate
* @param endY the ending y-coordinate
* @param movingPiece the piece being moved
* @param capturedPiece the piece being captured
*/
public Move(int startX, int startY, int endX, int endY, Piece movingPiece, Piece capturedPiece) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.movingPiece = movingPiece;
this.capturedPiece = Optional.of(capturedPiece);
}
public int getStartX() {
return startX;
}
public int getStartY() {
return startY;
}
public int getEndX() {
return endX;
}
public int getEndY() {
return endY;
}
public Piece getMovingPiece() {
return movingPiece;
}
public Optional<Piece> getCapturedPiece() {
return capturedPiece;
}
/**
* Returns true if this move involves a capture.
*
* @return true if a piece is captured, false otherwise
*/
public boolean isCapture() {
return capturedPiece.isPresent();
}
@Override
public String toString() {
String moveInfo = movingPiece.getType() + " from (" + startX + "," + startY + ") to (" + endX + "," + endY + ")";
if (isCapture()) {
moveInfo += " capturing " + capturedPiece.get().getType();
}
return moveInfo;
}
}