package ex_cap1;
public class Ex11_KnightMoves {
public static boolean isValid(int row, int col) {
return row >= 1 && row <=8 && col <= 8 && col >= 1;
}
public static String getDestinationValue(int crtRow, int
crtCol, int rowsMoved, int colMoved) {
int destRow, destCol;
destRow = crtRow + rowsMoved;
destCol = crtCol + colMoved;
return (isValid(destRow, destCol) && isValid(crtRow,
crtCol))==true? "(" + (crtRow + rowsMoved) + "," + (crtCol +
colMoved) + ")":"false";
}
public static String getAllDestinations(int crtRow, int
crtCol) {
return getDestinationValue(crtRow, crtCol, 2, 1) + " " +
getDestinationValue(crtRow, crtCol, 2, -1) + " " +
getDestinationValue(crtRow, crtCol, -2, 1)
+ " " + getDestinationValue(crtRow, crtCol, -2, -1) + "
" + getDestinationValue(crtRow, crtCol, 1, -2) + " " +
getDestinationValue(crtRow, crtCol, 1, 2) + " " +
getDestinationValue(crtRow, crtCol, -1, 2) + " " +
getDestinationValue(crtRow, crtCol, -1, -2);
}
public static void main (String[] args) {
[Link](isValid(1, 9));
[Link](isValid(2, 4));
[Link](getDestinationValue(1, 1, -2, +1));
[Link](getDestinationValue(1, 1, +2, +1));
[Link](getAllDestinations(1, 1));
[Link](getAllDestinations(2, 4));
}