2014-12-05 53 views
-1

我正在創建一個二十一點遊戲,到目前爲止,我已經制作了一個卡類和甲板課。卡類可以工作,但是當我去測試時,我的甲板班沒有顯示任何東西。我的套牌類是爲了使用嵌套循環來設置具有從數組中取值的卡片,然後GetCard假設允許用戶從卡片中獲取一張卡片(當洗牌和處理時)並且SetCard類別是假設的在牌組中設置牌(當洗牌時),但是當我去測試我的牌組時,它只是說對象引用未設置爲對象的實例。我不知道如何解決這個問題。BlackJack甲板類不能正常顯示

任何幫助將appricated

這裏是我有我的甲板類

class Deck 
{ 

    private const Int32 MAXCARDS = 52; 
    private Card[] _cards = new Card[MAXCARDS]; 

    public Deck() 
    { 
     Card.SUIT[] suits = { Card.SUIT.SPADES, Card.SUIT.HEARTS, Card.SUIT.DIAMONDS, Card.SUIT.CLUBS }; 
     String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; 

     Card[] orderedCards = new Card[suits.Length * values.Length]; 
     int newIndex = 0; 

     // Generate an array with all possible combinations of suits and values 
     for (int suitsIndex = 0; suitsIndex < suits.Length; suitsIndex++) 
     { 
      for (int valuesIndex = 0; valuesIndex < values.Length; valuesIndex++) 
      { 
       newIndex = values.Length * suitsIndex + valuesIndex; // Think about it :) 
       orderedCards[newIndex] = new Card(suits[suitsIndex], values[valuesIndex]); 
      } 
     } 
    } 


    // allows the user to get a card from the deck (when shuffling and dealing) 
    public Card GetCard(Int32 index) 
    { 
     if (index >= 0 && index <= 51) 
     { 
      return _cards[index]; 
     } 
     else 
     { 
      throw (new System.ArgumentOutOfRangeException("cardNum", index, 
      "Value must be between 0 and 51.")); 
     } 
    } 
    // allows the user to set a card in the deck (when shuffling) 
    public Card SetCard(Int32 index, Card Card) 
    { 
     if (index >= 0 && index <= 51) 
     { 
      return _cards[index]; 

     } 
     else 
     { 
      throw (new System.ArgumentOutOfRangeException("cardNum", index, 
      "Value must be between 0 and 51.")); 
     } 
    } 
} 

下面是我使用的測試

static void Main(string[] args) 
    { 
     Deck testDeck = new Deck(); 
     Card card; 

     try 
     { 
      for(Int32 index =0; index < 52; index ++) 
      { 
       card = testDeck.GetCard(index); 
       Console.WriteLine(card.Suit + " " + card.Value); 
      } 
      testDeck.SetCard(0, new Card(Card.SUIT.HEARTS, "10")); 
      card = testDeck.GetCard(0); 
      Console.WriteLine(card.Suit + " " + card.Value); 
      testDeck.SetCard(52, new Card(Card.SUIT.DIAMONDS, "3")); 
      card = testDeck.GetCard(52); 
      Console.WriteLine(card.Suit + " " + card.Value); 


     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 
     Console.ReadLine(); 
    } 
+1

非常活躍的類:)幾個小時前有人問如何驗證輸入。發佈這個給他''bool valid = values.Any(x => x == value);' – 2014-12-05 23:46:59

+0

'SetCard()'看起來與GetCard()相同,但忽略了附加函數參數'Card'。 – 2014-12-05 23:51:53

回答

0

你的代碼永遠不要在您的構造函數中爲_cards分配任何值Deck。相反,你填寫一個名爲OrderedCards的局部變量。

刪除OrderedCards,並直接在構造函數中設置_cards。如:

public Deck() 
{ 
    Card.SUIT[] suits = { Card.SUIT.SPADES, Card.SUIT.HEARTS, Card.SUIT.DIAMONDS, Card.SUIT.CLUBS }; 
    String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; 

    int newIndex = 0; 

    // Generate an array with all possible combinations of suits and values 
    for (int suitsIndex = 0; suitsIndex < suits.Length; suitsIndex++) 
    { 
     for (int valuesIndex = 0; valuesIndex < values.Length; valuesIndex++) 
     { 
      newIndex = values.Length * suitsIndex + valuesIndex; // Think about it :) 
      _cards[ newIndex ] = new Card(suits[suitsIndex], values[valuesIndex])); 
     } 
    } 
}