I am trying to write a program in Java where i have some commands written in a text file, on the basis of these commands a horse will move in the same direction or changing its angle by 10 degree. Horse starting position is (0,0) and is facing east The destination is represented as a square centered on (500,0) coordinate. The square extends 30 meters north, south, east, and west of the (500,0) coordinate:
This is the command.txt file:
Go
Hah
Go
Gaff
Go
Gaff
Go
Go
Whoa
Command Meaning :
Go
Move forward 50 meters in the direction you are facing.
Hah
Turn left 10 degrees, but do not move forward.
Gaff
Turn right 10 degrees, but do not move forward.
Whoa
Stop.
Here is the code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HorseRiding {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the path of the file");
Scanner sc = new Scanner(System.in);
String filename = sc.nextLine();
horse(filename);
}
public static void horse(String filename) {
File inputFile = new File(filename);
Scanner readFile = null;
try {
readFile = new Scanner(inputFile);
} catch (FileNotFoundException e) {
System.out.println("Error! File not found!");
}
double x=0, y=0, angle=0, distance=50, x2=0, y2=0;
System.out.println("The horse starts at: (" + x + "," + y + ")");
while(readFile.hasNextLine()){
String line = readFile.nextLine();
while(line.equals("Go"))
{
x += distance + x2;
y += y2;
System.out.println("Command: "+line+", New position: ("+x+", "+y+")");
line = readFile.nextLine();
}
while(line.equals("Hah"))
{
angle -= 10;
x2 = distance * Math.cos(angle);
y2 = distance * Math.sin(angle);
System.out.println("Command: "+line+", New position: ("+x+", "+y+")");
line = readFile.nextLine();
}
while(line.equals("Gaff"))
{
angle+=10;
x2 = distance * Math.cos(angle);
y2 = distance * Math.sin(angle);
System.out.println("Command: "+line+", New position: ("+x+", "+y+")");
line = readFile.nextLine();
}
while(line.equals("Whoa"))
{
System.out.println("Final Position of Horse: ("+x+", "+y+")");
if((x<530.00 && x>470) && (y>-30.00 && y<30.00)) {
System.out.println("SUCCESS");
break;
}
else {
System.out.println("FAILURE");
break;
}
}
readFile.close();
}
}
}
I am getting an exception error:
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150)
at java.base/java.util.Scanner.findWithinHorizon(Scanner.java:1781)
at java.base/java.util.Scanner.hasNextLine(Scanner.java:1610)
at HorseRiding.horse(HorseRiding.java:40)
at HorseRiding.main(HorseRiding.java:14)
moreover, it runs twice, till the second command Hah and then throws the above exception.
Please review the following snippet, here some other issues are addressed:
x2
, y2
because Math.cos
, Math.sin
accept angle in radians, not degrees.angle
.import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HorseRiding {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Enter the path of the file");
Scanner sc = new Scanner(System.in);
String filename = sc.nextLine();
horse(filename);
//horse("horse.txt");
}
private static void horse(String filename) throws FileNotFoundException {
File inputFile = new File(filename);
Scanner readFile = new Scanner(inputFile);
final double distance = 50, successX = 500, successY = 0, range = 30;
double x = 0, y = 0, angle = 0, x2 = distance, y2 = 0;
System.out.println("The horse starts at: (" + x + ", " + y + ")");
while (readFile.hasNext()) {
String command = readFile.next();
if ("Whoa".equals(command)) {
break;
}
if ("Go".equals(command)) {
x += x2;
y += y2;
} else if ("Hah".equals(command) || "Gaff".equals(command)) {
angle += "Hah".equals(command) ? 10 : -10;
x2 = distance * Math.cos(Math.toRadians(angle));
y2 = distance * Math.sin(Math.toRadians(angle));
}
//System.out.println("Command: " + command + ", New position: (" + x + ", " + y + "), angle: " + angle);
System.out.printf("Command: %-4s\tNew position: (%6.2f, %6.2f)\tangle: % 3.0f%n",
command, x, y, angle);
}
if (x >= successX - range && x <= successX + range &&
y >= successY - range && y <= successY + range) {
System.out.println("SUCCESS");
} else {
System.out.println("FAILURE");
}
readFile.close();
}
}
Resulting output for the given sample file is:
Enter the path of the file
horse.txt
The horse starts at: (0.0, 0.0)
Command: Go New position: ( 50.00, 0.00) angle: 0
Command: Hah New position: ( 50.00, 0.00) angle: 10
Command: Go New position: ( 99.24, 8.68) angle: 10
Command: Gaff New position: ( 99.24, 8.68) angle: 0
Command: Go New position: (149.24, 8.68) angle: 0
Command: Gaff New position: (149.24, 8.68) angle: -10
Command: Go New position: (198.48, 0.00) angle: -10
Command: Go New position: (247.72, -8.68) angle: -10
FAILURE