Hangman Game Letter replace problem

the dash's change

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 8088 Views - Last Post: 15 August 2007 - 03:35 PM Rate Topic: -----

#1 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Hangman Game Letter replace problem

Post icon  Posted 12 August 2007 - 02:50 AM

ok i made a hangman game. you press the new game in the menu and it asked for a 5-10 letter word. it auto makes how many dash's instead of letters according to the number of letter you enter for the word. that works fine, but when guessing the letters and u get to 5 letters it takes the rest of the dash's away for some reason and wont let you get the whole word. heres my code
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
		'simulates the hangman game

		Dim word As String
		Dim letter As String
		Dim isDashReplaced As Boolean
		Dim isGameOver As Boolean
		Dim incorrectCounter As Integer

		'hide 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 5 to 10-letter word from first player
		Do
			word = InputBox("Enter a 5 to 10-letter word:", "Hangman Game")
		Loop Until word.Length >= 5 Or word.Length = 10

		'convert the word to uppercase
		word = word.ToUpper

		'display 5-10 dashes in the xWordLabel
		'clear the xIncorrectLabel
		If 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

		'allow the second player to guess a letter
		'the game is over when the second player
		'either guesses the word or makes 10
		'incorrect guesses
		Do Until isGameOver


			'get a letter from second player, then
			'convert the letter to uppercase
			letter = InputBox("Enter a letter:", _
							"Letter", "", 450, 400)
			letter = letter.ToUpper

			'search the word for the letter
			For indexnum As Integer = 0 To 4
				'if the word contains the letter, then
				'replace the dash in the xWordLabel and
				'assign True to isDashRelaced to
				'indicate that a replacement was made
				If word.Substring(indexnum, 1) = letter Then
					Mid(Me.xWordLabel.Text, indexnum + 1) _
					= letter
					isDashReplaced = True
				End If
			Next indexnum

			'determine whether a dash was replaced
			If isDashReplaced Then
				'if the word does not contain any dashes,
				'the game is over because the second
				'player guessed the word; otherwise, reset
				'the isDashreplaced variabls for the next
				'search
				If Me.xWordLabel.Text.Contains("-") = False Then
					isGameOver = True
					MessageBox.Show("Great guessing!", _
						"Hangman Game", MessageBoxButtons.OK, _
						MessageBoxIcon.Information)
				Else
					isDashReplaced = False
				End If

			Else	'processed when no dash was replaced
				'display the incorrect letter, then update
				'the incorrect letter, then update
				'the appropriated picture box
				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
	End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Hangman Game Letter replace problem

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 09:21 AM

his could be part of your problem, you're only searching through 5 characters, not the length of the word

'search the word for the letter
	  For indexnum As Integer = 0 To 4
			'if the word contains the letter, then
			 'replace the dash in the xWordLabel and
			  'assign True to isDashRelaced to
			  'indicate that a replacement was made
			   f word.Substring(indexnum, 1) = letter Then
					Mid(Me.xWordLabel.Text, indexnum + 1) _
					= letter
					isDashReplaced = True
			   End If
	   Next indexnum


Was This Post Helpful? 0
  • +
  • -

#3 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 07:54 PM

so how would i write the code to correspond with how ever many letters are written for the word? would i do something like if, else ... or case, or i dunno
Was This Post Helpful? 0
  • +
  • -

#4 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 08:05 PM

sorry i couldnt get back earlier i had a basketball game to attend then work following...
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 08:10 PM

I would make word a Global variable

Private word As String



Then when you set the length when starting a new game

'get a 5 to 10-letter word from first player
Do
	word = InputBox("Enter a 5 to 10-letter word:", "Hangman Game")
Loop Until word.Length >= 5 Or word.Length = 10

'convert the word to uppercase
word = word.ToUpper




Now that variable is set and can be accessed in your other methods. So then, you can loop through the actual length of the word

'search the word for the letter
For indexnum As Integer = 0 To word.Length 'since its a global you can reference it here
	  'if the word contains the letter, then
	   'replace the dash in the xWordLabel and
	   'assign True to isDashRelaced to
		'indicate that a replacement was made
		if word.Substring(indexnum, 1) = letter Then
			  Mid(Me.xWordLabel.Text, indexnum + 1) = letter
			   isDashReplaced = True
		 End If
Next indexnum



View PostTophCoder, on 12 Aug, 2007 - 08:05 PM, said:

sorry i couldnt get back earlier i had a basketball game to attend then work following...


Its ok bro, I was just having a bad day (VERY bad day) :crazy: and DIC is my escape, except there wasn't any activity so I had no way of escaping.
Was This Post Helpful? 0
  • +
  • -

#6 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 08:16 PM

thanks again for the help. i think your the only one who helps me :)

u wouldnt have ne photoshop tutorials on siganture making would ya? hehe
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 08:20 PM

No but Ill see if I can find one :P
Was This Post Helpful? 0
  • +
  • -

#8 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 09:37 PM

ok so im tryin to code this game still ha. i used what u provided. i put the word im using in, and start guessing but it doesnt show any right or wrong words and comes up with an error on this part of the code...
'search the word for the letter
For indexnum As Integer = 0 To word.Length 'since its a global you can reference it here
	  'if the word contains the letter, then
	   'replace the dash in the xWordLabel and
	   'assign True to isDashRelaced to
		'indicate that a replacement was made
error here-->if word.Substring(indexnum, 1) = letter Then<------------error here
			  Mid(Me.xWordLabel.Text, indexnum + 1) = letter
			   isDashReplaced = True
		 End If
Next indexnum


this is the error.

Index and length must refer to a location within the string. Parameter name: length

This post has been edited by TophCoder: 12 August 2007 - 09:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 09:44 PM

Sometimes I forget if strings are 0 or 1 based :crazy:

try this

'search the word for the letter
For indexnum As Integer = 0 To word.Length - 1 'since its a global you can reference it here
	  'if the word contains the letter, then
	   'replace the dash in the xWordLabel and
	   'assign True to isDashRelaced to
		'indicate that a replacement was made
error here-->if word.Substring(indexnum, 1) = letter Then<------------error here
			  Mid(Me.xWordLabel.Text, indexnum + 1) = letter
			   isDashReplaced = True
		 End If
Next indexnum


Was This Post Helpful? 0
  • +
  • -

#10 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 12 August 2007 - 10:02 PM

well that got rid of that problem, but i have another. Its not replacing the - with the letter. even if its the right one it says wrong letter. and when its done its saying the word is ----- haha
Was This Post Helpful? 0
  • +
  • -

#11 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 13 August 2007 - 07:45 PM

u still with me here, did u understand what i was sayin?
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Hangman Game Letter replace problem

Posted 13 August 2007 - 07:56 PM

Yeah I understood, been busy trying to figure something out for work (FINALLY got it working today). Post your updated code here so I can look at it :)
Was This Post Helpful? 0
  • +
  • -

#16 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 13 August 2007 - 08:12 PM

 
	Private Sub xFileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xFileNewMenuItem.Click
		'simulates the hangman game

		Dim letter As String
		Dim isDashReplaced As Boolean
		Dim isGameOver As Boolean
		Dim incorrectCounter As Integer

		'hide 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 5 to 10-letter word from first player
		Do
			word = InputBox("Enter a 5 to 10-letter word:", "Hangman Game")
		Loop Until word.Length >= 5 Or word.Length = 10

		'convert the word to uppercase
		word = word.ToUpper

		'display 5-10 dashes in the xWordLabel
		'clear the xIncorrectLabel

		If 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

		'allow the second player to guess a letter
		'the game is over when the second player
		'either guesses the word or makes 10
		'incorrect guesses
		Do Until isGameOver


			'get a letter from second player, then
			'convert the letter to uppercase
			letter = InputBox("Enter a letter:", _
							"Letter", " ", 450, 400)
			letter = letter.ToUpper

			'search the word for the letter
			For indexnum As Integer = 0 To word.Length - 1
				'if the word contains the letter, then
				'replace the dash in the xWordLabel and
				'assign True to isDashRelaced to
				'indicate that a replacement was made
				If word.Substring(indexnum, 1) = letter Then
					Mid(Me.xWordLabel.Text, indexnum + 1) = letter
					isDashReplaced = True
				End If
			Next indexnum

			'determine whether a dash was replaced
			If isDashReplaced Then
				'if the word does not contain any dashes,
				'the game is over because the second
				'player guessed the word; otherwise, reset
				'the isDashreplaced variabls for the next
				'search
				If Me.xWordLabel.Text.Contains("-") = False Then
					isGameOver = True
					MessageBox.Show("Great guessing!", _
						"Hangman Game", MessageBoxButtons.OK, _
						MessageBoxIcon.Information)
				Else
					isDashReplaced = False



OK so i got it to were i put the word in, guess the letters and they show up. but when i get to letter 4 it starts taking away to many dash's and when the word is done it still only shows 5 letters. so basically when i start guessing i see how ever many letters there should be in Dashs but when i end i see 5 letters and 0 dashs no matter how big the word is.
Was This Post Helpful? 1

#17 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Hangman Game Letter replace problem

Posted 13 August 2007 - 08:22 PM

What comes after

Else
	isDashReplaced = False


Was This Post Helpful? 0
  • +
  • -

#18 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Hangman Game Letter replace problem

Posted 13 August 2007 - 08:33 PM

Else
					isDashReplaced = False
				End If

			Else	'processed when no dash was replaced
				'display the incorrect letter, then update
				'the incorrect letter, then update
				'the appropriated picture box
				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
	End Sub 


just my hangman picture boxs for when the letter isnt in the word. and that seems to be working right.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2