0

I have a program that asks for a knights coordinates and prints

out all possible moves. That works. What doesn't work is if it is not a legal place for the knight (i.e. off the board) it prints an error but also prints coordinates for -1,-2. not sure why.

Any help would be appreciated.

here is the code i used:

```
/**
* The program accepts a knight's coordinates and returns all possible moves.
*
* @author (Leeba)
*/
import java.util.Scanner;
public class knight
{
public static void main()
{
Scanner k = new Scanner(System.in);
int xCoordinate, yCoordinate,newYCoordinate, newXCoordinate;
//requests coordinates of knight
System.out.println("Please enter knight's x and y coordinates.");
xCoordinate = k.nextInt();
yCoordinate = k.nextInt();
// check if x and y coordinates are legally off chessboard
if ((xCoordinate <= 0) || (xCoordinate >= 9) || (yCoordinate <= 0) || (yCoordinate >= 9))
{
System.out.println("You entered a number that is off the chess board");
}
else
{
// print all possible moves after checking if they are legal
System.out.println("The following are your possible moves");
newXCoordinate = xCoordinate+1;
newYCoordinate = yCoordinate+2;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
newXCoordinate = xCoordinate-1;
newYCoordinate = yCoordinate+2;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
newXCoordinate = xCoordinate+1;
newYCoordinate = yCoordinate-2;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");}
newXCoordinate = xCoordinate-1;
newYCoordinate = yCoordinate-2;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
newXCoordinate = xCoordinate+2;
newYCoordinate = yCoordinate+1;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
newXCoordinate = xCoordinate+2;
newYCoordinate = yCoordinate-1;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
newXCoordinate = xCoordinate-2;
newYCoordinate = yCoordinate+1;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
newXCoordinate = xCoordinate-2;
newYCoordinate = yCoordinate-1;
if ((newXCoordinate >=1) && (newXCoordinate <= 8) && (newYCoordinate<=8) && (newYCoordinate >=1))
System.out.println ("(" + newXCoordinate + "," + newYCoordinate+ ")");
}
}
```

*Edited 3 Years Ago by Nick Evan*: Fixed formatting