-1

I'm trying to add a play card to a list of cards for the players (player 0 and player 1). I tried to be generic and to implement the code that will be easy to change for a game of n players. But, I'm confused with the syntax, adding a new card for a specific player isn't work... Please help:

public class GameThread extends Thread{
private Socket socket = null;
private Socket[] connections = new Socket[2];
private List<List<Card>> player = new ArrayList<List<Card>>();

CardPack cardPack = new CardPack(); //creating a new card pack

public GameThread(int sessionID, Socket s) {
    socket = s;
//      Initial card for two players
    for (int i = 0; i < 2; i++){
        player.add(0, this.cardPack.getRandomCard());
        player.add(1, this.cardPack.getRandomCard());

    }
}

PS. This is a specific problem with a specific answer. Thanks Sotirios Delimanolis, Tunaki for your help of signing my question as a duplicate to question of somebody who transfers tables from SQL to ArrayLists, but why to change the reputation?

Real T H A N K S for InfernalRapture

mibo6700
  • 123
  • 1
  • 2
  • 9
  • what is your question? – CSK Jun 29 '16 at 15:13
  • It's a list> you can't just add objects to a list like that. You need to do `player[index].add(new Card())` format. Or define `List person1`, `List person2` and add them from there – Adam Jun 29 '16 at 15:16

1 Answers1

0
player.get(0).add(cardPack.getRandomCard());
player.get(1).add(cardPack.getRandomCard());

I'd recommend naming the two players for future reference, since you'll probably need to use them repeatedly.

public class GameThread extends Thread{
private Socket socket = null;
private Socket[] connections = new Socket[2];
private List<List<Card>> players = new ArrayList<List<Card>>();

CardPack cardPack = new CardPack(); //creating a new card pack

public GameThread(int sessionID, Socket s) {
    socket = s;
//      Initial card for two players
    List<Card> playerOne = new ArrayList<>();
    List<Card> playerTwo = new ArrayList<>();
    players.add(playerOne);
    players.add(playerTwo);
    playerOne.add(this.cardPack.getRandomCard());
    playerTwo.add(this.cardPack.getRandomCard());
    }
}
InfernalRapture
  • 572
  • 7
  • 19