0

I have a an array of deck of 52 cards.

I would like to have a method that I can mix the cards up following a strategic placement of the cards.

First I separate the cards into to packets like this:

    Card[] packetOne = new Card[cards.length / 2];
    Card[] packetTwo = new Card[cards.length / 2];

    for (int i = 0; i < (cards.length / 2); i++) {

        packetOne[i] = cards[i];

    }

    for (int i = 0; i < packetTwo.length; i++) {

        for (int c = cards.length / 2; c < cards.length; c++) {

            packetTwo[i] = cards[c];

        }

    }

Now that the cards are into two piles, I would like to sort the cards in a new array so that: the first card in packet one is first and after that the first card in packet two, and after that the 2nd card in packet one and after this card the 2nd card in packet two, etc,etc,etc until all cards are done.

Also if the number of cards in the deck at any time is odd, the first packet will have one more card than the second packet but the same sorting idea.

Does anyone know how to do this type of thing?

user1664285
  • 23
  • 1
  • 5

3 Answers3

2

If you are using Java 8 then there's a nice easy way to do this using streams:

Card[] combinedPack = IntStream.range(0, cards.length/2).boxed()
    .flatMap(i -> Stream.of(packetOne[i], packetTwo[i]))
    .toArray(Card[]::new);

This creates a stream of indices, maps those to new streams of the cards from each packet and then converts back to an array.

sprinter
  • 27,148
  • 6
  • 47
  • 78
1
Card[] sortedPacket = new Card[cards.length];

int i = 0;
for (int j = 0; j < cards.length/2 ;j++) {
    if (packetOne[j] != null) {
        sortedPacket[i++] = packetOne[j];
    }
    if (packetTwo[j] != null) {
        sortedPacket[i++] = packetTwo[j];
    }
}

This will cycle for each element number in packetOne/packetTwo, adding in the order you have specified

Adam S
  • 76
  • 1
  • 6
  • will this still work if there is an odd number of cards? if there is an odd number, the first packet should have one more card than the second packet – user1664285 Apr 06 '16 at 23:35
  • The second `if` statement in the `for` will ensure that for the final cards, if packetTwo is empty yet packetOne is not, no error will be thrown. – Adam S Apr 06 '16 at 23:40
0

I would use 3 indices, like this:

Card[] array3 = new Card[cards.length];
int i1 = 0;
int i2 = 0;
int i3 = 0;
while (i3 < array3.length) {
    array3[i3++] = packetOne[i1++];
    array3[i3++] = packetTwo[i2++];
}

It can be done with only one index, but by matching indices to arrays like this, and using ++ on the right index every time you read/write, it makes it much easier to see that it's correct.

Edit

If your original pack could contain an odd or even number of cards, you've got the length of packetOne wrong, because n / 2 rounds down. It's a bit more complicated, but still using the system of matching indices to arrays, the whole thing becomes:

Card[] packetOne = new Card[(cards.length + 1) / 2];
Card[] packetTwo = new Card[cards.length / 2];
int i = 0, i1 = 0, i2 = 0;
while (i1 < packetOne.length) {
    packetOne[i1++] = cards[i++];
}
while (i2 < packetTwo.length) {
    packetTwo[i2++] = cards[i++];
}
Card[] array3 = new Card[cards.length];
int i3 = i1 = i2 = 0;
while (i3 < array3.length) {
    array3[i3++] = packetOne[i1++];
    if (i3 < array3.length) {
        array3[i3++] = packetTwo[i2++];
    }
}
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116