king and queen added. usertouch updated.

This commit is contained in:
keshi 2025-05-10 14:03:04 +02:00
parent cbb87a015e
commit 7a8ae49776
3 changed files with 90 additions and 22 deletions

View File

@ -9,6 +9,7 @@ public class Board {
private Piece chosenPiece=null;
private int turnNumber=0;
private boolean isTurnWhite=true;
private ArrayList<int[]> highlightedSquares= new ArrayList<>();
public Board(int colNum, int lineNum) {
this.width=colNum;
@ -97,7 +98,7 @@ public class Board {
Piece clickedPiece=board[x][y];
if(chosenPiece==null) {
//when no piece is selected
if(clickedPiece!=null) {
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) {
chosenPiece=clickedPiece;
}
}
@ -105,15 +106,28 @@ public class Board {
if(isSelected(x,y)) {
chosenPiece=null; //unselect the chosen piece to revert to normal state
}
else { //move selected piece to new position
board[chosenPiece.getX()][chosenPiece.getY()]=null; //clear the old position
board[x][y]=new Piece(x, y, chosenPiece.getType(), chosenPiece.isWhite());//place at new position
chosenPiece=null; //unselect the piece moved
turnNumber++; //tracks the number of moves
isTurnWhite=!isTurnWhite;
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
}
}
}
}
public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates
@ -130,10 +144,7 @@ public class Board {
public void movePiece(int oldX, int oldY, int newX, int newY) {
if (board[oldX][oldY] != null) { //checks if there is a piece at that position
Piece piece = board[oldX][oldY]; // we get the piece from the board
board[newX][newY] = piece; // we place the piece on the new position
board[oldX][oldY] = null; // we clear last position
@ -157,8 +168,7 @@ public class Board {
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//TODO
return false;
return true;
}
public void undoLastMove() {

View File

@ -16,7 +16,7 @@ public class MovePiece {
int currentY = piece.getY(); // we call the method getY to get y coordinates
boolean isWhite = piece.isWhite(); // checks if the pawn is white or black
int direction = isWhite ? -1 : 1; // we check, if its white we have -1 as it goes down if false we have black then we have 1
boolean firstMove = isWhite ? currentY == 6 : currentY == 1; // checks if its the first move
boolean firstMove = isWhite ? currentY == 6 : currentY == 1; // checks if its the first move. are the pawns still on their lines?
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { // we go one step forward
@ -24,13 +24,16 @@ public class MovePiece {
return true;
}
else if (x == currentX && y == currentY + (2 * direction) && firstMove && board.getPiece(x, y) == null) { // we go two steps forward bcs its the start of the game
else if (x == currentX && y == currentY + (2 * direction) && firstMove && board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) { // we go two steps forward bcs its the start of the game
//and we ensure that there really is nothing in between the original position and the 2nd step
board.movePiece(currentX, currentY, x, y);
return true;
}
// Note: In a full version, you should also check the square in between is empty (e.g., board.getPiece(x, currentY + direction) == null) otherwise the pawn could "jump over" a piece!
else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
board.movePiece(currentX, currentY, x, y); // we go diagonally where another figure is placed
board.movePiece(currentX, currentY, x, y); // we go diagonally and capture where another black piece is placed
return true;
}
@ -46,7 +49,6 @@ public class MovePiece {
if (currentX == x || currentY == y) // Check if the move is either in the same row or the same column
if (isPathClear(currentX, currentY, x, y)) { //Check if there are obstacles in the path
Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //Check if the target is empty or an enemy piece
@ -54,7 +56,6 @@ public class MovePiece {
return true; //move was successful
}
}
return false; //if not it was unsuccessful
}
@ -130,11 +131,41 @@ public class MovePiece {
}
return false;
}
//Queen movement logic
public boolean moveQueen(int x, int y) {
int currentX=piece.getX();
int currentY=piece.getY();
boolean Diagonal = Math.abs(currentX - x)== Math.abs(currentY-y); //diagonal spaces identified
boolean Straight= currentX == x || currentY == y; //horizontal or vertical spaces identified
if((Diagonal || Straight) && isPathClear(currentX, currentY, x, y)) { //if either space is available and clear
Piece targetPiece= board.getPiece(currentX, currentY); //choose the empty space
if(targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move piece to null space or capture opposite colour piece
board.movePiece(currentX, currentY, x, y);
return true;
}
}
return false;
}
//King movement logic
public boolean moveKing(int x, int y) {
int currentX = piece.getX();
int currentY = piece.getY();
int stepX = Math.abs(x - currentX); //spaces moved horizontally
int stepY = Math.abs(y - currentY);//spaces moved vertically
if ((stepX <= 1 && stepY <= 1) && !(stepX == 0 && stepY == 0)) { //moving 1 space in any direction
Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move is space is empty or capture
board.movePiece(currentX, currentY, x, y);
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,27 @@
/*package backend;
public class initial_Usertouch {
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
board[chosenPiece.getX()][chosenPiece.getY()]=null; //clear the old position
board[x][y]=new Piece(x, y, chosenPiece.getType(), chosenPiece.isWhite());//place at new position
chosenPiece=null; //unselect the piece moved
turnNumber++; //tracks the number of moves
isTurnWhite=!isTurnWhite;
}
}
}
}
*/