2013-12-10 60 views
0

我正在嘗試創建一個卡片遊戲高爾夫球以獲得樂趣並練習我的Java技能。我正嘗試將枚舉值用於卡片值和套件。這些值保存在Card Class中的名爲Card的構造函數中。似乎無法打印出一個充滿對象/枚舉的數組列表

我遇到的問題是打印我的ArrayList甲板,它擁有我所有的個人卡片。這個方法可以在DeckOfCards類中找到。我想看看我的程序是否創建了一整套卡片。先謝謝您的幫助!

DeckOfCards類

package finalProjectGolf; 
    // Deck class represent a deck of playing cards. 

import java.util.ArrayList; 
import java.util.Collections; 

public class DeckOfCards { 


    ArrayList<Card> deck = new ArrayList<Card>(); //array of Card objects 

    private int currentCard; // index of next Card to be dealt (0-51) 
    private static int numDecks = 1; 

    public int getCurrentCard() { 
     return currentCard; 
    } 

    public void setCurrentCard(int currentCard) { 
     this.currentCard = currentCard; 
    } 

    private static final int NUMBER_OF_CARDS = 52 * numDecks; //constant # of Cards 


    //constructor fill deck of Cards 
    public DeckOfCards(){ 

     currentCard = 0; //set currentCard so first Card dealt is deck[0] 

     //Card Index 
      int c = 0; 
     //for each deck 
       for (int d = 0; d < numDecks; d++){ 
        //for each suit 
        for (int s = 0; s < 4; s++){ 
         // for each number 
         for (int n = 1; n <= 13; n++){ 
          //add a new card to the deck 
          deck.add(new Card(CardValue.values()[n],Suit.values()[s])); //when using Enums java makes arrays automatically and you can use them by .values() 
          c++;  
         }}}//end for loop 
    }//end DeckOfCards constructor 

    //shuffle deck of Cards with one-pass algorithm 
    public void shuffle() { 
     Collections.shuffle(deck); 
    } 

    public int points(){ 
     int value = deck.get(currentCard).getCardValue().getCardValue(); 
     return value; 
    } 

    //deal one Card 
    public Card dealCard(int currentCard) { 

     //determine whether Cards remain to be dealt 
     if(currentCard < deck.size()){ 
      return deck.get(currentCard); //return current Card in array 
     } 
     else 
      return null; // return null to indicate that all Cards were dealt 
    }//end method dealCard 


    public void printDeck(){ 
     { 
      currentCard = 0; //set currentCard so first Card dealt is deck[0] 

      //Card Index 
       int c = 0; 
      //for each deck 
        for (int d = 0; d < numDecks; d++){ 
         //for each suit 
         for (int s = 0; s < 4; s++){ 
          // for each number 
          for (int n = 1; n <= 13; n++){ 
           //add a new card to the deck 
           System.out.printf(""); //when using Enums java makes arrays automatically and you can use them by .values() 
           c++;  
          }}}//end for loop 
     }//end DeckOfCards constructor 

    } 
}// end class DeckOfCards 

卡類

package finalProjectGolf; 


public class Card 
{ 
     private Suit suit; 
     private CardValue cardValue; 


     public Card (CardValue cardValue, Suit suit) //constructor of Card, holds Card value and it's suit 
     { 
     this.cardValue = cardValue; 
     this.suit = suit; 

     } 

     public Suit getSuit() 
     { 
     return suit; 
     } 

     public void setSuit(Suit suit) 
     { 
     this.suit = suit; 
     } 

     public CardValue getCardValue() 
     { 
     return cardValue; 
     } 

     public void setCardValue(CardValue cardValue) 
     { 
     this.cardValue = cardValue; 
     } 

     public String toString(){ 
      return cardValue + " of " + suit; 
     }// end method toString 


    } 

CardValue類

package finalProjectGolf; 


    public enum CardValue 
    { 
     ACE(1), 
     TWO(2), 
     THREE(3), 
     FOUR(4), 
     FIVE(5), 
     SIX(6), 
     SEVEN(7), 
     EIGHT(8), 
     NINE(9), 
     TEN(10), 
     JACK(11), 
     QUEEN(12), 
     KING(13); 

     private int cardValue; 

     private CardValue (int value) 
     { 
     this.cardValue = value; 
     } 

     public int getCardValue() { 
     return cardValue; 
     } 
    } 

西服類

package finalProjectGolf; 

public enum Suit 
{ 
    HEARTS(1), 
    SPADES(2), 
    CLUBS(3), 
    DIAMONDS(4); 

    private int suit; 

    private Suit (int value) 
    { 
    this.suit = value; 
    } 

    public int getCardSuit() { 
    return suit; 
    } 
} 
+0

問題是......順便說一句,爲什麼你不重寫所有類/枚舉中的'toString'方法? –

回答

1

問題不是你的printDeck方法不起作用(我還沒有測試過,但乍一看看起來很合理),這是你永遠不會說的。您可以將它放在DeckOfCards構造函數的末尾,如果要檢查它是否全部正確。

另外,你真的應該重構一堆你的邏輯,最值得注意的是在DeckOfCards類中。你有一些大塊的計算 - 把它放在一個方法中。另外,不要在類中聲明變量,而應該在構造函數中聲明它們。例如:

ArrayList<Card> deck; //array of Card objects 

private int currentCard; // index of next Card to be dealt (0-51) 
private static int numDecks; 

//constructor fill deck of Cards 
public DeckOfCards(int numDecks){ 
    deck = new ArrayList<Card>(); 
    currentCard = 0; //set currentCard so first Card dealt is deck[0] 
    this.numDecks = numDecks; 

糾正我,如果我錯了,不過,你並沒有真正說明什麼問題是...

+0

有幾個問題,你的建議幫了很大忙,謝謝! – anonymous

2

爲了增加AaronB的回答,您printDeck方法其實錯了,就像你發佈的那樣。目前它打印空字符串52次。另外,你不需要爲了打印套牌中的所有物品而循環三次。一個非常簡單的實現,打印每張卡片上的單甲板新的行會:

public void printDeck() { 
    for(Card card : deck) { 
     System.out.println(card.toString()); 
    } 
} 

您還需要重寫你的枚舉值的toString方法,使他們打印你想要的名字。看看https://stackoverflow.com/a/14413605/1425014如何做到這一點。

你的主類叫做DeckOfCards。這表明這個班級代表了一副撲克牌。但是,通過for (int d = 0; d < numDecks; d++)private static int numDecks = 1來判斷,看起來您打算讓DeckOfCards代表一個或多個牌組。如果你需要的不僅僅是DeckOfCards,而是使DeckOfCards類複雜化,那麼簡單地使用一個集合(比如ArrayList)可能會更清晰。

最後,在您將代碼發佈到此處之前,您應該嘗試確保您的評論有意義。從DeckOfCards構造函數複製/粘貼後,您的printDeck()函數的註釋尚未更改。

+0

偉大的評論,感謝您檢查打印方法。 @anonymous:許多較新的程序員往往會陷入特定的語法;退後一步,通過邏輯和慢速思考它是製作任何高質量代碼非常重要的一部分。 「我真的需要這樣做嗎?這有道理嗎?」在您計劃和編寫代碼時應始終貫穿您的腦海。 – AaronB