I am creating a little BlackJack application in C#. I am able to deal an intial hand to both the dealer and the player and then hit for the player. However once I do this, the dealer is supposed to draw a card from the deck when the dealer handvalue is less than 17.
This is the get a single card from the deck in the deck class:
public Card GetCard()
{
int r0 = rndCard.Next(0, cardsInDeck_.Count - 1);
cardsInDeck_.RemoveAt(r0);
return cardsInDeck_[r0];
}
when I hit "stay" and the dealer activates I get this error on the last line
Index was out of range. Must be non-negative and less than the size of the collection
This is my "stay" method on the form
private void buttonStay_Click(object sender, EventArgs e)
{
while (dealer.GetValue() < 17)
{
dealer.CardsInDealerHand.Add(deck.GetCard());
}
dealerHandValue_ = dealer.GetValue();
if (dealerHandValue_ > 21)
{
Win();
}
else
{
WinCondition();
}
}
I am new to programming in general but I think that something is wrong with either my deck or dealer.
I would appreciate any help on this.