[英]Card and Deck toString and shuffle
So, in my java class, we were given these card and deck classes, which will be used for a card game later on. 因此,在我的Java类中,我们获得了这些纸牌和套牌类,这些纸牌和套牌类将在以后用于纸牌游戏。 Here is the card code:
这是卡代码:
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();
}
}
And here is deck class code: 这是甲板类代码:
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();
}
}
My issue is that I am stuck on the toString method for both classes and the shuffle method for the deck class. 我的问题是,对于这两个类,我都陷入了toString方法,而对于甲板类,我陷入了洗牌方法。 Any help?
有什么帮助吗?
The shuffle can be implemented as follows 改组可以实现如下
/** Shuffles this deck. */
public void shuffle() {
// Shuffle the elements in the array
Collections.shuffle(Arrays.asList(cards));
}
The toString simply builds a string and returns it. toString只是构建一个字符串并返回它。 This can be anything you want to describe your class.
这可以是您要描述的任何类。 Here is what I would do.
这就是我要做的。
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;
}
as for card, I'd change a few things. 至于卡,我会改变一些事情。 You assign a bunch of ints to represent the card, I would personally use an enum if you don't care about the values.
您分配了一堆整数来表示卡,如果您不关心这些值,我个人会使用一个枚举。 This way cards that have values other than a number, ie a jack vs a 10, will be represented by their name, rather than some number.
这样,具有非数字值的卡(即千斤顶与10)将通过其名称而不是某个数字来表示。 Who really ever says "i have a 12 of clubs".
谁真的说过“我有12个俱乐部”。 I'd change my stuff to this.
我要把东西换成这个。
public enum Rank{ACE,TWO,THREE ....SO ON}
public enum Suit {HEARTS,CLUBS,SPADES,DIAMONDS}
you'd then change all of your get methods, but the real advantage comes in the fact you don't need a long switch statement to deduce the card NAME from the enum values. 然后,您将更改所有的get方法,但真正的优势在于您不需要很长的switch语句即可从枚举值推断出卡名。 you can implement toString like this.
您可以像这样实现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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.