0

My program creates a deck of cards and deals them all out to 4 different hands. This is my code. It creates the 4 hands and deals the cards to each of them.

    Hand[] hands = new Hand[4];
       for(int i=0; i<hands.length; i++){
           hands[i] = new Hand();
       }
       for(int i=0; i<=Deck.size()+8; i++){
           for(Hand hand : hands){
               hand.addSingleCard(Deck.deal());
           }
       }

Now i have 4 hands, each with 13 cards, I want to iterate over the first hand, removing each card and add it to the second hand so Hand 1 has 0 cards and Hand 2 has 26. What is the best way to implement this?

Im self learning, so if you have a method thats different to what someone else has posted, i'd still love to see it :)

1
  • 1
    First, we need to know the specifics on the Hand class: how you get and remove cards. Commented Nov 16, 2012 at 23:40

2 Answers 2

2

Assuming Hand holds its cards into a Collection<Card> (i.e. a List<Card> or a Set<Card> for example):

public void transferAllCardsToOtherHand(Hand hand) {
    hand.cards.addAll(this.cards);
    this.cards.clear();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't try to remember everything. Learn to read the javadoc of the classes and interfaces you're using. That's how you will learn.
1

Assuming that the card data structure in your Hand class is an array or Collection, you can use a for-each loop.

static void transferCards (Hand from, Hand to) {
    for (Card card : from.cards) {
        to.addSingleCard(card);
    }
    from.cards.clear();
}

Feel free to replace the from.cards with whichever variable represents your cards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.