c# - BlackJack deck class not displaying properly -


this question has answer here:

i creating blackjack game , far have made card class , deck class far. card class works deck class isn't displaying when go test it. deck class supose use nested loop set cards values taken arrays , getcard suppose allow user card deck (when shuffling , dealing) , setcard class suppose set card in deck (when shuffling) when go test deck class says object reference not set instance of object. not sure on how fix this.

any appricated

here have deck class

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 array possible combinations of suits , values         (int suitsindex = 0; suitsindex < suits.length; suitsindex++)         {             (int valuesindex = 0; valuesindex < values.length; valuesindex++)             {                 newindex = values.length * suitsindex + valuesindex; // think :)                 orderedcards[newindex] = new card(suits[suitsindex], values[valuesindex]);             }         }     }      // allows user card deck (when shuffling , dealing)     public card getcard(int32 index)     {         if (index >= 0 && index <= 51)         {             return _cards[index];         }         else         {             throw (new system.argumentoutofrangeexception("cardnum", index,             "value must between 0 , 51."));         }     }     // allows user set card in 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 between 0 , 51."));         }     } } 

here's code using test it

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();     } 

you never assign value _cards in constructor deck. instead, fill local variable called orderedcards.

drop orderedcards, , directly set _cards in constructor. such this:

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 array possible combinations of suits , values     (int suitsindex = 0; suitsindex < suits.length; suitsindex++)     {         (int valuesindex = 0; valuesindex < values.length; valuesindex++)         {             newindex = values.length * suitsindex + valuesindex; // think :)             _cards[ newindex ] = new card(suits[suitsindex], values[valuesindex]) );         }     } } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -