我有一个手类有很多卡。庄家向我发牌,我希望我的游戏显示用户当前拥有的牌。这是我在GameEngine
课堂上打印手牌的游戏代码。问题是它会打印出整个甲板!这是打印出来的:Your hand: [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King] of Spades
。我只希望它打印一张卡,而不是所有卡,那么我的代码有什么问题?
public void beginCardDeal () {
for (int x = 0; x < 2; x++) {
System.out.print("\nDealer is dealing a card. . . \n");
d.dealCard();
System.out.println("\nYour hand: " + pl.getMyCards());
}
}
pl 是Player
该类的一个实例,因为 Players 有一手。d 是 的一个实例Deck
。
public class Player extends Person {
private Hand myCards = new Hand();
private int playerCashAmount = 100;
public Player() {
}
public Player(String name) {
this.name = name;
}
public String getName() {
return super.getName();
}
public int getPlayerCashAmount() {
return playerCashAmount;
}
public void setPlayerCashAmount(int playerCashAmount) {
this.playerCashAmount = playerCashAmount;
}
public Hand getMyCards() {
return myCards;
}
public void setMyCards(Hand myCards) {
this.myCards = myCards;
}
另外,这是我的Deck
课:
public class Deck {
private ArrayList<Card> deck = new ArrayList<Card>();
private List<Card> cardUsed = new ArrayList<Card>();
int numCards = 0;
public Deck(int numCards) {
this.createDeck(numCards, 4);
}
private void createDeck(int numCards, int numSuits) {
deck = new ArrayList<Card>();
cardUsed = new ArrayList<Card>();
if ((numCards % numSuits) > 0) return;
for (int i=0; i < numSuits; i++) {
for(int j=1; j <= (numCards / numSuits); j++) {
deck.add(new Card(new Suit(i), j));
}
}
}
public Card dealCard( ) {
Card dealtCard = null;
if (deck.size() == 0){
deck.addAll(cardUsed);
this.shuffle();
cardUsed = new ArrayList<Card>();
}
dealtCard = deck.get(0);
deck.remove(0);
cardUsed.add(dealtCard);
return dealtCard;
}
public void shuffle() {
Collections.shuffle(deck);
}
public ArrayList<Card> getDeck() {
return deck;
}
public void setDeck(ArrayList<Card> deck) {
this.deck = deck;
}
public int getNumUsedCards() {
return cardUsed.size();
}
public List<Card> getCardUsed() {
return cardUsed;
}
public void setCardUsed(List<Card> cardUsed) {
this.cardUsed = cardUsed;
}
编辑:
public class Hand {
int total = 0;
private ArrayList<Card> hand = new ArrayList<Card>();
public Hand(){
}
public String toString(){
//Suit s = new Suit(total);
//Card c = new Card(s, total);
//return (Arrays.toString(c.getCardRanks())) + " of " + s.getSuitName();
return Arrays.toString(hand.toArray());
}
public boolean discardHand(){
if (hand.isEmpty()) {
hand = new ArrayList<Card>();
}
return false;
}
public void addCard(Card c) {
this.hand.add(c);
}
public Card getPosition(int index){
return hand.get(index);
}
public int handCardCount(){
return hand.size();
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public ArrayList<Card> getHand() {
return hand;
}
public void setHand(ArrayList<Card> hand) {
this.hand = hand;
}