简体   繁体   English

ArrayList 创建卡片组

[英]ArrayList creating card deck

Over here I have some code regarding a carddeck that I'm trying to make in Java.在这里,我有一些关于我正在尝试用 Java 制作的卡片组的代码。 The idea behind it is, to make 2 methods, to create a createDeck method, that returns an ArrayList that also contains 52 different cards.它背后的想法是,创建 2 个方法,创建一个 createDeck 方法,该方法返回一个 ArrayList,其中还包含 52 个不同的卡片。

And randomizing them.并将它们随机化。 so basically placing the list of cards provided as a parameter and placing them in random order.所以基本上将提供的卡片列表作为参数放置并以随机顺序放置它们。 Could it be done using "Collections.shuffle(arrayList);"?可以使用“Collections.shuffle(arrayList);”来完成吗?

Any help would be appreciated任何帮助,将不胜感激

class Card
{
    public static final String[] SUIT = new String[]{"Clubs", "Spades", "Diamonds", "Hearts"};
    public static final String[] RANK = new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

    public static ArrayList<Card> createDeck()
    {
    }

    private Integer suit;
    private Integer rank;

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

    public String toString()
    {
        return "'" + SUIT[this.suit] + " " + RANK[this.rank] + "'";
    }
}

class Main
{
    public static void main(String[] arguments)
    {
        System.out.println(new Card(2, 6));
        System.out.println(new Card(3, 12));
    }
}

Idea is to basically iterate through all possible combinations of SUIT and RANK .想法是基本上遍历SUITRANK所有可能组合。 You can do that by looping through them in nested way.您可以通过以嵌套方式循环遍历它们来做到这一点。

Make sure you add these combinations in ArrayList using ArrayList.add(Object) method.确保使用ArrayList.add(Object)方法在ArrayList添加这些组合。 This will generate your deck.这将生成您的牌组。

Now, to randomize this deck you can use Java Collections library.现在,您可以使用 Java Collections库来随机化该牌组。 Call random method with Collections.shuffle(deck) and whole deck will be shuffled.使用Collections.shuffle(deck)调用随机方法,整个甲板将被洗牌。

import java.util.ArrayList;
import java.util.Collections;

class Card {

    public static final String[] SUIT = new String[]{"Clubs", "Spades", "Diamonds", "Hearts"};
    public static final String[] RANK = new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

    public static ArrayList<Card> createDeck() {

        ArrayList<Card> deck = new ArrayList<>();
        for(int i=0; i<4; i++){
            for(int j=0; j<13; j++){
                deck.add(new Card(i, j));
            }
        }
        Collections.shuffle(deck);
        return deck;
    }
    ...
}

You could use an enum to implement is.您可以使用枚举来实现 is。 This would give you the possibility to assign values to the ranks.这将使您可以为等级分配值。 Another benefit would be, that you can easily generate a reduced deck, eg without cards 2, 3 and 4 or so.另一个好处是,您可以轻松生成减少的牌组,例如没有 2、3 和 4 左右的卡片。

I wrote a solution with internal classes, but you could pull them out as well.我用内部类写了一个解决方案,但你也可以把它们拉出来。

public class Cards {

    public static void main(String[] args) {
        Cards cards = new Cards();

        final List<Card> deck = cards.createDeck();
        Collections.shuffle(deck);

        deck.forEach(System.out::println);
    }

    private List<Card> createDeck() {
        final List<Card> deck = new ArrayList<>();
        for (Rank rank : Rank.values()) {
            for (Suit suit : Suit.values()) {
                deck.add(Card.of(suit, rank));
            }
        }
        return deck;
    }

    public enum Suit {
        Clubs,
        Spades,
        Diamonds,
        Hearts
    }

    public enum Rank {
        Ace("Ace"),
        King("King"),
        Queen("Queen"),
        Jack("Jack"),
        Ten("10"),
        Nine("9"),
        Eight("8"),
        Seven("7"),
        Six("6"),
        Five("5"),
        Four("4"),
        Three("3"),
        Two("2");

        private final String name;

        Rank(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

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

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

        public static Card of(Suit suit, Rank rank) {
            return new Card(suit, rank);
        }

        public Suit getSuit() {
            return suit;
        }

        public Rank getRank() {
            return rank;
        }

        @Override
        public String toString() {
            return rank.getName() + " of " + suit;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM