使用卡片对象拖曳卡片组

时间:2019-11-13 19:13:27

标签: java loops shuffle

一直在尝试创建一个名为CardDeck的类,以改组在另一个类中创建的卡对象(称为DeckOfCard,对不起混乱,没有很好的命名),但是用尽了如何实现此目的的想法。

这是我想出的,我也包括了原始的DeckOfCards类,任何帮助/建议都受到欢迎和赞赏!

//CODE FOR CARD OBJECT//
   public final static int ace = 1, two= 2,three=3, four=4, five=5, six=6, seven =7, eight=8, nine=9, ten=10, jack= 11, queen=12, king=13; 

   public final static int diamonds= 1, clubs= 2, spades= 3, hearts=4; 

   private final static int numberOfFaces = 13;
   private final static int numberOfSuits = 4;

   private int face, suit;
   private String faceValue, suitName;

   //create a random card 
   public DeckOfCards()
   {
       face= (int)(Math.random() * numberOfFaces);
       setFaceValue();

       suit= (int) (Math.random() * numberOfSuits);
       setSuitName();
    }

   //sets the string representation of each face value to its coorspdoing numeric value
    private void setFaceValue()
   {
       switch(face)
       {
           case ace:
            faceValue= "Ace";
            break;
           case two:
            faceValue= "Two";
            break;
           case three:
            faceValue= "Three";
            break;
           case four:
            faceValue= "Four";
            break;
           case five:
            faceValue = "Five";
            break;
           case six:
            faceValue = "Six";
            break;
           case seven:
            faceValue= "Seven";
            break;
           case eight:
            faceValue= "Eight";
            break;
           case nine:
            faceValue= "Nine";
            break;
           case ten:
            faceValue= "Ten";
            break;
           case jack:
            faceValue= "Jack";
            break;
           case queen:
            faceValue= "Queen";
            break;
           case king:
            faceValue= "King";
            break;
       }
   }

      //set the string representation of each suit 
private void setSuitName()
{
    switch(suit)
    {
        case diamonds:
            suitName = "Diamonds";
            break;
        case clubs:
            suitName= "Clubs";
            break;
        case spades:
            suitName = "Spades";
            break;
        case hearts:
            suitName = "Hearts";
            break;
        }
    }

public String getFaceValue()
{
    return faceValue;
}

public String getSuitName()
{
    return suitName;
}

public String toString()
{
    return faceValue+ " of " +suitName;
}
}

这是我当前的代码...虽然不多,但是这与我到目前为止所能达到的程度差不多:

import java.util.Random;
public class CardDeck
{
   private DeckOfCards[] cards;

   //create new deck of cards
   public CardDeck()
   {
       cards = new DeckOfCards[52];
       int index= 0;


       int[] cardTypes = {DeckOfCards.ace, DeckOfCards.diamonds, DeckOfCards.spades,      DeckOfCards.hearts};

       for(int cardType : cardTypes)
       {
           for(int i = 1; i<=13; i++)
           {
               DeckOfCards card = new DeckOfCards();
               cards[index++]= card;
            }
        }
  }

  //create shuffle method, use loop to generate random suit and random faceValue
    public void shuffle()
    {


        System.out.println("Suffuling cards");

            int loopCount = 53;

            while (loopCount > 0) {

            double index1 = Math.random();

            double index2 = Math.random();

            DeckOfCards temp = cards[index1];

            cards[index1] = cards[index2];

            cards[index2] = temp;

            loopCount--;

}

}

}

4 个答案:

答案 0 :(得分:1)

为等级和西服定义enum类型。这提供了类型安全性,因此您不会意外通过西服参数的排名,反之亦然。枚举值也为在地图中定义不同游戏等的评分系统提供了很好的关键。还请注意,枚举可以具有属性和方法,因此您可以通过这种方式为值添加用户友好名称。

然后创建一个具有等级和西服的Card类型。

遍历等级,对于每个等级,遍历西装。为等级和西装的每个组合创建一个新的Card,并将其添加到List中;这是你的甲板。甲板建成后,您可以shuffle it with a convenience method in Collections.

public final class Card {

    public enum Rank {
        ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
    }

    public enum Suit {
        SPADES, HEARTS, DIAMOND, CLUBS
    }

    private final Rank rank;

    private final Suit suit;

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

    public Rank getRank() {
        return rank;
    }

    public Suit getSuit() {
        return suit;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (!(obj instanceof Card)) return false;
        Card that = (Card) obj;
        return (getRank() == that.getRank()) && (getSuit() == that.getSuit());
    }

    @Override
    public int hashCode() {
        return getRank().hashCode() * 31 + getSuit().hashCode();
    }

    @Override
    public String toString() {
        return getRank() + " of " + getSuit();
    }

}

public class Deck {

    private final List<? extends Card> cards;

    public Deck(Collection<? extends Card> cards) {
        this.cards = new ArrayList<>(cards);
    }

    public void shuffle(Random random) {
        if (random == null) random = ThreadLocalRandom.current();
        Collections.shuffle(cards, random);
    }

    public static Deck newStandardDeck() {
        List<Card> cards = new ArrayList<>();
        for (Card.Rank rank : Card.Rank.values()) {
            for (Card.Suit suit : Card.Suit.values()) {
                cards.add(new Card(rank, suit));
            }
        }
        return new Deck(cards);
    }

}

答案 1 :(得分:0)

您的问题在这里:

double index1 = Math.random();
double index2 = Math.random();

Math.random()返回介于0和1之间的双精度浮点数。 尝试以下方法:

 Random r = new Random();

 while(loopCount > 0) {
     int index1 = r.nextInt(cards.length);
     int index2 = r.nextInt(cards.length);

答案 2 :(得分:0)

因此,您的代码结构存在一些大问题,但我对此进行了挽救。 最简单的解决方案是创建卡片的排序列表,每个套件和值对应一个列表,然后将每个卡片设置为随机索引。这将在“卡片”字段上执行该操作

// create shuffle method, use loop to generate random suit and random faceValue
    public void shuffle() {

        System.out.println("Suffuling cards");

        int loopCount = 0;

        ArrayList<DeckOfCards> inOrder = new ArrayList<DeckOfCards>();
        for(int i = 0; i < 54; i++)
        {
            inOrder.add(cards[i]);
        }

        DeckOfCards[] shuffled = new DeckOfCards[54];

        for(int i = 0; i < 54; i++)
        {
            //Math.random()*size of inOrder will give you a double between 0 and size of in order. 
            //Round this down and convert to an int and you have a random index!
            int randCardIndex = (int) (Math.floor(Math.random()*inOrder.size()));

            //Grab the card and add it to the shuffled deck in the current position
            shuffled[i] = inOrder.get(randCardIndex);

            //Remove this card so it can no longer be grabbed
            inOrder.remove(randCardIndex);
        }

        cards = shuffled;

    }

答案 3 :(得分:0)

请将名称更改为Card,而不是DeckOfCardsCard对象的列表成为一副纸牌,因此您不希望将该对象本身称为该纸牌。

random函数应采用与DeckOfCards的构造函数类似的方法,在这种构造方法中,您的乘数应应用于随机数的顶部并强制转换为int。之后,您的代码应该可以正常工作。卡大小52应该存储为全局私有变量。除非将其存储在变量中并调用该变量,否则切勿使用“幻数”。 index1和index2都应为以下内容:

double index1 = Math.random()*NUM_CARDS

您的私有数组下面具有以下变量:private final int NUM_CARDS = 52

否则,您将走在正确的轨道上,改组机制很好。只是随机索引的生成会获得一个介于0到1之间的十进制数字。

当您必须调试时,请使用打印变量技术。在这种情况下,请打印出index1和index2的每个值,并检查是否正在按预期生成这些值。您也可以尝试一种不太适合初学者的方法,称为调试模式(蟑螂图标),然后双击行号以设置要在其上暂停执行的断点,然后可以查看存储在该点的所有变量。码。