moves for pawn and rook are done so far
This commit is contained in:
parent
4ffa671210
commit
edb712f161
|
|
@ -120,6 +120,28 @@ public class Board {
|
||||||
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
|
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Piece getPiece(int x, int y) { //returns figure and gives coordinates
|
||||||
|
if (x >= 0 && x < width && y >= 0 && y < height) { //checks if coordinates are inside the board
|
||||||
|
return board[x][y]; //returns the piece which is currently on this coordinates
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
// Update its internal coordinates
|
||||||
|
piece.setX(newX);
|
||||||
|
piece.setY(newY);
|
||||||
|
}
|
||||||
|
}
|
||||||
/* saving-loading feature :*/
|
/* saving-loading feature :*/
|
||||||
|
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
public class MovePiece {
|
||||||
|
|
||||||
|
private Piece piece;
|
||||||
|
private Board board;
|
||||||
|
|
||||||
|
public MovePiece(Piece piece, Board board) {
|
||||||
|
this.piece = piece;
|
||||||
|
this.board = board;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean movePawn(int x, int y) { //method definition for pawn
|
||||||
|
int currentX = piece.getX(); // we call the method getX to get x coordinates
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { // we go one step forward
|
||||||
|
board.movePiece(currentX, currentY, x, y);
|
||||||
|
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
|
||||||
|
board.movePiece(currentX, currentY, x, y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public boolean moveRook(int x, int y) {
|
||||||
|
int currentX = piece.getX();
|
||||||
|
int currentY = piece.getY();
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
board.movePiece(currentX, currentY, x, y);
|
||||||
|
return true; //move was successful
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false; //if not it was unsuccessful
|
||||||
|
}
|
||||||
|
private boolean isPathClear(int currentX, int currentY, int targetX, int targetY) { //method to check if the path is clear
|
||||||
|
if (currentX == targetX) { //checks if it moves vertically
|
||||||
|
int step = (targetY > currentY) ? 1 : -1; // decides the direction of the step
|
||||||
|
for (int y = currentY + step; y != targetY; y += step) { // we have a loop in here
|
||||||
|
if (board.getPiece(currentX, y) != null) {
|
||||||
|
return false; // Blocked path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (currentY == targetY) { //checks if it moves horizontally
|
||||||
|
int step = (targetX > currentX) ? 1 : -1;
|
||||||
|
for (int x = currentX + step; x != targetX; x += step) {
|
||||||
|
if (board.getPiece(x, currentY) != null) {
|
||||||
|
return false; // Blocked path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true; // No obstacles found
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue