Java Array Index Out of Bounds somehow?

前端 未结 3 760
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 08:49

In my game\'s code, I am trying to add a card to hand. As soon as I do, my array is out of bounds. Everything looks right, but maybe I\'m missing something.

FYI, one

3条回答
  •  一整个雨季
    2020-12-22 09:25

    I think the order of your code is incorrect (hard to tell with this code)

    for (int i = 1; i < 8; i++)
    {
     one.addCard(Card.draw(deck));
     deck = Card.getDeck();
     two.addCard(Card.draw(deck));
     deck = Card.getDeck();
    }
    

    maybe should be

    for (int i = 1; i < 8; i++)
    {
     deck = Card.getDeck();
     one.addCard(Card.draw(deck));
     deck = Card.getDeck();
     two.addCard(Card.draw(deck));
    }
    

    Update

    Also

    public void addCard(Card c) {
        hand[handsize] = c;
        handsize++;
    }
    

    handsize is never incremented - it is always 0

提交回复
热议问题