Hi, im pretty new to programming, and I am struggling with my current project.
This class is supposed to imitate a Queen piece from a chess board. I take its position (int row, int column ) and I must make an isValid method that will return whether or not the piece can move from its current position to a new given position that is brought into the isValid method through its parameters. What I have so far covers the rows and columns, but I can not figure out how to validate the diagonals.
Here is the code:
public class Queen {
// declare needed variables
private int queenRow;
private int queenColumn;
private String name = "Queen";
private boolean valid;
public Queen(int queenRow, int queenColumn, String name) {
this.queenRow = queenRow;
this.queenColumn = queenColumn;
this.name = name;
}
// method to set the Queen's position
public void setQueenPos(int queenRow, int queenColumn) {
this.queenRow = queenRow;
this.queenColumn = queenColumn;
}
public void moveQueen(int i, int j) {
if (valid == true) {
this.queenRow = i;
this.queenColumn = j;
} else {
System.out.println("The queen cannot move there.");
}
}
// method to validate the piece's move
public boolean isValid(int row, int column) {
if (row == this.queenRow && column <= 8 && column > 0) {
valid = true;
} else if (column == this.queenColumn && row <= 8 && row > 0) {
valid = true;
}
return valid;
}
// method to return the Queen's position
public int getQueenRow() {
return queenRow;
}
public int getQueenColumn() {
return queenColumn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Queen obj = new Queen(1, 1, "Queen");
System.out.println(obj.getQueenRow() + " " + obj.getQueenColumn());
obj.isValid(1, 5);
obj.moveQueen(1, 5);
System.out.println(obj.getQueenRow() + " " + obj.getQueenColumn());
}
}