3
\$\begingroup\$

I'm new to this community and also to Java programming! I recently picked up "Head First Java" and I did my Battleship project.

If you could kindly, give me feedback on the code?

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

    int location;

    Battleship ship = new Battleship();

    System.out.println("Ship is 3 blocks long, ocean is 10 blocks wide, choose a number: ");

    Scanner scanner = new Scanner(System.in);
    while (ship.end()) {
        location = scanner.nextInt();
        ship.shoot(location);
    }
    System.out.println("Kill!\nYou win!");
}

public static class Battleship {
    private int[] ocean;
    private int shipHead;
    private int counter;

    // check if game is over
    boolean end() {
        if (counter == 3) {
            return false;
        }
        return true;
    }

    // creates the battlefield
    Battleship() {
        ocean = new int[10];
        shipHead = (int) (Math.random() * 7);
        for (int i = shipHead; i < shipHead + 3; i++) {
            ocean[i] = 1;
        }
    }

    // find the start of the ship
    void shipStart() {
        System.out.println("Ship head is at position " + (shipHead + 1));
    }

    // cheat sheet
    void showOcean() {
        for (int i = 0; i < ocean.length; i++) {
            System.out.print(ocean[i] + " ");
        }
        System.out.println();
    }

    void shoot(int a) {
        a -= 1;
        if (a >= 0 && a < 10) {
            switch (ocean[a]) {
                case 1:
                    System.out.println("Hit!");
                    ocean[a] = -1;
                    counter++;
                    break;
                case 0:
                    System.out.println("Miss!");
                    break;
                case -1:
                    System.out.println("Was shot before!");
                    break;
            }
        } else {
            System.out.println("Number between 1 and 10!");
        }
    }

}

}

\$\endgroup\$
1
  • \$\begingroup\$ One more suggestion, when you print the ocean, sometimes a location is one square, like a space, other times it's two such as in printing -1 for hit locations making the ocean longer. I suggest using a case and printing various ASCII characters for each state instead of just a number. \$\endgroup\$
    – penguin359
    Commented Mar 13, 2023 at 4:40

1 Answer 1

4
\$\begingroup\$

Your code is clear and clean, I would only suggest:

Opposite name

// check if game is over
boolean end() {
    if (counter == 3) {
        return false;
    }
    return true;
}

I would expect this function to return true when the game ends but it does the opposite, I suggest is_ended as a name and inverting the check, (then use the function with a ! in front). Also the if is not necessary (return counter == 3 is enough).

Separation of concerns

The Battleship class should return Strings rather than printing, the main program should print. This allows easier testing and generalization to Web UI / GUI rather than console only.

Removing magic numbers

    ocean = new int[10];
    shipHead = (int) (Math.random() * 7);
    for (int i = shipHead; i < shipHead + 3; i++) {
        ocean[i] = 1;
    }

What are the numbers 10, 7 and 3? They are respectively boardSize, boardSize - shipSize, shipSize, so boardSize and shipSize should be arguments to the initialization method of Battleship to allow customization of the game to different sizes.

Use an Enum to associate numbers with states

When you write:

 ocean[i] = 1;

It does not mean that there is one thing there while there could be any number of things there, it means SHIP, just as 0 means NOTHING and -1 means DESTROYED. Enums are used when numbers represent concepts rather than quantities. Here is a basic example of enums. Using them will make the code more readable.

Comment for off-by-one errors

Off by one errors are really tricky, so I suggest adding a comment to:

a -= 1;

To:

a -= 1; // User input is 1-index while Java is 0-index
\$\endgroup\$
3
  • \$\begingroup\$ thanks for clear review! I'll fix it and bring it back \$\endgroup\$ Commented Mar 12, 2023 at 17:10
  • \$\begingroup\$ @java_grindr I am glad you liked it, I added another point on the name of the end function that left me a bit puzzled. Good luck in your Java grind! \$\endgroup\$
    – Caridorc
    Commented Mar 12, 2023 at 17:14
  • \$\begingroup\$ @java_grindr After these suggestions the code will be pretty much perfect! I suggest adding a 2d grid and more ships for your second review so we will have more feedback to give you! \$\endgroup\$
    – Caridorc
    Commented Mar 12, 2023 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.