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

View File

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