我有一個手牌類,有一定數量的牌。經銷商向我的手牌發放一張牌,我希望我的遊戲能夠顯示用戶目前擁有的牌。這裏是我的GameEngine
課程中的遊戲代碼,我在手中打印出卡片。問題是它打印出整個卡組!這是打印出來的:Your hand: [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King] of Spades
。我只想要它打印一張卡片,而不是所有的卡片,所以我的代碼中搞砸了什麼?BlackJack爪哇:手印錯誤
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
類的一個實例,因爲玩家有一隻手。 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;
}
你的'Hand'類在哪裏? –
我們需要看手類,以及這些類如何相互作用來回答這個問題。 –
它的責任是什麼? – user573215