How to know which list an object belongs to?

Asked

Viewed 84 times

0

I’m having a problem, and I’m having trouble figuring out which list an object belongs to. I have to make a game of truce in java, I have the player class, and each player has a list of cards (the card is the object), but the game has 4 players. The problem is that to process the move I need to know which player the playing card belongs to. I have the deck list, which contains the cards created in the builder, and give them to the player within the loop.

public class Jogador {
private int id;
private String nome;
private List<LabelCarta> mao;

public Jogador() {
    mao = new ArrayList<>();
}

public void fazerJogada() {

}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public List<LabelCarta> getMao() {
    return mao;
}
public void setMao(List<LabelCarta> mao) {
    this.mao = mao;
}

Class that is running the game

I call these two methods in the constructor, after initializing the other components

private void gerarCartas() {
    for(int i = 1; i<=40;i++) {

        carta = new LabelCarta(i); //crio a carta pelo indice 

        baralho.add(carta); //adiciono ela ao baralho principal
    }

    Collections.shuffle(baralho);//embaralhar

}

private void adicionarCartas() {

    for(int player = 0; player<=4;player++) {
        jogador = new Jogador();// crio 4 jogadores pelo loop
        jogador.setNome("Jogador "+(player+1));

        for(int cartaJogador = 0; cartaJogador<=2;cartaJogador++ ) {

            jogador.getMao().add(baralho.get(cartaJogador)); //da carta ao 

            baralho.remove(cartaJogador); //depois de dar a carta ao jogador a removo do baralho principal

} }

1 answer

0


Maybe this can help you, did not know what belonged to the object Labelcarta, so I surgeri an example of attribute, if it does not work comments there that we get there.

    List<Jogador> jogadorList = new ArrayList<>();  
    private void adicionarCartas() {

        for(int player = 0; player<=4;player++) {
            jogador = new Jogador();// crio 4 jogadores pelo loop
            jogador.setNome("Jogador "+(player+1));
            for(int cartaJogador = 0; cartaJogador<=2;cartaJogador++ ) {
                jogador.getMao().add(baralho.get(cartaJogador)); //da carta ao 

                baralho.remove(cartaJogador);
            }
        jogadorList.add(jogador);
        }
    }

    private void donoDaCarta(LabelCarta labelCarta) {
        for(Jogador j:jogadorList) {
            for(LabelCarta lc: j.getMao) {
                if(labelCarta.getAlgumAtributoUnico == lc.getAlgumAtributoUnico) {
                        System.out.println("O dono da carta passada no parametro é "+j.getNome());
                    }
                }
            }
        }
  • I’m scanning the empty array in the player builder, I don’t know if you need to do it again. I’ve been able to add the cards to the player’s hand, and I’ve checked that, and you’re right. The point is that there are 4 players, each has 3 cards, and I can’t tell which player each card belongs to. I can’t think of a way to figure it out

  • Show the Player class and the deck array to see if I can see your question

  • Because apparently you’re instantiating the player class every time you create a player is this? if so, I recommend you do arrylist or a hashmap just to link the card to the player. Tell if this is what I help you with.

  • That’s right, I’ll put the code in the post

  • Ready, I edited the post

  • edited the answer

  • It worked, that’s right. I didn’t know there was such a thing as for. Thank you very much!!!

  • it is called foreach, created from java 5, which is also the same as for(int i = 0; i < playerList.size(); i++) { }

  • for nothing man, it’s always good to help

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.