-2

I created a variable called attemptCount that I am initiating at 0 and should increment every time the player tries to type a direction that is not available. I run the debugger and the variable is resetting to 0 once I attempt to type in the same direction again. The variable attemptCount is not declared locally and it is set in the game method. Any ideas why the counter is resetting instead of incrementing? I tried setting attempCount to static and tried creating a new variable that would increment attemptCount e.g. counter = attempCounter++ but none worked. I also took a look at another question that was answered but could not understand how I would apply that in this case. Can anyone shed some light on what I am doing wrong?

public class Game implements java.io.Serializable {
    private final String resourcePath;
    private final ClassLoader cl;
    private World world;
    private List<Ghost> ghosts = new ArrayList<>();
    private List<MiniGhost> miniGhosts = new ArrayList<>();
    private Map<String, List<? extends Items>> items = new HashMap<>();
    private List<Weapon> weapons;
    private final SaveGame SaveGame = new SaveGame();
    private Ghost currentGhost;
    private final Random r = new Random();
    private final String divider = "***************************************" +
            "*********************************************************";
    private Player player;
    private HauntingJFrame jFrame;
    SaveGame save = new SaveGame();
    private final transient FileReader fileReader = new FileReader();
    private MusicPlayer mp;
    private MusicPlayer soundEffect;
    private MusicPlayer walkEffect;
    private MusicPlayer keyboardEffect;
    private MusicPlayer paperFalling;
    private int guessCounter = 0;
    boolean isGameRunning = true;
    String moveGuide = "To move type: Go North, Go East, Go South, or Go West";
    String currentRoom;
    private String currentLoc;
    private boolean isSound = true;
    private int attemptCount;

    public void changeRoom(boolean isValidInput, String[] input, int attemptCount) throws
            IOException, InterruptedException {
        while (isValidInput) {
            String normalize = normalizeText(input[1]);
            try {
                if (world.getCurrentRoom().roomExits.containsKey(normalize)) {
                    player.setMostRecentExit(normalize);
                    world.setCurrentRoom(world.getCurrentRoom().roomExits.get(normalize));
                    isValidInput = false;
                    if (isSound) {
                        walkEffect.playSoundEffect();
                    }
                    Thread.sleep(1800);
                    narrateRooms(world.getCurrentRoom().getDescription(), Color.red);
                    updateCurrentRoom();
                } else {
                    replaceGameWindowWithColorText("You hit a wall. Try again.\n ", Color.RED);
                    attemptCount++;
                    System.out.println(attemptCount);
                    if (attemptCount >= 2) {
                        appendToGameWindowsWithColorNoSound("\n", Color.WHITE);
                        openMap();
                        appendToGameWindowsWithColorNoSound("Where would you like to go?\n ", Color.WHITE);
                        setAttemptCount(0);
                    }
                }
                break;
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }
  • 1
    Might I add that a while-loop with an `break;` that always gets reached inside it is a weird construct. Why use a loop at all if after the first iteration you always tell it to stop and break out of the loop? – OH GOD SPIDERS Feb 19 '21 at 17:35

2 Answers2

0

You have a parameter in changeRoom also named attemptCount. When you refer to attemptCount in the body of the method, you are referring to the parameter and not to the member variable. You can fix it by changing the name of the parameter, changing the name of the variable or by using this.attemptCount whenever you mean the member variable and not the parameter.

Sergio Feo
  • 292
  • 2
  • 7
0

"The variable attemptCount is not declared locally"

Actually it is, as method parameter:

public void changeRoom(boolean isValidInput, String[] input, int attemptCount)

so, even if incremented locally, its value will not be returned back to calling code since it is not passed as reference - see this: Is Java “pass-by-reference” or “pass-by-value”?