質問

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++;
            }
        }
    }
}
役に立ちましたか?

解決

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;
    }
}

他のヒント

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top