gpt4 book ai didi

java - 卡牌和牌组弦乐和洗牌

转载 作者:行者123 更新时间:2023-12-01 18:51:26 26 4
gpt4 key购买 nike

因此,在我的 java 类(class)中,我们获得了这些纸牌和牌组类,这些类将在稍后的纸牌游戏中使用。这是卡代码:

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 方法上。有什么帮助吗?

最佳答案

shuffle可以实现如下

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

}

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

对于甲板

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

至于卡,我会改变一些东西。您分配一堆整数来代表卡片,如果您不关心这些值,我个人会使用枚举。这样,具有数字以外的值的牌(即 J 与 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";
}

关于java - 卡牌和牌组弦乐和洗牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15822063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com