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) {
Piece clickedPiece=board[x][y];
if(chosenPiece==null) {
//when no piece is selected
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) {
chosenPiece=clickedPiece;
// Select a piece if one is clicked
if (clickedPiece != null) {
chosenPiece = clickedPiece;
// Clear previous highlights
highlightedSquares.clear();
// Highlight the new possible moves
addPossibleMoves(chosenPiece);
}
} else {
// If a move is clicked, try to move
if (isHighlighted(x, y)) {
MovePiece movePiece = new MovePiece(chosenPiece, this);
boolean successfulMove = false;
// Call the right move method based on the piece type
switch (chosenPiece.getType()) {
case Pawn:
successfulMove = movePiece.movePawn(x, y);
break; // (break means exit the loop)
case Rook:
successfulMove = movePiece.moveRook(x, y);
break;
case Bishop:
successfulMove = movePiece.moveBishop(x, y);
break;
case Knight:
successfulMove = movePiece.moveKnight(x, y);
break;
default:
System.out.println("Unknown Piece Type"); //if the piece.getType() returns something unexpected we get this message
}
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;
if (successfulMove) {
turnNumber++;
isTurnWhite=! isTurnWhite;
} else { //invalid move
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
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
}
@ -168,9 +217,13 @@ public class Board {
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//return highlightedSquares.contains(Point(x,y));
return true
;}
for (int[] square : highlightedSquares) {
if (square[0] == x && square[1] == y) {
return true;
}
}
return false;
}
public void undoLastMove() {
//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
}
}
}
}
*/