[使用卡片对象洗牌

问题描述 投票:0回答:1

[一直试图创建一个名为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--;

}

}

}
java loops shuffle
1个回答
0
投票
您的问题在这里:
© www.soinside.com 2019 - 2024. All rights reserved.