🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Cardgame: Print output correct first time but…print output second time is a bug

Started by
4 comments, last by Alberth 3 years, 2 months ago
Code is in Java:

int NUM_OF_DRAWS = 6;
        
        int[] x = {256,384, 512, 640, 768, 896};

        for(int i = 0; i < NUM_OF_DRAWS;i++)
        {
            playerHand.get(i).getPosition().setX(x[i]);

            playerHand.add(playerDeck.get(i));
            
            
            System.out.println(playerHand.get(i).getPosition().getX() + " fine");

        
        }
        
        for(int i = 0; i < playerHand.size();i++)
        {
            System.out.println(playerHand.get(i).getPosition().getX() + " bug");
            
        }

I can't seem to figure out why I print x coordinates the first time, the x coordinates are assigned correctly. But as soon as I break out of the for loop and print it again using a for loop , the x coordinates of all the cards corresponds to the last element of the array.

output:
256.0 fine
384.0 fine
512.0 fine
640.0 fine
768.0 fine
896.0 fine
896.0 bug
896.0 bug
896.0 bug
896.0 bug
896.0 bug
896.0 bug
Advertisement

My first guess is that all of the elements of playerHand are referring to the same object, or all of the elements have the same position object. How are you initialising playerHand and playerDeck?

RulerOfNothing said:

My first guess is that all of the elements of playerHand are referring to the same object, or all of the elements have the same position object. How are you initialising playerHand and playerDeck?

private Stack<Sprite> playerDeck;

private Stack<Sprite> playerHand;

----

playerHand = new Stack<Sprite>();

playerDeck = new Stack<Sprite>();

--

public class Sprite {

private Vector2D position;

public Sprite(Vector2D position)

{

this.position = position;

}

public Vector2D getPosition()

{

return position;

}

public void setPosition(Vector2D position)

{

this.position = position;

}

}

Then your code as written doesn't work, since Stacks start off empty and calling get on an empty Stack will result in an ArrayIndexOutOfBoundsException exception. So how are you filling playerHand and playerDeck?

Stack has been deprecated in favor of Deque ( https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html​ ), so consider using the better class instead.

superherocat said:

playerHand.add(playerDeck.get(i));

There is indeed the mystery of not crashing on an empty stack, although you only need one “add”, as the line above copies a link from the last used Sprite to be used in the next iteration. Therefore the entire Stack instance is filled with links to the same object.

Remember, “new" makes new objects, any other assignment with objects just adds another link to the already existing source object.

This topic is closed to new replies.

Advertisement