Java - using logical OR in

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);

    //Console c = System.console();
    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;

    // go through each item in the list and compare against coordsEntered
    for (String s : coordsFired) {
        System.out.println("in for loop, printing iterated var" + s);

        if (s.equals(coordsEntered)) {
            // put these matched coordsFire into listHit
            results = false;
        } else {
            System.out.println("already fired at " + coordsEntered);
            results = true;
        }
    }

    return results;
}
+4
source share
2 answers

I suggest you add OOP a bit and create a class for Coords:

public class Coords {

    private final int x;
    private final int y;

    public Coords(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    /**
     * This method is used for Coords comparison
     */
    @Override
    public boolean equals(Object o) {
        Coords coords = (Coords) o;
        return y == coords.y && coords.x ==x;
    }

    /**
     * This method is used to output coords.
     */
    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
}

So, the code will look something like this:

public static Coords inputCoords(List<Coords> coordsFired) {
        Scanner sc = new Scanner(System.in);

        //Console c = System.console();
        Coords coords;

        do {
            System.out.println("in do\\while");
            System.out.println("Enter coordinates as 'x, y': ");
            String coordsEntered = sc.nextLine();
            coords = parseCoords(coordsEntered);
            System.out.println("end of do\\while loop");
        } while (coords == null || !areCoordsValid(coords) || !areCoordsNotFired(coords, coordsFired));

        coordsFired.add(coords);
        System.out.println("contents of List<String> coordsFired" + coordsFired);

        return coords;
    }

    public static boolean areCoordsValid(Coords coords) {
        boolean result = true;

        if (coords.getX() > 25 || coords.getY() > 25) { // I think you also need to validate that it is possible values
            result = false;
            System.out.println("The dimensions of the board are 25 x 25, 'x,y' entered must be less than this. " +
                    "You entered '" + coords.getX() + "' for x and '" + coords.getY() + "' for y.");
        }

        return result;
    }

    public static boolean areCoordsNotFired(Coords coords, List<Coords> firedCoards) {
        boolean result = true;
        if (firedCoards.contains(coords)) {
            result = false;
            System.out.println("You already fired at " + coords.getX() + "," + coords.getY());
        }
        return result;
    }

    public static Coords parseCoords(String coordsEntered) {
        Coords coords = null;
        try {
            String[] splittedCoords = coordsEntered.split(","); // Method splits values by comma. It should return an array of Strings with x value at the first element and y at the second one;
            if (splittedCoords.length == 2) {
                String x = splittedCoords[0].trim(); // Method removes all spaces at the beginning and ending of a passed String
                String y = splittedCoords[1].trim();
                coords = new Coords(Integer.parseInt(x), Integer.parseInt(y)); //Creates new instance of Coords class. x and y are passed as constructor params.
            } else {
                System.out.println("Format for coords is wrong.  You entered '" + coordsEntered + "'.");
            }

        } catch (NumberFormatException e) { // Integer.parseInt throws an exception if the string does not contain parsable integer.
            // We catch an exception and handle it by writing a message
            System.out.println("Coords are supposed to be numbers...  You entered '" + coordsEntered + "'.");
        }
        return coords;
    }

Set . , Set.contains() , List.contains(). Set, equals() hashCode().

+3

, .

while:

while(!validateCoords(coordsEntered) 
     || coordsFiredAt(coordsEntered, coordsFired))
+1

Source: https://habr.com/ru/post/1541824/


All Articles