1

I'm trying to program a bug to move around an array attached to a custom Room object, whilst keeping count of how many times each tile has been stepped on.

The Room object is working properly, as are the movement and the counting. However, the bug's coordinates, bugX and bugY, are somehow reverting to 0 after exiting the nextMove method. Their values only revert when exiting the method; even the last line of code in the nextMove method itself uses their new values.

Relevant portion of the method is attached, but other sections can be added upon request.

if (dirNum == 0 && bugY < length-1)         //Move up
    bugY++;
else if (dirNum == 1 && bugX < width-1)     //Move right
    bugX++;
else if (dirNum == 2 && bugY > 0)           //Move down
    bugY--;
else if (dirNum == 3 && bugX > 0)           //Move left
    bugX--;
else {
    System.out.println("Error: Cannot move " + direction + ".");
    canMove = false;
    dirNum = generator.nextInt(4);
    continue;
}

This is the context for the command itself.

while (endSim == false) {
    nextMove(bugX, bugY);
    System.out.print(room.printRoom() + "\n\nNext move? (y/n) ");
    simSentinel = in.next();
    if (simSentinel.charAt(0) == 'n')
        endSim = true;
}

The declarations where the starting coordinates are assigned aren't inside any loops, let alone where the variable itself is called.

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
H. Davis
  • 55
  • 1
  • 5
  • 2
    Are `bugX` and `bugY` in `nextMove` the names of the parameters? Then of course the changes inside that method won't be visible outside of it, they are passed by value – UnholySheep Sep 09 '18 at 16:16
  • 2
    I think the question is wrongly tagged. The code seems to be java (System.out. print()), but the question is tagged as javascript. – Juan Sep 09 '18 at 16:26
  • Related: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – T.J. Crowder Sep 09 '18 at 17:29

2 Answers2

1

The problem is the one described by @T.J.Crowder in his answer though applied to java.

Variables passed as parameters in java are passed by value. If the value is changed by the method receiving the parameter, the change only affects the value inside that method. The "outside" value doesn't change.

What you can do is to encapsulate the coords in an object and pass the encapsulating object as a parameter. Then the method will receive the object by value, and change it's state (instead of the value of the object).

For a deeper understanding see this question

EDIT I:

I cleand up the code a bit. Though it is is missing the declaration of room and simSentinel, if you add that you should have a running example.

public class Bug{
    public int x=0;
    public int y=0; 
}

    public class SimpleSim {

    private int  dirNum = 0;
    private int length = 20;
    private int width = 20;
    private boolean canMove = true;
    private Random generator = new Random();
    private boolean endSim = false;

    public static void main(String [] args) {
        SimpleSim simpleSim = new SimpleSim();
        simpleSim.start();

    }

    private void start() {
        Bug myBug = new Bug();
        // Give the bug some initial x, y values.
        myBug.x = 0;
        myBug.y = 0;

        while (endSim == false) {
            nextMove(myBug);
            System.out.print(room.printRoom() + "\n\nNext move? (y/n) ");
            simSentinel = in.next();
            if (simSentinel.charAt(0) == 'n')
               endSim = true;
            }

        }
    }

    public void nextMove(Bug bug){
        if (dirNum == 0 && bug.y < length-1)         //Move up
           bug.y++;
        else if (dirNum == 1 && bug.x < width-1)     //Move right
           bug.x++;
        else if (dirNum == 2 && bug.y > 0)           //Move down
           bug.y--;
        else if (dirNum == 3 && bug.x > 0)           //Move left
           bug.x--;
        else {
           System.out.println("Error: Cannot move " + "?" + ".");
           canMove = false;
           dirNum = generator.nextInt(4);
        }

     }

}
Juan
  • 5,525
  • 2
  • 15
  • 26
  • In my defense, the question was tagged [tag:javascript] (it's since been fixed). I just, erm, didn't notice the `System.out.println` call... :-) (Edit: Oh, it was you who noticed it!) – T.J. Crowder Sep 09 '18 at 17:14
  • @T.J.Crowder Yes, I had referenced your answer because the problem is the one you had described. Parameters passed by value. – Juan Sep 09 '18 at 17:17
  • Your explanation helped immensely; it took some effort, but I got it to work. Made Bug a new custom object with its coordinates and set the nextMove to return that Bug object after changing its coordinates. – H. Davis Sep 10 '18 at 13:30
0

It seems that you are passing your bugX and bugY parameters by value. In this case, changing their value inside the method won't affect their values outside the method. You may want to make your nextMove method return the new values for bugX and bugY after they are computed so that you can gather them back into your actual bugX and bugY variables