|
|
Argh, visual basic express ed. 2008, hangman won't work! says that 'Index was out of range. Must be non-neg'?
When user has input too many wrong goes it crashes and comes up yellow at the line 'index = thisword.word.IndexOf(letter, start)' saying 'Index was out of range. Must be non-neg'.
Code:
Public Class frmHangman
Private Sub btnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMenu.Click
Me.Hide()
frmMenu.Show()
End Sub
'create 'bag' variable as a new collection
Public bag As New Collection
'TEXT
Public thisword As word
Public counter As Integer = 0
Public index As Integer
Public foundLetter As Boolean = False
Structure word
'create 'word' and 'hint' as string
Public word As String
Public hint As String
End Structure
Private Sub frmHangman_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim index As Integer
Dim wordString(1) As String
'read hangman .txt file as new streamreader
Dim readthis As New System.IO.StreamReader("hangman.txt")
Do While readthis.EndOfStream = False
'split the string in text file up where commas appear
wordString = Split(readthis.ReadLine(), ",")
'first part of .txt line is for the word
thisword.word = wordString(0)
thisword.word.ToLower()
'second part of the .txt line is for the hint
thisword.hint = wordString(1)
thisword.hint.ToLower()
'add to the bag
bag.Add(thisword)
Loop
readthis.Close()
'get random question from the bag
Randomize()
index = Rnd() * (bag.Count - 1) + 1
thisword = bag.Item(index)
lblClue.Text = thisword.hint
bag.Remove(index)
For index = 1 To thisword.word.Length
'show number of dashes to represent letters
lblDisplay.Text = lblDisplay.Text & "-"
Next
End Sub
Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click, btnB.Click, btnC.Click, btnD.Click, btnE.Click, btnF.Click, btnG.Click, btnH.Click, btnI.Click, btnJ.Click, btnK.Click, btnL.Click, btnM.Click, btnN.Click, btnO.Click, btnP.Click, btnQ.Click, btnR.Click, btnS.Click, btnT.Click, btnU.Click, btnV.Click, btnW.Click, btnX.Click, btnY.Click, btnZ.Click
display(sender)
Dim index As Integer
Dim start As Integer = 0
Dim letter As String
Do
letter = sender.Text
'see if the letter combination is in the string
letter = letter.ToLower
index = thisword.word.IndexOf(letter, start)
'if it is then
If index >= 0 Then
'remove the dashes corresponding to the letters
lblDisplay.Text = lblDisplay.Text.Remove(index, letter.Length)
'and insert the letters
lblDisplay.Text = lblDisplay.Text.Insert(index, letter)
foundLetter = True
Else
If foundLetter = True Then
foundLetter = False
Else
counter = counter + 1
If counter = 1 Then
pbFrame.Show()
ElseIf counter = 2 Then
pbHead.Show()
ElseIf counter = 3 Then
pbBody.Show()
ElseIf counter = 4 Then
pbArm1.Show()
ElseIf counter = 5 Then
pbArm2.Show()
ElseIf counter = 6 Then
pbLeg1.Show()
ElseIf counter = 7 Then
pbLeg2.Show()
MessageBox.Show("too bad - run out of goes")
'start = index + 1
'start = index + 1
If bag.Count > 0 Then
Randomize()
index = Rnd() * (bag.Count - 1) + 1
thisword = bag.Item(index)
lblClue.Text = thisword.hint
bag.Remove(index)
'reset the dashes
lblDisplay.Text = ""
'show the dashes
For index = 1 To thisword.word.Length
'show number of dashes to represent letters
lblDisplay.Text = lblDisplay.Text & "-"
Next
pbArm1.Hide()
pbArm2.Hide()
pbBody.Hide()
pbFrame.Hide()
pbHead.Hide()
pbLeg1.Hide()
I get your search, index = thisword.word.IndexOf(letter, start), which is fine but don't understand why start is a variable. Won't the seach always be from the start of the word, 0? You can just put in 0. I'd imagine the error is becuase your start variable is greater than thisword.length and that somewhere your changing the start vairable and the position of the search.
You can play Hangman for dosh on the net . Is this taking it too far ? Classed as a game of skill . What do you think ?I have to make a small computer game using Visual Basic for school, and I'm running low on ideas. Some of the othersThere must be a formula to it, which are the most common letters and which order would you use them in?I want to make a story of Oedipus, and for those of you who don't know, someone commits suicide by hanging in that story.I have this book that's due SOON and I'm also a very slow reader. Anyone know where I can get an audio book forI am looking for a free online hangman games with genres such as pop trivia, music, celebs, tv&movies, or stuff likeI am looking for a free online hangman games with genres such as pop trivia, music, celebs, tv&movies, or stuff like
|
|
|