Java For-Each Loop to Deal to multiple Hands

Posted by qwertyRocker on Stack Overflow See other posts from Stack Overflow or by qwertyRocker
Published on 2012-11-14T22:57:43Z Indexed on 2012/11/14 22:59 UTC
Read the original article Hit count: 300

Filed under:
|

I'm trying to find a good way to 'deal' cards to 4 difference hands.

         System.out.println("Deal to 4 Hands: "); 

         Hand hand1 = new Hand();
         Hand hand2 = new Hand();
         Hand hand3 = new Hand();
         Hand hand4 = new Hand();

         hand1.addSingleCard(Deck.deal());
         hand2.addSingleCard(Deck.deal());             
         hand3.addSingleCard(Deck.deal());
         hand4.addSingleCard(Deck.deal());

         hand1.addSingleCard(Deck.deal());
         hand2.addSingleCard(Deck.deal());            
         hand3.addSingleCard(Deck.deal());
         hand4.addSingleCard(Deck.deal());

         System.out.println("Cards left in deck: " + Deck.size());

         System.out.println("Player 1's Hand: \n" + hand1.getHand());
         System.out.println("Player 2's Hand: \n" + hand2.getHand());
         System.out.println("Player 3's Hand: \n" + hand3.getHand());
         System.out.println("Player 4's Hand: \n" + hand4.getHand());

Is there an easier way to deal to hands? For example using a For-Each loop?

I tried this: but it doesn't work. I haven't really used this type of loop very must...

         for(Hand card : hand1){
            System.out.println("Player 1's Hand: \n" + hand1);
         }

By the way, this deals 2 cards to 4 difference hands, then prints each hand.

© Stack Overflow or respective owner

Related posts about java

Related posts about foreach