How to interlace two array lists?

Kira :

I am trying to develop a program that shuffles a deck by dividing the deck into two and then interlacing them.

Class Deck represents a deck of 52 cards. There are two methods: Deck(int n) and Card drawCard().

Deck(int n) is the constructor. The parameter tells how many rounds the deck should be shuffled. In each round of shuffling, the whole deck is first divided into two sub-decks. The sub-decks are then interlaced into one whole deck.

Some notes:

  • To simplify the discussion, we assume the cards are 1, 2, …, 10.

  • In the first round, the whole deck is divided into [1, 2, 3, 4, 5] and [6, 7, 8, 9, 10]. We then combine the two sub-decks by interlacing them to [1, 6, 2, 7, 3, 8, 4, 9, 5, 10].

  • In the second round, we again divide the whole decks into two sub-decks [1, 6, 2, 7, 3] and [8, 4, 9, 5, 10] and then combine them to [1, 8, 6, 4, 2, 9, 7, 5, 3, 10].

  • As we always put the cards in the first sub-deck before the second sub-deck, the first card and the last card of the deck remains the same no matter how many rounds we shuffle.

  • The original order of the deck is S2, S3, …, SK, SA, H2, …, HA, C2, …, CA, D2, …, DA.

Card drawCard() removes the first card in the deck and returns it. Refer to the deck after the second round in the above discussion, drawCard() returns 1 and the deck becomes [8, 6, 4, 2, 9, 7, 5, 3, 10].

My method of interlacing: Create 3 array lists wherein 2 of them (cards1 and cards2) held the cards SA - HA and C2 - DA and the other (shuffled) held the interlaced deck. I managed to implement the original deck order, however when I try to interlace, I get an out of bounds error: "Index 0 out of bounds for length 0".

Question: What am I doing wrong?

Here are my codes:

import java.util.*;

public class Deck {
    private int rounds;
    private ArrayList<Card> cards = new ArrayList<Card>();
    private ArrayList<Card> cards1 = new ArrayList<Card>();
    private ArrayList<Card> cards2 = new ArrayList<Card>();
    private ArrayList<Card> shuffled = new ArrayList<Card>();

    public Deck(int n) {
        for (Suit s : Suit.values()) {
            for (Rank r : Rank.values()) {
                cards.add(new Card(r,s));
            }
        }

        for (int x=0; x<n; x++) {
            for (int i=0; i<((cards.size())/2); i++) {
                cards1.add(cards.get(i));
                for (int j=26; j<cards.size(); j++) {
                    cards2.add(cards.get(j));
                    for (int k=0; k<cards.size(); k++) {
                        shuffled.add(k*2, cards1.get(i));
                        shuffled.add(k*2+1, cards2.get(j));
                    }
                }
            }
        }

        System.out.println(cards);
        System.out.println(cards1);
        System.out.println(cards2);
        System.out.println(shuffled);
        rounds = n;
    }

    public Card drawCard() {
        Card removed = shuffled.get(0);
        shuffled.remove(0);
        return removed;
    }
}


public class Card {
    private Rank rank;
    private Suit suit;

    public Card (Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public String toString() {
        return suit + "" + rank;
    }
}

public enum Suit {
    SPADE("S"), 
    HEART("H"), 
    CLUB("C"), 
    DIAMOND("D"); 

    private String suit;

    Suit (String s) {
      suit = s;
    }

    public String toString() {
      return suit;
    }
}
// YOU CANNOT MODIFY THIS FILE

public enum Rank {
  TWO("2"), 
  THREE("3"), 
  FOUR("4"), 
  FIVE("5"), 
  SIX("6"), 
  SEVEN("7"), 
  EIGHT("8"),
  NINE("9"), 
  TEN("10"), 
  JACK("J"), 
  QUEEN("Q"), 
  KING("K"),
  ACE("A"); 

  private String rank;

  // Constructor
  Rank (String r) {
    rank = r;
  }

  public String toString() {
    return rank;
  }
}

public class TestDeck {
  public static void main(String[] args) {

    Deck deck; 

    deck = new Deck(0);
    System.out.println("The original deck is: ");
    for (int i = 0; i < 52; i++) {
      System.out.print(deck.drawCard() + " ");
    }
    System.out.println();
    System.out.println();

    deck = new Deck(1);
    System.out.println("After shuffling once is: ");
    for (int i = 0; i < 52; i++) {
      System.out.print(deck.drawCard() + " ");
    }
    System.out.println();
    System.out.println();

    deck = new Deck(2);
    System.out.println("After shuffling twice is: ");
    for (int i = 0; i < 52; i++) {
      System.out.print(deck.drawCard() + " ");
    }
    System.out.println();
    System.out.println();
  }
}

The supposed output for class TestDeck is

The original deck is:
S2 S3 S4 ... DK DA

After shuffling once is:
S2 C2 S3 C3 ... DA

After shuffling twice is:
S2 H2 C2 D2  ... DA
xerx593 :

Ok, dear, actually you get a "index out of bounds" (god knows why... :), here is how i solved it (with comments):

public class Deck {

    //constants for 52 and 26 :
    private static final int FULL_DECK = Suit.values().length * Rank.values().length;
    private static final int HALF_DECK = FULL_DECK / 2;
    // use the constants, we need only one list (+2 temp lists, throw away
    // "shuffeld" (not needed, confusing, we use "cards" for "full deck")):
    private final ArrayList<Card> cards = new ArrayList<>(FULL_DECK);
    public Deck(int n) {

        init(); // as you had/see below

        // more overview/structure ... and we can limit n:
        for (int rounds = 0; rounds < n % 8; rounds++) {
            interlace();
        }
        // comment this, since we do output in main method...
        // System.out.println(cards);
    }

init & "interlace" methods:

    private void init() {
        for (Suit s : Suit.values()) {
            for (Rank r : Rank.values()) {
                cards.add(new Card(r, s));
            }
        }
    }

    private void interlace() {
        // throw exception, when illegal state
        assert (!cards.isEmpty());
        // left & right temp lists:
        final ArrayList<Card> left = new ArrayList<>(HALF_DECK);
        final ArrayList<Card> right = new ArrayList<>(HALF_DECK);
        // put the first half of "cards" into "left"
        left.addAll(cards.subList(0, HALF_DECK));
        // ...the rest into "right"
        right.addAll(cards.subList(HALF_DECK, FULL_DECK));
        // clear "cards"
        cards.clear();

        // iterate half deck:
        for (int i = 0; i < HALF_DECK; i++) {
            // fill cards from "left" (with "double step")
            cards.add(i * 2, left.get(i));
            // ..and from "right" (with "double step" +1;)
            cards.add(i * 2 + 1, right.get(i));
        }
        // done!
        // debug:
        // System.out.println(left);
        // System.out.println(right);
        // System.out.println(cards);
    }

the "draw" method would go like this:

    public Card drawCard() {
        assert (!cards.isEmpty());
        return cards.remove(0);
    }

And with the same main method (Suit, Rank classes), we get:

The original deck is: 
S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA 

After shuffling once is: 
S2 C2 S3 C3 S4 C4 S5 C5 S6 C6 S7 C7 S8 C8 S9 C9 S10 C10 SJ CJ SQ CQ SK CK SA CA H2 D2 H3 D3 H4 D4 H5 D5 H6 D6 H7 D7 H8 D8 H9 D9 H10 D10 HJ DJ HQ DQ HK DK HA DA 

After shuffling twice is: 
S2 H2 C2 D2 S3 H3 C3 D3 S4 H4 C4 D4 S5 H5 C5 D5 S6 H6 C6 D6 S7 H7 C7 D7 S8 H8 C8 D8 S9 H9 C9 D9 S10 H10 C10 D10 SJ HJ CJ DJ SQ HQ CQ DQ SK HK CK DK SA HA CA DA 

It's not "that" thread safe ...but for demo purpose... hope it helps! :)

..and the index out of bounds actually, because you never filled shuffeled, when n == 0 ... iobex at Main: System.out.print(deck.drawCard() + " "); (and (n == 0))

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379299&siteId=1