卡片组JAVA

Chr*_*ris 13 java

我已经创建了我的卡片组,可以处理每张卡片和一套西装,直到没有剩余卡片为止.对于我的项目,我需要将其拆分为3个类,其中包括一个驱动程序类.我首先用一切创建了一个类,所以我知道如何使它全部工作.

public class DeckOfCards2 {
  public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

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

    // Shuffle the cards
    for (int i = 0; i < deck.length; i++) {
      int index = (int)(Math.random() * deck.length);
      int temp = deck[i];
      deck[i] = deck[index];
      deck[index] = temp;
    }

    // Display the all the cards
    for (int i = 0; i < 52; i++) {
      String suit = suits[deck[i] / 13];
      String rank = ranks[deck[i] % 13];
      System.out.println( rank + " of " + suit);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在尝试将其分成3个类.我在DeckOfCards类的所有deck/suit变量上得到红色sqiggle行.我不知道如何解决它.

public class DeckOfCards {
  private Card theCard;
  private int remainingCards = 52;

  DeckOfCards() {
    theCard = new Card();   
  }

  public void shuffle(){
    for (int i = 0; i < deck.length; i++) {
       int index = (int)(Math.random() deck.length);
       int temp = deck[i];
       deck[i] = deck[index];
       deck[index] = temp;
       remainingCards--;
     }
  }

  public void deal(){
    for (int i = 0; i < 52; i++) {
       String suit = suits[deck[i] / 13];
       String rank = ranks[deck[i] % 13];
       System.out.println( rank + " of " + suit);
       System.out.println("Remaining cards: " + remainingCards);
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

卡类:

public class Card {
  int[] deck = new int[52];
  String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
  String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

  Card() {
    for (int i = 0; i < deck.length; i++) {
      deck[i] = i;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

经销商类

public class Dealer {
  public static void main(String[]args){
    System.out.println("The deck will randomly print out a card from a full deck each time");

    DeckOfCards player = new DeckOfCards();
    player.deal();
  }
}
Run Code Online (Sandbox Code Playgroud)

Lui*_*ano 34

正如其他人已经说过的那样,你的设计不是很清晰,而且面向对象.

最明显的错误是,在您的设计中,卡片知道卡片组.Deck应该知道卡片并在其构造函数中实例化对象.例如:

public class DeckOfCards {
    private Card cards[];

    public DeckOfCards() {
        this.cards = new Card[52];
        for (int i = 0; i < ; i++) {
            Card card = new Card(...); //Instantiate a Card
            this.cards[i] = card; //Adding card to the Deck
        }
     }
Run Code Online (Sandbox Code Playgroud)

之后,如果你想要你也可以扩展Deck以建立不同的Deck of Cards(例如,超过52张牌,Jolly等).例如:

public class SpecialDeck extends DeckOfCards {
   ....
Run Code Online (Sandbox Code Playgroud)

我要改变的另一件事是使用String数组来表示套装和排名.从Java 1.5开始,该语言支持Enumeration,它非常适合这类问题.例如:

public enum Suits {
    SPADES, 
    HEARTS, 
    DIAMONDS,
    CLUBS;  
}
Run Code Online (Sandbox Code Playgroud)

使用Enum,您可以获得一些好处,例如:

1)枚举是类型安全的,除了预定义的枚举常量之外,你不能将任何其他东西分配给枚举变量.例如,您可以编写Card的构造函数,如下所示:

public class Card {

   private Suits suit;
   private Ranks rank;

public Card(Suits suit, Ranks rank) {
    this.suit = suit;
    this.rank = rank;
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以构建一致的卡片,只接受枚举值.

2)您可以在Switch语句中使用Enum in int或int原始数据类型(这里我们不得不说,因为Java上也允许使用Java 1.7 switch语句)

3)在Java中在Enum上添加新常量很容易,您可以在不破坏现有代码的情况下添加新常量.

4)您可以遍历Enum,这在实例化卡片时非常有用.例如:

/* Creating all possible cards... */
for (Suits s : Suits.values()) {
    for (Ranks r : Ranks.values()) {
         Card c = new Card(s,r);
    }  
}
Run Code Online (Sandbox Code Playgroud)

为了不再发明轮子,我也改变了将数据卡从数组保存到Java Collection的方式,这样你就可以在你的套牌上使用很多强大的方法,但最重要的是你可以使用Java Collection的随机播放你的甲板.例如:

private List<Card> cards = new ArrayList<Card>();

//Building the Deck...

//...

public void shuffle() {
    Collections.shuffle(this.cards); 
}
Run Code Online (Sandbox Code Playgroud)


Asa*_*ssi 7

这是我的实现:

public class CardsDeck {
    private ArrayList<Card> mCards;
    private ArrayList<Card> mPulledCards;
    private Random mRandom;

public enum Suit {
    SPADES,
    HEARTS,
    DIAMONDS,
    CLUBS;
}

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

public CardsDeck() {
    mRandom = new Random();
    mPulledCards = new ArrayList<Card>();
    mCards = new ArrayList<Card>(Suit.values().length * Rank.values().length);
    reset();
}

public void reset() {
    mPulledCards.clear();
    mCards.clear();
    /* Creating all possible cards... */
    for (Suit s : Suit.values()) {
        for (Rank r : Rank.values()) {
            Card c = new Card(s, r);
            mCards.add(c);
        }
    }
}


public static class Card {

    private Suit mSuit;
    private Rank mRank;

    public Card(Suit suit, Rank rank) {
        this.mSuit = suit;
        this.mRank = rank;
    }

    public Suit getSuit() {
        return mSuit;
    }

    public Rank getRank() {
        return mRank;
    }

    public int getValue() {
        return mRank.ordinal() + 2;
    }

    @Override
    public boolean equals(Object o) {
        return (o != null && o instanceof Card && ((Card) o).mRank == mRank && ((Card) o).mSuit == mSuit);
    }


}

/**
 * get a random card, removing it from the pack
 * @return
 */
public Card pullRandom() {
    if (mCards.isEmpty())
        return null;

    Card res = mCards.remove(randInt(0, mCards.size() - 1));
    if (res != null)
        mPulledCards.add(res);
    return res;
}

/**
 * Get a random cards, leaves it inside the pack 
 * @return
 */
public Card getRandom() {
    if (mCards.isEmpty())
        return null;

    Card res = mCards.get(randInt(0, mCards.size() - 1));
    return res;
}

/**
 * Returns a pseudo-random number between min and max, inclusive.
 * The difference between min and max can be at most
 * <code>Integer.MAX_VALUE - 1</code>.
 *
 * @param min Minimum value
 * @param max Maximum value.  Must be greater than min.
 * @return Integer between min and max, inclusive.
 * @see java.util.Random#nextInt(int)
 */
public int randInt(int min, int max) {
    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = mRandom.nextInt((max - min) + 1) + min;
    return randomNum;
}


public boolean isEmpty(){
    return mCards.isEmpty();
}
}
Run Code Online (Sandbox Code Playgroud)


How*_*ard 0

首先,您的课程存在架构问题。deck您在班级内移动了该财产Card。但当然它是卡片组的属性,因此必须在类中DeckOfCards。那么初始化循环不应该在Card你的牌组类的构造函数中,而应该在你的牌组类的构造函数中。int而且,牌组目前是 的数组,但应该是Cards 的数组。

其次,在方法内部,Deal您应该引用suitsasCard.suits并使该成员成为静态最终成员。对于 也一样ranks

最后,请遵守命名约定。方法名称始终以小写字母开头,即而shuffle不是Shuffle