# Displaying Contents of a List

• (2 Pages)
• 1
• 2

## 20 Replies - 904 Views - Last Post: 03 May 2013 - 03:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320195&amp;s=e27967dbf860a7fd43afae94ebf0bbe1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: Displaying Contents of a List

Posted 03 May 2013 - 12:07 PM

You've got two almost-right ways of doing it bundled up into one wrong way.

either do a nested loop - deal one card to each hand, N times - or do your multiply and then take the modulo. Doing both is not correct.

```        for(int i = 0; i < N.Hands; i++)
if( i == N.Hands)
i = 0;
}

```

The usual way to take the modulo is

```i = i%numberOfHands;

```

But notice that doing it this way means your loop never terminates. Instead, you'd do

```        for(int i = 0; i < numberOfHands*cardsPerHand; i++)
}

```

Except you don't want to have that invariant calculated each time through the loop, it's wasteful. So:

```
int cardsToDeal = numberOfHands*cardsPerHand;
for(int i = 0; i < cardsToDeal ; i++)
}

```

### #17 mkiser

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 22-February 12

## Re: Displaying Contents of a List

Posted 03 May 2013 - 12:15 PM

Will your way make so that hand[i] loops through 5 players and then return to the first one so it can add its sexond card? or will it just loop till 5 players

### #18 CasiOo

• D.I.C Lover

Reputation: 1577
• Posts: 3,551
• Joined: 05-April 11

## Re: Displaying Contents of a List

Posted 03 May 2013 - 12:18 PM

What is the reason for dealing the 1st card to player1, 2nd card to player2 etc. ? You are creating an application, it is not like in real life
They won't be able to see if you deal the first or second card from the deck
..Unless you are ordering the cards in some special way of course

I still think you should add a Deck class
Make it simple, not more complex />
```public void dealCards(Deck deck, Player[] players, int cardsPerPlayer) {
for (Player player : players) {
Card[] cards = deck.take(cardsPerPlayer);
player.dealCards(cards);
}
}

```

Following your weird dealing 1st to player1 bla bla, I would still do like in my first post
```public void dealCards(Deck deck, Player[] players, int cardsPerPlayer) {
int playerIndex = 0;
for (int i=0; i<cardsPerPlayer*players.length; i++) {
Card card = deck.pull();
players[playerIndex].dealCard(card);
playerIndex = (playerIndex + 1) % players.length
}
}

```

Edit: lets just add more weird solutions cause why not
```public void dealCards(Deck deck, Player[] players, int cardsPerPlayer) {
Card[] cards = deck.take(cardsPerPlayer * players.length);

for (int i=0; i<cards.length; i++)
players[i % players.length].dealCard(cards[i]);
}

```

This post has been edited by CasiOo: 03 May 2013 - 12:30 PM

### #19 mkiser

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 22-February 12

## Re: Displaying Contents of a List

Posted 03 May 2013 - 12:20 PM

the deck must stay in order. so it has to be like real life dealing.

### #20 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: Displaying Contents of a List

Posted 03 May 2013 - 12:24 PM

mkiser, on 03 May 2013 - 02:15 PM, said:

Will your way make so that hand[i] loops through 5 players and then return to the first one so it can add its sexond card? or will it just loop till 5 players

You should be able to satisfy yourself on that point. Examine the code and see what you think it does, step by step.

### #21 mkiser

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 22-February 12

## Re: Displaying Contents of a List

Posted 03 May 2013 - 03:13 PM

Thanks a lot evrybody. I will test out what I got from you guys later tonight.