I'm having a problem, that I'm not able to know which list an object belongs to. I have to play a trick game in java, I have the player class, and each player has a list with the cards (the card is the object), however the game has 4 players. The problem is that in order to process the play, I need to know which player the playing card belongs to. I have the deck list, which contains the cards created in the constructor, and I give them to the player inside 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
} }