制作一副纸牌并将它们洗牌

时间:2015-11-20 23:54:36

标签: java

我现在已经坚持了一段时间,目标是创建一个阵列来保持卡片,一旦我创建阵列打印出来,唯一的卡片就是打印的心脏之王。为什么不迭代?

public class Card
{
    // Card suits (provided for your convenience - use is optional)
    public static final int SPADES   = 0;
    public static final int HEARTS   = 1;
    public static final int CLUBS    = 2;
    public static final int DIAMONDS = 3;

    // Card faces (provided for your convenience - use is optional)
    public static final int ACE      = 1;
    public static final int TWO      = 2;
    public static final int THREE    = 3;
    public static final int FOUR     = 4;
    public static final int FIVE     = 5;
    public static final int SIX      = 6;
    public static final int SEVEN    = 7;
    public static final int EIGHT    = 8;
    public static final int NINE     = 9;
    public static final int TEN      = 10;
    public static final int JACK     = 11;
    public static final int QUEEN    = 12;
    public static final int KING     = 13;




    // define fields here
    private static int suit;
    private static int val;
    private String[] suits = {"Clubs", "Spades", "Diamonds", "Hearts"};
    private String[] vals = {"Ace","2", "3", "4", "5", "6", "7", 
            "8", "9", "10", "Jack", "Queen", "King" };

    // This constructor builds a card with the given suit and face, turned face down.
    public Card(int suit, int val)
    {

        this.val = val;
        this.suit = suit;

    }

    public @Override String toString()
    {
        return vals[val] + " Of " + suits[suit];
    }

    // This method retrieves the suit (spades, hearts, etc.) of this card.
    public int getSuit()
    {

        return this.suit;

    }

    // This method retrieves the face (ace through king) of this card.
    public int getFace()
    {
//      return this.val;

        switch(val)
        {
        case 0 : return 1;
        case 1 : return 2;
        case 2 : return 3;
        case 3 : return 4;
        case 5 : return 6;
        case 6 : return 7;
        case 7 : return 8;
        case 8 : return 9;
        case 9 : return 10;
        case 10 : return 10;
        case 12 : return 10;
        default : return 0;
        }
    }








}`


import java.util.ArrayList;
import java.util.Random;
// This class represents the deck of cards from which cards are dealt to players.
public class Deck
{
    //array
    public static Card[] cards; 
//  private static ArrayList<Card> cards;

    int i;
    int counter;


    // This constructor builds a deck of 52 cards.
    Deck()
    {
        i = 51;
        //array implementation 
        cards = new Card[52];
        int x = 0;


        for(int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 13;j++)
            {

                //Array implementation 
                cards[x] = new Card(i,j);
                x++;
            }
        }


    }




    // This method takes the top card off the deck and returns it.
    public Card deal()
    {

        //Array implementation 
        int index = 0;
        Card temp = cards[index];

        return temp;

    }


    // this method returns true if there are no more cards to deal, false otherwise
    public boolean isEmpty()
    {
        if(cards.length == 0)
        {
            return true;
        }
        return false;
    }

    //this method puts the deck int some random order
//  public void shuffle()
//  {
//      for(int i = 51; i > 0; i--)
//      {
//          int rand = (int) (Math.random() * (i+1));
//          Card temp = this.cards[i];
//          this.cards[i] = cards[rand];
//          cards[rand] = temp;
//      }
//  
//  }


}


public class BlackJack extends Deck
{
    public static void main(String []args)
    {
        System.out.println("Ready to play a game of BlackJack?");

        Deck deck = new Deck();
        for(int i = 0; i < cards.length;i++)
        {
            System.out.println(cards[i]);
        }

2 个答案:

答案 0 :(得分:5)

问题是由以下原因引起的:

// define fields here
private static int suit;
private static int val;

因为它们是静态的,所有Card实例都共享这些,所以它们都将具有它们设置的最后一个值。删除静态。

答案 1 :(得分:1)

为您提供一些提示,以便您可以更轻松地阅读/维护代码:

  • 使用枚举而不是常量。您可以像类
  • 一样为它们提供构造函数/成员变量
  • 使用for-each-loops,它更容易阅读并防止通常的1-by-1问题
  • 在所有其他修饰符之前输入'static'并保持所有静态变量和成员与任何非静态内容分开。这样你就不会偶然混淆它们

此代码的错误/不正常的一面是:

  • 我使用静态嵌套枚举和类。通常每个都应该进入自己的文件并且是非静态的
  • 我喜欢根据它们的范围为我的变量添加前缀,所以我不能搞砸了:参数的pXxx,静态变量的sXxx,成员的mXxxx,方法变量没有前缀

public class CardGame {

static public enum SuitType { SPADES, HEARTS, CLUBS, DIAMONDS } static public enum FaceType { ACE("Ace", 1), // TWO("2", 2), // THREE("3", 3), // FOUR("4", 4), // FIVE("5", 5), // SIX("6", 6), // SEVEN("7", 7), // EIGHT("8", 8), // NINE("9", 9), // TEN("10", 10), // JACK("Jack", 11), // QUEEN("Queen", 12), // KING("King", 13), // ; private final String mName; private final int mValue; private FaceType(final String pName, final int pValue) { mName = pName; mValue = pValue; } public String getName() { return mName; } public int getValue() { return mValue; } public int getValue_alternative() { return ordinal() + 1; // ordinal is the index of the enum, starting at ace=0 ... king=12 // sorting and comparing of suits could also be done by .ordinal() if need be } } static public class Card { private final SuitType mSuit; private final FaceType mFace; public Card(final SuitType pSuit, final FaceType pFace) { mSuit = pSuit; mFace = pFace; } public SuitType getSuit() { return mSuit; } public FaceType getFace() { return mFace; } @Override public String toString() { return mFace.getName() + " of " + mSuit + " - " + mFace.getValue() + (mFace.getValue() > 1 ? " points" : " point"); } } static public class Deck { private final ArrayList<Card> mCards; public Deck() { mCards = createFullDeck(); } private ArrayList<Card> createFullDeck() { // can also be static as it dowes not use member cariables => potential factory method final ArrayList<Card> ret = new ArrayList<>(); for (final SuitType suit : SuitType.values()) { for (final FaceType face : FaceType.values()) { final Card c = new Card(suit, face); ret.add(c); } } return ret; } public void shuffleRandomly() { final ArrayList<Card> tempList = new ArrayList<>(); tempList.addAll(mCards); mCards.clear(); while (tempList.size() > 0) { final int index = (int) (Math.random() * tempList.size()); final Card card = tempList.remove(index); mCards.add(card); } } public void print() { System.out.println(" - - - DECK BEGINS - - - "); int counter = 0; for (final Card c : mCards) { System.out.println("\t" + c); ++counter; } System.out.println("> Printed " + counter + " cards."); System.out.println(" - - - DECK ENDS - - - "); } } public static void main(final String[] args) { final Deck d = new Deck(); d.print(); d.shuffleRandomly(); d.print(); } }