卡和甲板toString和shuffle

时间:2013-04-04 21:28:12

标签: java tostring shuffle

所以,在我的java课程中,我们获得了这些卡片和套牌课程,这些课程将在以后用于纸牌游戏。这是卡片代码:

public class Card {

    // public constants:

    public static final int ACE     = 1;
    public static final int DEUCE   = 2;
    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 KNAVE   = 11;
    public static final int QUEEN   = 12;
    public static final int KING    = 13;

    public static final int SPADES  = 1;
    public static final int HEARTS  = 2;
    public static final int DIAMONDS = 3;
    public static final int CLUBS   = 4;

    // private instance data;

    private int rank;
    private int suit;

    // public constructor:
    public Card ( int rank, int suit ) {
        if ( rank < Card.ACE | rank > Card.KING | suit < Card.SPADES | suit > Card.CLUBS ) {
            throw new IllegalArgumentException();
        } else {
            this.rank = rank;
            this.suit = suit;
        }
    }

    /** Returns this card's suit. */
    public int getSuit() {
        return this.suit;
    }

    /** Returns this card's rank. */
    public int getRank() {
        return this.rank;
    }   

    /** Returns a stringy version of this card. */
    public String toString() {
        // Replace the next instruction with your code:
        throw new UnsupportedOperationException();
    }


}

这是甲板类代码:

public class Deck {

    // private instance data;
    private Card[] cards;

    // public constructor:
    public Deck() {
        this.cards = new Card[52];
        int i = 0;
        for ( int suit = Card.SPADES; suit <= Card.CLUBS; suit++ ) {
            for ( int rank = Card.ACE; rank <= Card.KING; rank++ ) {
                this.cards[i] = new Card(rank,suit);
                i++;
            }
        }
    }

    /** Returns a copy of the card at the specified index in this deck. */
    public Card cardAt(int index) {
        if ( index < 0 | index > 51 ) {
            throw new IllegalArgumentException();
        } else {
            return new Card( this.cards[index].getRank(),this.cards[index].getSuit() ); 
        }
    }

    /** Shuffles this deck. */
    public void shuffle() {
        // Replace the next instruction with your code:
        throw new UnsupportedOperationException();

    }

    /** Returns a stringy version of this deck. */
    public String toString() {
        // Replace the next instruction with your code:
        throw new UnsupportedOperationException();
    }


}

我的问题是我被困在两个类的toString方法和deck类的shuffle方法上。有什么帮助吗?

1 个答案:

答案 0 :(得分:4)

shuffle可以按如下方式实现

 /** Shuffles this deck. */
    public void shuffle() {
       // Shuffle the elements in the array
       Collections.shuffle(Arrays.asList(cards));

    }

toString只是构建一个字符串并返回它。这可以是您想要描述您的课程的任何内容。这就是我要做的。

for deck

 /** Returns a stringy version of this deck. */
    public String toString() {
         String output="Current Deck:\n";
         for (int i=0; i <cards.length ; i++){
          output+= cardAt(i).toString();
         }
       return output;
    }
对于卡,我会改变一些事情。你分配了一堆int来表示卡片,如果你不关心这些值,我会亲自使用枚举。这样,具有除数字之外的值的卡,即插孔与10,将由它们的名称而不是某个数字表示。谁真的说“我有12个俱乐部”。我会把我的东西改成这个。

public enum Rank{ACE,TWO,THREE ....SO ON}
public enum Suit {HEARTS,CLUBS,SPADES,DIAMONDS}

然后你会改变你所有的get方法,但真正的好处在于你不需要一个长的switch语句来从枚举值中推断卡NAME。你可以像这样实现toString。

//change rank and suit type from int to the Enums
Rank rank;
Suit suit;

/*modify all your getters*/

 /** Returns a stringy version of this deck. */
    public String toString() {
        //enum will be printed out!

        return rank+" of " + suit +"\n";
    }