一副扑克牌,方法难度和一副扑克牌类

A pack of playing cards, difficulty in methods and the Pack and PlayingCard Classes

我必须开发一个名为 Pack 的 class,它需要能够生成一副由 SuitRank 排序的 52 张扑克牌。它还需要能够用另一种方法洗牌。最后,它必须 return String 代表那副牌。

尽管如此,我必须以某种方式使用我已经完成的另一个 class,PlayingCard,以及这个 Pack class。这是当我 运行 陷入死胡同时,我想不出在我使用的数组中洗牌的方法,并且由于不兼容问题我无法使用 PlayingCard

这是当前包 Class:

import java.util.Random;

public class Pack {
    int[] cards = new int[52];

    public Pack() {
        // Setting up array
        String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
        String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};

        // Intialising array
        {    
            for (int i = 0; i < cards.length; i++) {
                cards[i] = i;
            }
        }
    }

    public void shuffle() {
        for (int i = 0; i < 52; i++) {
            Random random = new Random();
            int rand = random.nextInt(50)+1;
            cards[i] = rand;
        }
    }

    @Override
    public String toString() {
        return getClass().getName() + "[suit[]= " + suit[] + "rank[]= " + rank[] + "]";
    }
}

这是另一个基于建议的包 Class,注意缺少 toString():

import java.util.Random;

public class Pack {
    PlayingCard[] cards = new PlayingCard[52];

    public Pack() {
        // Setting up array
        String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
        String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};

        // Initialising array
        for (int i = 0; i < cards.length; i++) {
            cards[i] = i;
        }   
    }

    public void shuffle() {
        for (int i = 0; i < 52; i++) {
            Random random = new Random();
            int rand = random.nextInt(50) + 1;
            cards[i] = rand;   // Error 3
        }
    }

    @Override
    public String toString() {

这是我一直在使用的模板,因为它所做的一切都很好:

Using the class PlayingCard develop another class called Pack. 
An outline for this class is provided below:
public class Pack
{
PlayingCard[] cards = new PlayingCard[];
/**
* Constructs a pack of 52 cards.
* Sorted by suit Clubs, Diamonds, Hearts, Spades.
* Sorted ascending.
*/
public Pack()
{
}
/**
* Shuffles cards in pack.
*/
public void shuffle()
{
}
/**
* @return string representation of 52 card pack.
*/
public String toString()
{
}
}

最后,这是 Pack 必须以某种方式使用的 PlayingCard class。

import java.util.Objects;

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

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

    public Rank getRank() {   
        System.out.println(rank);
        return rank;
    }

    public Suit getSuit() {
        System.out.println(suit);
        return suit;
    }

    public String toString() {
        return getClass().getName() + "[rank= " + rank + "suit= " + suit + "]";
    }

    public void format() {
        System.out.format(rank + " OF " + suit);
        System.out.println();
    }

    @Override
    public boolean equals(Object otherObject) {
        if (otherObject == null) {
            return false;
        }

        if (!(otherObject instanceof PlayingCard)) {
            return false;
        }

        if (this == otherObject) {
            return true;
        }

        PlayingCard other = (PlayingCard) otherObject;
        return suit.equals(other.suit) && rank == other.rank;
    }

    @Override
    public int hashCode() {
        return Objects.hash(suit, rank);
    }
}

那么,我如何将 PlayingCard class 与 Pack 一起使用,以及如何让 Pack 中的洗牌 class 与我的数组等一起运行,因为它将在时刻是将随机数放入卡片数组。

如果需要更多信息,请告诉我,我会添加。

编辑 1:

为了回应第一个答案,这里是用于显示潜在问题的花色枚举,以及我在回答之前所做的当前版本的 Pack。它是如何坚持的?

枚举:

public enum Suit 
{
    SPADES(-2), CLUBS(-1), HEARTS(0), DIAMONDS(1);

    private int value;

    private Suit(int value)
    {
        this.value = value;
    }

    public int getValue()
    {
        return value;
    }
}

更新包:

import java.util.Random;

public class Pack
{
    int[] cards = new int[52];

    public void Deck()
    {
        // Setting up array
        String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
        String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", 
            "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};

        // Intialising array
        {    
            for (int i = 0; i < cards.length; i++)
            {
                cards[i] = i;
            }
        }
   }

   public void shuffle()
   {
        for (int i = 0; i < 52; i++)
        {
            Random random = new Random();
            int rand = random.nextInt(50)+1;
            int temp = cards[i];
            cards[i] = cards[rand];
            cards[rand] = temp;
        }
    }

    @Override
    public String toString()
    {
        return 
    }
}

注意 RankSuitPlayingCard class 中。提示定义 2 个枚举 classes (show only Suit class) :

public enum Suit {
   SPADE("Spade"),
   HEART("Heart"),
   DIAMOND("Diamond"),
   CLUB("Club");

   private String name;
   private Suit(String name) { this.name = name; } 

   public String toString() {return name;}
}

您可以将这 2 个枚举声明为 public static enum {...}Pack class 中。在这种情况下使用枚举的原因是我们提前知道 all 卡片,这比处理强类型对象比仅 IntegersString 更安全和西装。

然后您可以在 SuitRank 值上使用双循环初始化 Pack 卡片,例如

public Pack() {
   int i = 0;
   for (Rank r : Rank.values()) {
      for (Suit s : Suit.values()) {
         cards[i] = new PlayingCard(r, s);
         i++;
      }
   }
}

要洗牌,只需使用Collections.shuffle(),例如

public void shuffle() {
    List<Card> newDeck = Arrays.asList(cards);
    Collections.shuffle(newDeck);
}

List<Card> 由数组支持;然后将洗牌传播到卡片数组。