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+ ")");
                }
            }

Recommended Answers

All 6 Replies

Code tags and formatting please.

[code]

// paste code here

[/code]

It looks at first glance that you have a brackets problem. If the code to execute if an if statement is true is more than one line, you need to surround that code with brackets.

Sorry about teh formatting.
All the ifs have only one line of code. I also to be sure that was not the problem added the {} and it did not help.
Also it only prints one possible move as if not all which is stranger.
Leeba

Code tags and formatting please.

[code]

// paste code here

[/code]

It looks at first glance that you have a brackets problem. If the code to execute if an if statement is true is more than one line, you need to surround that code with brackets.

// 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+ ")");}

The formatting will look better if you paste it into a word processor or IDE. Note that lines 10, 11, 14, 15, 18, 19 always execute and are not part of an if statement. Also note the ending } on line 21 that ends your else statement.

// 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+ ")");}

The formatting will look better if you paste it into a word processor or IDE. Note that lines 10, 11, 14, 15, 18, 19 always execute and are not part of an if statement. Also note the ending } on line 21 that ends your else statement.

Sorry i am very new to this.
I know that the lines you noted are performed always. (but they are in the else so it should only be if the knight is on a legal space).

You were right about the } on row 21. I missed that. Thanks so much.
Now I need to go on to my next exercise question. YOu will probably see me again.

Sorry i am very new to this.
I know that the lines you noted are performed always. (but they are in the else so it should only be if the knight is on a legal space).

You were right about the } on row 21. I missed that. Thanks so much.
Now I need to go on to my next exercise question. YOu will probably see me again.

OK, I see the logic better now. I misread those lines. Yes, I understand now why you want them to always be executed. I wasn't sure before so I pointed them out. Does it work now?

Yes it does. thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.