2

I have this homework assignment, where our teacher wants us to create the card game "War" in java. A huge part of it, is shuffling the 52 different cards into two players hands. I would greatly appreciate any help for when it comes to shuffling it. I am an inexperienced coder, and the other ones posted don't make any sense for me. My code:

package shuffle;

import java.util.Random;

public class SHUFFLE {

    public static void main(String[] args) {
        shuffle();
    }

    public static void shuffle() {
        Random r = new Random();
        int[] hand1 = new int[26];
        int[] hand2 = new int[26];
        int i = 1, rand, rand2;
        int o = 1;
        do {
            System.out.println("Top");
            rand = r.nextInt(52) + 1;
            rand2 = r.nextInt(2) + 1;
            System.out.println("number generated: "+rand);
            System.out.println("sector: " + rand2);
            if (rand2 == 1) {
                if (rand <= 52) {
                    while (hand1[o] > 0) {
                        if (hand1[o] == rand) {
                        } else {
                            hand1[o]--;
                        }
                    }
                    while (hand2[i] > 0) {
                        if (hand2[i] == rand) {
                        } else {
                            hand2[i]--;
                        }
                        hand1[o] = rand;
                        o++;
                    }
                }

            }

            if (rand2 == 2) {
                if (rand <= 52) {
                    while (hand1[o] > 0) {
                        if (hand1[o] == rand) {
                        } else {
                            hand1[o]--;
                        }
                    }
                    while (hand2[i] > 0) {
                        if (hand2[i] == rand) {
                        } else {
                            hand2[i]--;
                        }
                        hand2[i] = rand;
                        i++;
                    }
                }

            }

        }while(hand1[o] < 26 && hand2[i] < 26);
    }

}
hydregion
  • 69
  • 5
  • please be more specific about your question, what do you want to be answered? – Imran Ali Jun 16 '16 at 01:38
  • 1
    Just create one shuffled Deck as per http://stackoverflow.com/questions/24520782/java-shuffle-card-deck, and then split into two havles – Scary Wombat Jun 16 '16 at 01:38
  • I took a look at that, but it is way above my understanding, and I can't grasp what is going on – hydregion Jun 16 '16 at 01:43
  • @hydregion You gotta do more than just take a look at it. Run the code, study it, debug it, etc. – Michael Markidis Jun 16 '16 at 01:51
  • 1
    Use an Arraylist instead of an array. Use `Collections.shuffle`. Distribute into two sublists. Problem solved? – OneCricketeer Jun 16 '16 at 01:54
  • @cricket_007, I suspect that depends on what the homework is trying to teach. If it's just how to use Java, you're probably right. If it's how to learn how to write algorithms and code, maybe not so much :-) Not saying you're wrong since we don't have that info in the question, just putting forward another possibility. – paxdiablo Jun 17 '16 at 01:11

1 Answers1

1

Don't waste time shuffling the deck before-hand, the Fisher Yates shuffle is a far more efficient way of selecting cards at random from even a sorted deck.

You can see this answer to learn how it's done then it's basically using that algorithm to distribute the selected cards to alternate hands.

So, bottom line (pseudo-code since it's classwork), you can use the following to create two hands of twenty-six cards each (with one-based arrays):

# Set up deck of cards with initial size, and empty hands.

cards[] = [acespades, twospades, ..., queenhearts, kinghearts]
quant = size(cards)

hand1 = []
hand2 = []

# Need to distribute entire deck.

while quant > 0:
    # Get card for first hand using Fisher Yates shuffle

    index = random(1..quant)
    hand1.append(cards[index])
    cards[index] = cards[quant]
    decrement quant

    # If no cards left, exit the loop.

    if quant == 0:
        exit while

    # Now get card for second hand.

    index = random(1..quant)
    hand2.append(cards[index])
    cards[index] = cards[quant]
    decrement quant

Note the extra check in there for if your original deck has an odd number of cards (the if ... exit while bit). If you know the deck will be even, this is not needed - I've just put it in there for cases where the deck size is not known before-hand).

Also note that there is opportunity for refactoring the code that deals a card so that you don't have it repeated. I'll leave that as an educational exercise.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @hydregion It might be beneficial to explain why you are not bothering to shuffle the cards for your assignment – draksia Jun 16 '16 at 02:36
  • 1
    Great code. One thing I'd tweak though - make the if statement return or break the loop if quant is less than one instead of nesting the second hand's code - makes it more readable. – Richard Barker Jun 16 '16 at 19:52