Hi, beginner in vb here, I'm trying to write a game of blackjack in vb.net, and I'm currently trying to set up a subroutine that will check to see if a card matches another card already dealt, and if so deal a different card (to avoid dealing the same card twice, as with a real deck). Here's the relevant code to this part:
Dim notDealt As Boolean Dim cardDealt(9) As Short Dim rand_deck1 As New Random Dim card_check As Short Dim deck1(51) As Integer Public Sub btn_hit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_hit.Click 'hit_count is a variable that tracks how many hits the player has elected to take 'this segment deals hit cards to the player and checks which card it is dealing If hit_count = 0 Then notDealt = False Do Until notDealt = True cardDealt(2) = rand_deck1.Next(52) card_checker(2, 2) Loop End Sub Public Sub card_checker(ByVal card_compared As Integer, ByVal card_checked As Integer) Dim a_total As Short = 0 Do Until card_compared < 0 card_compared = card_compared - 1 If cardDealt(card_checked) = cardDealt(card_compared) Then a_total = a_total + 1 Else a_total = a_total End If Loop If a_total = 0 Then notDealt = True Else notDealt = False End If End Sub
However, upon clicking the hit button, I get this error
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in blackjack.exe
Additional information: Index was outside the bounds of the array.
With this line highlighted:
If cardDealt(card_checked) = cardDealt(card_compared) Then
Does anybody know how I would fix this? I haven't been able to figure out the problem on my own. I'm using visual studio 2008, if that's necessary info.
Thanks to anyone that can help.
Edited 6 Years Ago by Mr. Magoolachub: n/a