highlights and usertouch in progress

This commit is contained in:
mariettakazimierczak 2025-05-10 20:00:48 +02:00
parent 3ed3826df5
commit 859e743922
2 changed files with 127 additions and 35 deletions

View File

@ -96,40 +96,89 @@ public class Board {
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
Piece clickedPiece=board[x][y]; Piece clickedPiece=board[x][y];
if(chosenPiece==null) { if(chosenPiece==null) {
//when no piece is selected // Select a piece if one is clicked
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { if (clickedPiece != null) {
chosenPiece=clickedPiece; chosenPiece = clickedPiece;
} // Clear previous highlights
} highlightedSquares.clear();
else { //if a piece is already selected // Highlight the new possible moves
if(isSelected(x,y)) { addPossibleMoves(chosenPiece);
chosenPiece=null; //unselect the chosen piece to revert to normal state }
}
else { //move selected piece to new position using MovePiece logics } else {
MovePiece movement= new MovePiece(chosenPiece, this); // If a move is clicked, try to move
boolean movementSuccess=false; if (isHighlighted(x, y)) {
if(chosenPiece.getType()==PieceType.Pawn) { MovePiece movePiece = new MovePiece(chosenPiece, this);
movementSuccess=movement.movePawn(x,y); boolean successfulMove = false;
} else if (chosenPiece.getType()==PieceType.Rook) {
movementSuccess=movement.moveRook(x, y); // Call the right move method based on the piece type
}else if (chosenPiece.getType()==PieceType.Bishop) { switch (chosenPiece.getType()) {
movementSuccess=movement.moveBishop(x, y); case Pawn:
}else if (chosenPiece.getType()==PieceType.King) { successfulMove = movePiece.movePawn(x, y);
movementSuccess=movement.moveKing(x, y); break; // (break means exit the loop)
}else if (chosenPiece.getType()==PieceType.Queen) { case Rook:
movementSuccess=movement.moveQueen(x, y); successfulMove = movePiece.moveRook(x, y);
} break;
if(movementSuccess) { case Bishop:
chosenPiece=null; successfulMove = movePiece.moveBishop(x, y);
turnNumber++; break;
isTurnWhite=! isTurnWhite; case Knight:
} else { //invalid move successfulMove = movePiece.moveKnight(x, y);
} break;
} default:
System.out.println("Unknown Piece Type"); //if the piece.getType() returns something unexpected we get this message
}
if (successfulMove) {
turnNumber++;
isTurnWhite = !isTurnWhite;
}
}
// Unselect the piece and clear highlights
chosenPiece = null;
highlightedSquares.clear();
}
repaint();
} }
private void addPossibleMoves(Piece piece) {
MovePiece movePiece = new MovePiece(piece, this);
int currentX = piece.getX();
int currentY = piece.getY();
// Loop through the board to check possible moves
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
boolean canMove;
switch (piece.getType()) {
case Pawn:
movePiece.movePawn(x, y);
case Rook:
movePiece.moveRook(x, y);
case Bishop:
movePiece.moveBishop(x, y);
case Knight:
movePiece.moveKnight(x, y);
default:
canMove = false;
System.out.println("Unknown Piece Type");
};
if (canMove) {
highlightedSquares.add(new int[]{x, y});
}
}
}
} }
public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y; return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
} }
@ -168,9 +217,13 @@ public class Board {
/* The following methods require more work ! */ /* The following methods require more work ! */
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
//return highlightedSquares.contains(Point(x,y)); for (int[] square : highlightedSquares) {
return true if (square[0] == x && square[1] == y) {
;} return true;
}
}
return false;
}
public void undoLastMove() { public void undoLastMove() {
//TODO //TODO

View File

@ -25,3 +25,42 @@ public class initial_Usertouch {
} }
} }
*/ */
//Keshini's user touch
/*
public void userTouch(int x, int y) {
Piece clickedPiece=board[x][y];
if(chosenPiece==null) {
//when no piece is selected
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) {
chosenPiece=clickedPiece;
}
}
else { //if a piece is already selected
if(isSelected(x,y)) {
chosenPiece=null; //unselect the chosen piece to revert to normal state
}
else { //move selected piece to new position using MovePiece logics
MovePiece movement= new MovePiece(chosenPiece, this);
boolean movementSuccess=false;
if(chosenPiece.getType()==PieceType.Pawn) {
movementSuccess=movement.movePawn(x,y);
} else if (chosenPiece.getType()==PieceType.Rook) {
movementSuccess=movement.moveRook(x, y);
}else if (chosenPiece.getType()==PieceType.Bishop) {
movementSuccess=movement.moveBishop(x, y);
}else if (chosenPiece.getType()==PieceType.King) {
movementSuccess=movement.moveKing(x, y);
}else if (chosenPiece.getType()==PieceType.Queen) {
movementSuccess=movement.moveQueen(x, y);
}
if(movementSuccess) {
chosenPiece=null;
turnNumber++;
isTurnWhite=! isTurnWhite;
} else { //invalid move
}
}
}
}
*/