part 1 done+added methods in Piece+Board

This commit is contained in:
Valentine GIRAL 2025-05-06 19:01:25 +02:00
parent 0ef1139f6a
commit b720024c15
2 changed files with 52 additions and 36 deletions

View File

@ -81,6 +81,7 @@ public class Board {
board[y][x] = null; // Remove the piece board[y][x] = null; // Remove the piece
} }
} }
this.turnNumber=0;
System.out.println("Board cleaned. All pieces removed."); System.out.println("Board cleaned. All pieces removed.");
} }
@ -123,48 +124,55 @@ public class Board {
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
//TODO //TODO
// First Touch: No position is selected, and a piece is at the position x,y // Case 1: No position is selected
if (this.selectedPosition==null && getBoardChar(x,y)!='.') { if (selectedPosition==null) {
// Select the position at x, y and store it as an array Piece clickedPiece = board[y][x];
this.selectedPosition[0] = x; // Check if there is a piece at clicked position and it belongs to current player
this.selectedPosition[1] = y; if (clickedPiece!=null && clickedPiece.isWhite()==turnWhite){
// Select this position
selectedPosition = new int[] {x,y};
}
} }
// Second Touch: A piece is already selected // Case 2: A position is already selected
else if (this.isSelected(x,y)){ else{
// If the selected position is clicked again, deselect it int selectedX = selectedPosition[0];
this.selectedPosition=null; int selectedY = selectedPosition[1];
}
// If the selected position is clicked again, deselect it
// If a piece is selected and the user clicks a new position if(selectedX == x && selectedY == y) {
else if (this.selectedPosition!=null){ selectedPosition = null;
// Move the piece from the selected position to x, y }
int fromX = this.selectedPosition[0];
int fromY = this.selectedPosition[1];
// Move the piece from (fromX, fromY) to (x, y)
this.movePiece(fromX, fromY, x, y);
// Update turn // If a piece is selected and the user clicks a new position
this.turnNumber++; else {
this.turnWhite=!this.turnWhite; movePiece(selectedX,selectedY,x,y);
// Update turn
// Deselect the position after moving this.turnNumber++;
this.selectedPosition = null; this.turnWhite=!this.turnWhite;
}
} // Deselect the position after moving
this.selectedPosition = null;
}
}
}
public void movePiece(int fromX, int fromY, int toX, int toY) { public void movePiece(int fromX, int fromY, int toX, int toY) {
// Move the piece on the board // Get the piece at the source position
this.board[toX][toY] = this.board[fromX][fromY]; // Place piece at the new position Piece piece = board[fromY][fromX];
this.board[fromX][fromY] = null; // Clear the old position
} if (piece != null) {
// Update the piece's internal position
private char getBoardChar(int x, int y) { piece.setX(toX);
int index = y * 8 + x; piece.setY(toY);
return boardString.charAt(index);
// Move the piece on the board
board[toY][toX] = piece; // Place piece at the new position
board[fromY][fromX] = null; // Clear the old position
} }
}
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y; return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;

View File

@ -22,6 +22,14 @@ public class Piece {
return color; return color;
} }
public void setX(int x) {
this.X=x;
}
public void setY(int y) {
this.Y=y;
}
// constructor // constructor
public Piece (boolean colorP, PieceType typeP, int xP, int yP) { public Piece (boolean colorP, PieceType typeP, int xP, int yP) {
this.color=colorP; this.color=colorP;