2013-07-15 38 views
0

我正在试图将5张牌交给一个可以包含6张牌对象的手形物体。 我写过手课,甲板课和卡课。将牌从一副牌交给一张名为Hands的牌阵列

该代码将卡交易给Hand对象,该对象可以包含Card对象,但我一直在Presentation.dll异常中获取System.Reflection.TargetInvocationException。

//function to deal specified player the topmost cards from deck 
//recieves an array of hands or Hand object 
public void dealPlayerCardsFromDeck(Hand players, int numberOfCards) 
{ 
    int j = 0; 

    //loop to go through deck elements and to ensure the end of the player's hand isnt reached 
    for (int i = 0; (i < deckLength) && (j <= numberOfCards); i++) 
    { 
     if (deck[i] != null) 
     { 
      players.hands[j].face = deck[i].face; 
      players.hands[j].value = deck[i].value; 
      deck[i] = null; 
      j++; 
     } 
    } 
}//end function 

这里是调用它在main(代码)

cardDeck.dealPlayerCardsFromDeck(players[0],5); 

的 “cardDeck” 是类甲板的对象

Deck cardDeck = new Deck(); 

请注意我使用C#

回答

0

我认为最合乎逻辑的解释是当使用i或j对数组进行索引时,循环中的某种索引输出或范围问题。 我推荐在调试器中执行以查看i或j是否超过了阵列的大小

+0

我确实这样做,异常发生在“players.hands [j] .face = deck [i]”。面对;”在我或j得到增加之前 –