我正在为我的计算机科学课开展一个项目。我的老师希望我们创建一个程序,这个程序将有一副牌,能够将它们洗牌并交易。他希望我们创建一个具有变量suitName,suitValue,faceName和faceValue的卡片类。我目前的代码如下:
//Construct Array
Card[] deck = new Card[52];
//Define cards
Card aceSpades = new Card("Spades", 1, "Ace", 1);
Card aceHearts = new Card("Hearts", 2, "Ace", 1);
Card aceClubs = new Card("Clubs", 3, "Ace", 1);
Card aceDiamonds = new Card("Diamonds", 4, "Ace", 1);
Card twoSpades = new Card("Spades", 1, "Two", 2);
Card twoHearts = new Card("Hearts", 2, "Two", 2);
Card twoClubs = new Card("Clubs", 3, "Two", 2);
Card twoDiamonds = new Card("Diamonds", 4, "Two", 2);
Card threeSpades = new Card("Spades", 1, "Three", 3);
Card threeHearts = new Card("Hearts", 2, "Three", 3);
Card threeClubs = new Card("Clubs", 3, "Three", 3);
Card threeDiamonds = new Card("Diamonds", 4, "Three", 3);
Card fourSpades = new Card("Spades", 1, "Four", 4);
Card fourHearts = new Card("Hearts", 2, "Four", 4);
Card fourClubs = new Card("Clubs", 3, "Four", 4);
Card fourDiamonds = new Card("Diamonds", 4, "Four", 4);
Card fiveSpades = new Card("Spades", 1, "Five", 5);
Card fiveHearts = new Card("Hearts", 2, "Five", 5);
Card fiveClubs = new Card("Clubs", 3, "Five", 5);
Card fiveDiamonds = new Card("Diamonds", 4, "Five", 5);
Card sixSpades = new Card("Spades", 1, "Six", 6);
Card sixHearts = new Card("Hearts", 2, "Six", 6);
Card sixClubs = new Card("Clubs", 3, "Six", 6);
Card sixDiamonds = new Card("Diamonds", 4, "Six", 6);
Card sevenSpades = new Card("Spades", 1, "Seven", 7);
Card sevenHearts = new Card("Hearts", 2, "Seven", 7);
Card sevenClubs = new Card("Clubs", 3, "Seven", 7);
Card sevenDiamonds = new Card("Diamonds", 4, "Seven", 7);
Card eightSpades = new Card("Spades", 1, "Eight", 8);
Card eightHearts = new Card("Hearts", 2, "Eight", 8);
Card eightClubs = new Card("Clubs", 3, "Eight", 8);
Card eightDiamonds = new Card("Diamonds", 4, "Eight", 8);
Card nineSpades = new Card("Spades", 1, "Nine", 9);
Card nineHearts = new Card("Hearts", 2, "Nine", 9);
Card nineClubs = new Card("Clubs", 3, "Nine", 9);
Card nineDiamonds = new Card("Diamonds", 4, "Nine", 9);
Card tenSpades = new Card("Spades", 1, "Ten", 10);
Card tenHearts = new Card("Hearts", 2, "Ten", 10);
Card tenClubs = new Card("Clubs", 3, "Ten", 10);
Card tenDiamonds = new Card("Diamonds", 4, "Ten", 10);
Card jackSpades = new Card("Spades", 1, "Jack", 11);
Card jackHearts = new Card("Hearts", 2, "Jack", 11);
Card jackClubs = new Card("Clubs", 3, "Jack", 11);
Card jackDiamonds = new Card("Diamonds", 4, "Jack", 11);
Card queenSpades = new Card("Spades", 1, "Queen", 12);
Card queenHearts = new Card("Hearts", 2, "Queen", 12);
Card queenClubs = new Card("Clubs", 3, "Queen", 12);
Card queenDiamonds = new Card("Diamonds", 4, "Queen", 12);
Card kingSpades = new Card("Spades", 1, "King", 13);
Card kingHearts = new Card("Hearts", 2, "King", 13);
Card kingClubs = new Card("Clubs", 3, "King", 13);
Card kingDiamonds = new Card("Diamonds", 4, "King", 13);
deck[0] = aceSpades;
deck[1] = aceHearts;
deck[2] = aceClubs;
deck[3] = aceDiamonds;
deck[4] = twoSpades;
deck[5] = twoHearts;
deck[6] = twoClubs;
deck[7] = twoDiamonds;
deck[8] = threeSpades;
deck[9] = threeHearts;
deck[10] = threeClubs;
deck[11] = threeDiamonds;
deck[12] = fourSpades;
deck[13] = fourHearts;
deck[14] = fourClubs;
deck[15] = fourDiamonds;
deck[16] = fiveSpades;
deck[17] = fiveHearts;
deck[18] = fiveClubs;
deck[19] = fiveDiamonds;
deck[20] = sixSpades;
deck[21] = sixHearts;
deck[22] = sixClubs;
deck[23] = sixDiamonds;
deck[24] = sevenSpades;
deck[25] = sevenHearts;
deck[26] = sevenClubs;
deck[27] = sevenDiamonds;
deck[28] = eightSpades;
deck[29] = eightHearts;
deck[30] = eightClubs;
deck[31] = eightDiamonds;
deck[32] = nineSpades;
deck[33] = nineHearts;
deck[34] = nineClubs;
deck[35] = nineDiamonds;
deck[36] = tenSpades;
deck[37] = tenHearts;
deck[38] = tenClubs;
deck[39] = tenDiamonds;
deck[40] = jackSpades;
deck[41] = jackHearts;
deck[42] = jackClubs;
deck[43] = jackDiamonds;
deck[44] = queenSpades;
deck[45] = queenHearts;
deck[46] = queenClubs;
deck[47] = queenDiamonds;
deck[48] = kingSpades;
deck[49] = kingHearts;
deck[50] = kingClubs;
deck[51] = kingDiamonds;
有没有更有效的方法来做到这一点
答案 0 :(得分:4)
是:
deck
答案 1 :(得分:2)
Card[] deck = new Card[52];
deck[0] = new Card("Spades", 1, "Ace", 1);
您可以为数组索引分配引用
答案 2 :(得分:2)
以下代码可以为您服务。它只是迭代两个枚举,并为你洗牌。
使用以下抽象:
Suit
Rank
Card
(Rank
和Suit
)Deck
:包含卡片(如果需要,随机播放)使用简单的抽象和非复杂的代码来保存所有卡片。
public enum Rank {
ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
public enum Suit {
hearts, clubs, diamonds, spades
}
public class Card {
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() {
return rank;
}
public Suit suit() {
return suit;
}
}
public class Deck {
// Static
private static final List<Card> allCards = new ArrayList<>();
static {
for (Suit s : Suit.values()) {
for (Rank r : Rank.values()) {
allCards.add(new Card(r, s));
}
}
}
private final List<Card> shuffledCards;
public Deck() {
// Shuffle
shuffledCards= new ArrayList<>(allCards);
Collections.shuffle(shuffledCards);
}
public Iterable<Card> shuffledCards() {
return shuffledCards;
}
}
答案 3 :(得分:0)
为了简化你的代码,这就是我要做的事情(只是我的两分钱):
在您的Card
课程中,让您的构造函数接受两个参数,接受int
并为您的suitName
和faceName
变量指定值,例如:
public Card(int suitValue, int faceValue) {
switch (suitValue) {
case 1: suitName = "Spades"; break;
case 2: suitName = "Hearts"; break;
case 3: suitName = "Clubs"; break;
case 4: suitName = "Diamonds"; break;
default: throw new RuntimeException("Invalid suit value: " + String.valueOf(suitValue));
}
// same for faceName
this.suitValue = suitValue;
this.faceValue = faceValue;
}
使用循环创建数组:
Card[] deck = new Card[52];
for (int k = 1; k <= 4; k++) {
for (int l = 1; l <= 13; l++) {
card[(k-1)*13+(l-1)] = new Card(k,l);
}
}
答案 4 :(得分:0)
我会这样做:
String[] suitNames = {"Spades", "Hearts", "Clubs", "Diamonds" };
String[] faceNames = {"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"};
List<Card> deck = new ArrayList<Card>();
for (int i = 0; i < suitNames.length; i++) {
String suitName = suitNames[i];
int suitValue = i + 1;
for (int j = 0; j < faceNames.length; j++) {
String faceName = faceNames[j];
int faceValue = j + 1;
Card c = new Card(suitName, suitValue, faceName, faceValue);
deck.add(c);
}
}