0

I am trying to loop through all the players and

1> Output a line
2> Create an object

based on certain conditions. But I am getting a Stackoverflow error.

public void refuteSuggestion(Weapon w, Character c, int currentPlayerNumber) {
    for (Player p : game.getPlayers()) {
        if(currentPlayerNumber != 6) {
            if (p.getNum() == currentPlayerNumber + 1) {
                Set<Card> cardSet = new HashSet<>();
                cardSet.addAll(p.getPlayerCards());
                Card card = (Card) getRandomObject(cardSet);
                System.out.println(card.getName(0));
            } else {
                refuteSuggestion(w, c, currentPlayerNumber + 1);
            }
        }
        else{
            refuteSuggestion(w, c, 0);
        }

    }
    System.out.println("No one refuted...");
}
Captain Red
  • 1,171
  • 2
  • 16
  • 27
Nanda Ardianto
  • 347
  • 1
  • 4
  • 11
  • Please provide some context here. – Mad Physicist Aug 05 '16 at 17:58
  • refuteSuggestion is supposed to check if the Weapon and the character matches a players card in their hand. the currenPlayerNumber is the number of the currentPlayer, player 1 will be 1. for every Player in the game, if the currentPlayerNumber is not 6 (the end of the player counts (gonna change this to <= 6 instead) check if the player's number is the currentPlayerNumber + 1 (next player) if it is, do stuff, else, recurse. If the currentPlayerNumber == 6, recurse from the start. Keep checking until no players have the card. @MadPhysicist – Nanda Ardianto Aug 05 '16 at 18:03
  • 3
    you call recursively: `refuteSuggestion(w, c, 0)` which puts you in an endless loop of calls to this method until the stack overflows – Nir Alfasi Aug 05 '16 at 18:07
  • @alfasin ah yes true, didn't put a condition to stop it. my bad! thanks! – Nanda Ardianto Aug 05 '16 at 18:09
  • For future reference, edits to your question should be made as edits, not as comments. Someone reading the question should not have to read the comments to get the full story. There is an edit button under the tags. – Mad Physicist Aug 05 '16 at 18:35

1 Answers1

1

Not sure what you are trying to do here, but you are calling the method from within the same method which is an endless loop. Hence the stack overflow error.

In your Else conditions, you are only changing the value of currentPlayerNumber so just do that instead of calling the same function after changing the currentPlayerNumber.

You may also create a separate function to handle your code inside if logic if it makes easier to understand.

public void refuteSuggestion(Weapon w, Character c, int currentPlayerNumber) {
    for (Player p : game.getPlayers()) {
        if(currentPlayerNumber != 6) {
            if (p.getNum() == currentPlayerNumber + 1) {

                  nextFunction(p);
            } else {
                currentPlayerNumber += 1;
            }
        }
        else{
            currentPlayerNumber = 0;
        }

    }
    System.out.println("No one refuted...");
}

public void nextFunction(Player p){
    Set<Card> cardSet = new HashSet<>();
    cardSet.addAll(p.getPlayerCards());
    Card card = (Card) getRandomObject(cardSet);
    System.out.println(card.getName(0));
  }
sstan
  • 35,425
  • 6
  • 48
  • 66
Captain Red
  • 1,171
  • 2
  • 16
  • 27