Compare commits

...

2 Commits

Author SHA1 Message Date
PIRANUT_PHLANG 2e04e59529 Merge branch 'master' of
https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
2025-05-07 15:36:40 +02:00
PIRANUT_PHLANG d77aa36fd4 move 2025-05-07 15:30:34 +02:00
2 changed files with 68 additions and 0 deletions

View File

@ -79,6 +79,10 @@ public class Board {
public void cleanBoard() {
pieces.clear();
turnNumber = 0;
turnWhite = true;
selected = null;
highlighted.clear();
//TODO
}
@ -118,6 +122,62 @@ public class Board {
}
public void userTouch(int x, int y) {
// If no position has been previously selected
if (selected == null) {
// Check if there is a piece at the selected coordinates
for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y) {
// If a piece is found, select the position
selected = new int[]{x, y};
return;
}
}
} else {
// If a position is already selected
if (selected[0] == x && selected[1] == y) {
// If the selected position is the same as the current position, unselect it
selected = null;
} else {
// Otherwise, move the piece from the selected position to the new position
Piece selectedPiece = null;
Piece capturedPiece = null;
for (Piece piece : pieces) {
if (piece.getX() == selected[0] && piece.getY() == selected[1]) {
selectedPiece = piece;
break;
}
}
for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y && piece != selectedPiece) {
capturedPiece = piece; // Capture the piece
pieces.remove(piece); // Remove the captured piece from the board
break;
}
}
if (selectedPiece != null) {
// Move the piece to the new position
selectedPiece.setX(x);
selectedPiece.setY(y);
// Increment the turn number and switch turns
turnNumber++;
turnWhite = !turnWhite;
// Optionally, add the move to moveHistory if you'd like to track moves
moveHistory.add(new Move(selectedPiece, selected[0], selected[1], x, y, capturedPiece));
}
// After the move, unselect the piece
selected = null;
}
}
//TODO
Piece clickedPiece = getPieceAt(x, y);
if (selected == null) {
@ -145,6 +205,7 @@ public class Board {
}
}
}
}

View File

@ -14,10 +14,16 @@ public class Piece {
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public PieceType getType() {
return type;
@ -31,4 +37,5 @@ public class Piece {
this.x=x;
this.y=y;
}
}