-3

I've been assigned a program with arrays to build a deck of cards, and I need a program to shuffle the deck. I have no idea where to start. I considered creating a new method named shuffleDeck, but I'm not sure if it would work properly with the two arrays. Arrays are a generally new topic to me, and I'm not sure how to shuffle the deck when there are two arrays building each separate card.

No direct answers please, but some hints/help with this would be greatly appreciated.

EDIT: I tried using Collections.shuffle, and it seems that it's not building properly with Strings.

import java.util.*;
import java.io.*;

public class cards_st {


public static void main(String[] args) {
    String [] card = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    String [] suit = {" of hearts"," of clubs"," of spades", " of diamonds"};
    String [] odeck = new String[52];
    buildDeck(odeck,card,suit);
    for(int d = 0; d <52; d++)
        System.out.println(odeck[d]);


    }
    public static void buildDeck(String [] tdeck,String [] tcard, String [] tsuit)
    {
        int count = 0;
        for (int i = 0; i < 13; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                tdeck[count]= tcard[i] + tsuit[j];
                count++;
            }
        }
    }
}
ZLapierre
  • 3
  • 2
  • 2
    `Collections.shuffle` – Averroes Apr 02 '14 at 15:22
  • I don't understand why the two arrays concern you so much. You end up with a single deck of 52 after the buildDeck method. If you shuffle after that point it's as simple as handling a single array, right? – babernathy Apr 02 '14 at 15:27
  • babernathy, is the code changing the two separate arrays into the array "odeck?" If so, how do I shift the String array into something I can utilize in Collections.shuffle? – ZLapierre Apr 02 '14 at 15:33
  • Yes. Google "Cartesian product" for details. I suspect since this is a homework problem, you'll get marked down for using Collections.shuffle. But if you're interested, you'd need to turn the array into a List. Google is your friend on that one too. – babernathy Apr 02 '14 at 15:36
  • @ZLapierre I would create a Card object with a few attributes (suit, cardnumber...) and use it in Collections.shuffle – Averroes Apr 02 '14 at 15:54

2 Answers2

0

If I could suggest something, it would be better if you follow oop. You have everything in one main class. You are using strings for "constants" which is not safe. Encapsulate the right elements into objects: Card and Deck. Next Card would have enums of cards (1,2,3,4,5) and suites (heart,...). Class Deck would have array of Card objects and method to initialise by going through values of all enums and creating cards. Next use collections.shuffle to shuffle it.

public class Card {
    public enum CardType {
        ONE,
        TWO,
        THREE;
    }

    public enum Suit {
            HEARTS,
            CLUBS,
    }

    private CardType cardType;
    private Suit suit;

    constructor && getters (...)
}



public class Deck {
    List<Card> cards = new ArrayList<Card>();
    public void init() {
        for(CardType cardType : CardType.values()) {
            for(Suit suit : Suit.values()) {
                  cards.add(new Card(cardType, suit));
            }
        }
        Collections.shuffle(cards);
    }
}

You can also give String names to enum types

public enum Suit {
    HEARTS("of hearts");
    private String toString;
    public Suit(String toString) {
        this.toString = toString;
    }
    @Override
    public String toString() {
        return toString;
    }
}
Taks
  • 2,033
  • 4
  • 18
  • 23
0
String [] cards = {"H2","H3","H4","H5","H6","H7","H8","H9","H10","HJack","HQueen","HKing","HAce"
,"D2","D3","D4","D5","D6","D7","D8","D9","D10","DJack","DQueen","DKing","DAce"
,"C2","C3","C4","C5","C6","C7","C8","C9","C10","CJack","CQueen","CKing","CAce"
,"S2","S3","S4","S5","S6","S7","S8","S9","S10","SJack","SQueen","SKing","SAce"
};

List<String> deck = Arrays.asList(cards);  

System.out.println("Initial collection: "+deck);

// shuffle the list
Collections.shuffle(deck);

 System.out.println("Final collection after shuffle: "+deck);
Pat B
  • 1,915
  • 23
  • 40