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
} }
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
– Matheus Carvalho
Show the Player class and the deck array to see if I can see your question
– Kaique Dias
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.
– Kaique Dias
That’s right, I’ll put the code in the post
– Matheus Carvalho
Ready, I edited the post
– Matheus Carvalho
edited the answer
– Kaique Dias
It worked, that’s right. I didn’t know there was such a thing as for. Thank you very much!!!
– Matheus Carvalho
it is called foreach, created from java 5, which is also the same as for(int i = 0; i < playerList.size(); i++) { }
– Kaique Dias
for nothing man, it’s always good to help
– Kaique Dias