Re: Help with Hangman Prog.
I would suggest that you create a Char array that is the same length as the word your user is trying to guess. Then you can simply place the characters into the array as the letters are guessed correctly. After each correct guess you would turn the array into a string and display it in your label:
VB Code:
'Declare the variables.
Private wordToGuess As String 'Put word to guess here.
Private lettersGuessed As Char()
VB Code:
'Refresh the array each time you start a new word.
lettersGuessed = New String("_"c, wordToGuess.Length).ToCharArray()
VB Code:
'Display the letters guessed so far on a correct guess.
Dim guess As Char 'Store current guess here.
For i As Integer = wordToGuess.LastIndexOf(guess) To 0 Step -1
If wordToGuess.Chars(i) = guess Then
lettersGuessed(i) = guess
End If
Next i
myLabel.Text = New String(lettersGuessed)
Note that if the word to guess does not contain the character just guessed then LastIndexOf will return -1 and the code inside the For loop will never be executed. If the character is present then that loop will start at the last occurrence and work backwards replacing the underscore at the correct index each time. This is not the least intricate code but it is pretty efficient I think. Feel free to ask if you don't understand any part of it.
Re: Help with Hangman Prog.
so where in my codes do i put the for loop in? Also, where did you get the "i", am i suppose to replace the "i" with something else?
Also, in the direction it said im suppose to use the functions space(string) and Mid(string, integer, integer). Is there a way I can use those functions to peform the same action?
Re: Help with Hangman Prog.
You should tell your teacher that it is obvious that they have a VB6 background because they are teaching you to code like a VB6 developer. Space and Mid can still be used but they are obsolete. No new developer should be being taught their use over their .NET equivalents. There is so much VB6-like code in there it is scary. If that's what you've got to do then I guess that's what you've got to do though.
Given that this is homework I'm not really prepared to give too much actual code, but I'll tell you how those functions you've been told to use relate to my code. The Space function simply creates a new string object that contains the specified number of spaces. It is equivalent to "New String(" "c, length)" in my code, although I used underscores instead of spaces. The Mid function returns a substring from a string. This part of my code:is equivalent to:except that Mid returns a String rather than a Char. Strictly the equivalent of that would be:
VB Code:
wordToGuess.Substring(i, 1)
The i is just the loop counter and indicates how many times you've been through the loop.
If you're game to, you can tell your teacher from me that it disappoints me to see new VB.NET students being taught to code like VB6 developers.
Re: Help with Hangman Prog.
Bring him a few pages of print-outs from MSDN of the commands. He'll see right away that Microsoft is advocating their use, rather than trying to undermine his authority. He might stand his ground, but he'll think about changing things in the future.
Re: Help with Hangman Prog.
This class is an intro into programming so maybe thats why he's only teaching the basic stuff. I have no idea why i took this class, im such a computer illiterate so thanks for helping me out.
Re: Help with Hangman Prog.
Quote:
Originally Posted by dglienna
Bring him a few pages of print-outs from MSDN of the commands. He'll see right away that Microsoft is advocating their use, rather than trying to undermine his authority. He might stand his ground, but he'll think about changing things in the future.
Good point. I'll just get my high horse down off this soap box... The methods that you are being taught will work fine with no problems. It's just that there are newer and better ways to achieve the same results. No doubt you'll still learn a lot from the class. For the moment, it's probably better that you just stick to what your teacher tells you so as not to get confused. There'll be plenty of time to fine tune things later if you want to.