BlackJack甲板类无法正常显示

时间:2014-12-05 23:41:41

标签: c#

我正在制作一个二十一点游戏,到目前为止我已经制作了一个卡片类和一个套牌类。卡类可以工作,但是当我去测试它时,我的套牌类没有显示任何东西。我的套牌类是使用嵌套循环来设置带有从数组中获取的值的卡,然后GetCard假设允许用户从套牌中获取卡(当洗牌和交易时)并且假设SetCard类在牌组中设置牌(当洗牌时)但是当我去测试我的牌组时,它只是说对象引用没有设置为对象的实例。我不知道如何解决这个问题。

任何帮助都会被批评

以下是我的甲板课程

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 个答案:

答案 0 :(得分: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]) );
        }
    }
}