VB 2005 Hangman Game Issue

Issue with Substring

Page 1 of 1

2 Replies - 1851 Views - Last Post: 15 August 2009 - 03:48 PM Rate Topic: -----

#1 rjarman65   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 09

VB 2005 Hangman Game Issue

Posted 14 August 2009 - 09:05 PM

Hi Everyone,

My university decided that it was time for me to take some Visual Basic and I'm purely a Java guy. With that in mind, I had to make this game a few different ways. Each way works exactly as it should except for the one I'm having a problem with. I'm getting an Arguement out of range exception for the wordIndex in the area where I try to prevent anything from letters from occurring. I have changed the parameters to rediculous levels and continue to get the same exception. Could anyone give me a clue on the path I need to venture to correct it? The purpose of the application is to allow one person to input a word from 1 - 10 letters in length without entering in any other symbols or numbers. The problem occurs when the word entered is only letters which is obviously not good.

Thank you

Here is the code:

 
Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xFileExitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xFileExitMenuItem.Click
		Me.Close()

	End Sub

	Private Sub xFileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xFileNewMenuItem.Click

		' variables
		Dim word As String
		Dim letter As String
		Dim isDashReplaced As Boolean
		Dim isGameOver As Boolean
		Dim incorrectCounter As Integer
		Dim isAllLetters As Boolean
		Dim wordIndex As Integer

		' hides the picture boxes
		Me.xBottomPictureBox.Visible = False
		Me.xPostPictureBox.Visible = False
		Me.xTopPictureBox.Visible = False
		Me.xRopePictureBox.Visible = False
		Me.xHeadPictureBox.Visible = False
		Me.xBodyPictureBox.Visible = False
		Me.xRightArmPictureBox.Visible = False
		Me.xRightLegPictureBox.Visible = False
		Me.xLeftArmPictureBox.Visible = False
		Me.xLeftLegPictureBox.Visible = False

		' get a word 1 - 10 characters long from 1st player
		' first request for a word
		word = InputBox("Enter a word from 1 to 10 letters long:", "Hangman Game")
		Do
			' stops words from going over 10 letter while in loop
			If word.Length > 10 Then
				word = InputBox("Please enter a word which is 1 - 10 letters long. Enter a new one in now:", "Hangman Game")
			End If
			' stops words from going under 1 letter while in loop
			If word.Length < 1 Then
				word = InputBox("Please enter a word which is 1 - 10 letters long. Enter a new one in now:")
			End If
		Loop Until word.Length < 11 And word.Length > 0

		' sets isAllLetters to true for alphabet requirement
		isAllLetters = True
		' Loop to ensure the word entered is only made of letters
		Do While isAllLetters AndAlso wordIndex < 10
			If word.Substring(wordIndex, 1) Like "[!a-zA-Z]" Then
				isAllLetters = False
			End If
			wordIndex = wordIndex + 1
		Loop
		If isAllLetters Then
			'convert the word to uppercase IF only letters
			word = word.ToUpper

			' displays the dashes in xWordLabel depending on how many letters the word is
			' clears the xIncorrectLabel at same time
			If word.Length = 1 Then
				Me.xWordLabel.Text = "-"
			ElseIf word.Length = 2 Then
				Me.xWordLabel.Text = "--"
			ElseIf word.Length = 3 Then
				Me.xWordLabel.Text = "---"
			ElseIf word.Length = 4 Then
				Me.xWordLabel.Text = "----"
			ElseIf word.Length = 5 Then
				Me.xWordLabel.Text = "-----"
			ElseIf word.Length = 6 Then
				Me.xWordLabel.Text = "------"
			ElseIf word.Length = 7 Then
				Me.xWordLabel.Text = "-------"
			ElseIf word.Length = 8 Then
				Me.xWordLabel.Text = "--------"
			ElseIf word.Length = 9 Then
				Me.xWordLabel.Text = "---------"
			Else
				Me.xWordLabel.Text = "----------"
			End If
			Me.xIncorrectLabel.Text = String.Empty

			' start of loop for 2nd player
			' ends when they guess the word correctly one letter at a time
			' or they picked 10 wrong letters
			Do Until isGameOver
				' get letter from 2nd player
				' also uses a nested loop to ensure only letters can be picked by 2nd player
				letter = InputBox("Enter a letter:", _
								"Letter", " ", 450, 400)
				Do Until letter Like "[a-zA-Z]"
					letter = InputBox("Please only enter in letters when guessing:", _
					   "Letter", "", 450, 400)
				Loop
				' converts them to capitals
				letter = letter.ToUpper

				' looks for letter that was picked
				For indexnum As Integer = 0 To 9
					' if the word has the letter picked in it,
					' the dash is replaced with the chosen letter
					If word.Substring(indexnum, 1) = letter Then
						Mid(Me.xWordLabel.Text, indexnum + 1) = letter
						isDashReplaced = True
					End If
				Next indexnum

				' checks if game won by 2nd player
				If isDashReplaced Then
					' if there are no dashes left
					' the game is over and they win (message given if wins)
					' or will continue the game because
					' there are dashes left 
					If Me.xWordLabel.Text.Contains("-") = False Then
						isGameOver = True
						MessageBox.Show("Great job! You are right!", _
							"Hangman Game", MessageBoxButtons.OK, _
							MessageBoxIcon.Information)
					Else
						isDashReplaced = False
					End If
				Else
					' displays wrong guesses in xIncorrectLabel
					' also shows part of the picture box when wrong
					' when 10 guesses are wrong, player 2 loses and
					' message displayed
					Me.xIncorrectLabel.Text = _
						Me.xIncorrectLabel.Text & " " & letter
					incorrectCounter = incorrectCounter + 1
					Select Case incorrectCounter
						Case 1
							Me.xBottomPictureBox.Visible = True
						Case 2
							Me.xPostPictureBox.Visible = True
						Case 3
							Me.xTopPictureBox.Visible = True
						Case 4
							Me.xRopePictureBox.Visible = True
						Case 5
							Me.xHeadPictureBox.Visible = True
						Case 6
							Me.xBodyPictureBox.Visible = True
						Case 7
							Me.xRightArmPictureBox.Visible = True
						Case 8
							Me.xLeftArmPictureBox.Visible = True
						Case 9
							Me.xRightLegPictureBox.Visible = True
						Case 10
							Me.xLeftLegPictureBox.Visible = True
							isGameOver = True
							MessageBox.Show("Sorry, the word is " _
								& word & ".", "Hangman Game", _
								MessageBoxButtons.OK, _
								MessageBoxIcon.Information)
					End Select
				End If
			Loop
			' message displayed when anything else besides a letter is used to create the word 
		Else
			MessageBox.Show("The word can only contain letters of the alphabet. Please start over.", _
						"Hangman Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
		End If
	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: VB 2005 Hangman Game Issue

#2 Aurel300   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: VB 2005 Hangman Game Issue

Posted 15 August 2009 - 01:47 AM

Well, sorry for late reply, but I will just tell you what´s the problem.

The problem is in this line:

Do While isAllLetters AndAlso wordIndex < 10



You have to replace it with...

Do While isAllLetters AndAlso wordIndex < word.Lenght



That should do it.
Was This Post Helpful? 0
  • +
  • -

#3 rjarman65   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 09

Re: VB 2005 Hangman Game Issue

Posted 15 August 2009 - 03:48 PM

That was too simple but you were right! Thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1