I am new to this programming, so please bear with me.
In training, I am trying to write a game with Batteleships; not OO at the moment, but rather procedural - small steps at a time.
I have a way to read the coordinates to shoot, these are the coordinates that I want to then check to make sure they are correct. There is one method that verifies that they are numbers and in the correct range, another method "presumably" verifies what has already been entered.
The problem that I find is that I do not exit the do while loop, and the while bit uses a logical OR for the two above methods. When writing these methods, they both do what they should do, so I'm not quite sure of the method that checks if the coordinate has already been run.
Some pointers will be really appreciated (for any reason), thanks!
the code:
public static String inputCoords(List<String> coordsFired){
Scanner sc = new Scanner(System.in);
String coordsEntered;
do {
System.out.println("in do\\while");
System.out.println("Enter coordinates as 'x, y': ");
coordsEntered = sc.nextLine();
System.out.println("end of do\\while loop");
} while(!validateCoords(coordsEntered)
|| !coordsFiredAt(coordsEntered, coordsFired));
coordsFired.add(coordsEntered);
System.out.println("contents of List<String> coordsFired" + coordsFired);
return coordsEntered;
}
public static boolean validateCoords(String coordsEntered){
boolean results;
int x, y;
String strx = splitCoordsString(coordsEntered, 'x');
String stry = splitCoordsString(coordsEntered, 'y');
if (numericCheckCoordsFire(strx) && numericCheckCoordsFire(stry)) {
x = Integer.parseInt(strx);
y = Integer.parseInt(stry);
if (x > 25 || y > 25) {
results = false;
System.out.println("The dimensions of the board are 25 x 25, 'x,y' entered must be less than this. You entered '" + strx + "' for x and '" + stry + "' for y.");
} else {
results = true;
}
} else {
results = false;
System.out.println("Coords are supposed to be numbers... You entered '" + strx + "' for x and '" + stry + "' for y.");
}
System.out.println(results);
return results;
}
public static boolean coordsFiredAt(String coordsEntered, List<String> coordsFired) {
boolean results = false;
for (String s : coordsFired) {
System.out.println("in for loop, printing iterated var" + s);
if (s.equals(coordsEntered)) {
results = false;
} else {
System.out.println("already fired at " + coordsEntered);
results = true;
}
}
return results;
}
source
share