I'm making a hangman game and I'm working on the difficulty feature. I keep getting an error when trying to select a random word. What I'm doing is populating an array at my form load here:
Then I choose from three buttons to set the difficulty. 0 is easy 1 is medium 2 is hard. Then I create a temporary list(of string) and add words from wordList to this new temporary list based on the difficult. I'm doing that like this:
Finally I choose a random word from this newely populated temporary list of string like this:
I'm getting the error at setting int. It's telling me 'minValue' cannot be greater than maxValue. Which tells me that there must be 0 words in the temporary list. But I know that there are words in the tempList because when I add:
And set a breakpoint at the for, it tells me the count - 1 is above 0. So I don't know why it's throwing that error. Could y'all possibly point it out for me?
vb.net Code:
wordList = My.Resources.wordList.Split(CChar(Environment.NewLine))
Then I choose from three buttons to set the difficulty. 0 is easy 1 is medium 2 is hard. Then I create a temporary list(of string) and add words from wordList to this new temporary list based on the difficult. I'm doing that like this:
vb.net Code:
'Create a temporary list Dim tempList As New List(Of String) 'Add to the temporary list based on the difficulty For i As Integer = wordList.Count - 1 To 0 Step -1 Select Case difficult Case 0 If wordList(i).Length <= 3 Then tempList.Add(wordList(i)) End If Case 1 If wordList(i).Length >= 4 AndAlso wordList(i).Length <= 5 Then tempList.Add(wordList(i)) End If Case 2 If wordList(i).Length >= 6 Then tempList.Add(wordList(i)) End If End Select Next
Finally I choose a random word from this newely populated temporary list of string like this:
vb.net Code:
'Choose a randomword Dim r As New Random Dim int As Integer = r.Next(0, tempList.Count - 1) rndWord = tempList.Item(int)
I'm getting the error at setting int. It's telling me 'minValue' cannot be greater than maxValue. Which tells me that there must be 0 words in the temporary list. But I know that there are words in the tempList because when I add:
Code:
For i As Integer = tempList.Count - 1 To 0 Step -1
Dim str As String = tempList.Item(i)
Next