0

因此,在我的 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 方法和甲板类的 shuffle 方法上。有什么帮助吗?

4

1 回答 1

4

shuffle可以实现如下

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

    }

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

甲板用

 /** 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;
    }

至于卡,我会改变一些东西。您分配一堆整数来表示卡片,如果您不关心这些值,我会亲自使用枚举。这样,具有数字以外的值的牌,即 1 点与 10 点,将由它们的名称表示,而不是某个数字。谁真的说过“我有 12 个俱乐部”。我会把我的东西改成这个。

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

然后,您将更改所有的 get 方法,但真正的优势在于您不需要长的 switch 语句来从枚举值中推断出卡片名称。你可以像这样实现 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";
    }
于 2013-04-04T21:34:23.853 回答