Finding a Certain Letter

Hi all,

I am a student working on a hangman game for my final exam and I was having some issues with my string manipulation. As you know, in hangman, if there are two or more of one letter, and that letter is chosen, every position that holds that letter is enabled. I was having some issues with the length of my code regarding this.
For a two letter word, I have it set it up in a control set so that

//other code up here elseif (len(app.SecretWord) = 2) then if (me.caption = left(app.SecretWord, 1)) then lb_firstLetter.Text = me.caption elseif (me.caption = mid(app.SecretWord, 2)) then lb_secondLetter.Text = me.caption elseif (me.caption <> left(app.SecretWord, 1)) or (me.caption <> mid(app.SecretWord, 2)) then MsgBox("error - check your code") end if if (left(app.SecretWord, 1) = mid(app.SecretWord, 2, 1)) and(me.caption = left(app.SecretWord, 1)) and (me.caption = mid(app.SecretWord, 2, 1)) then lb_firstLetter.text = me.caption lb_secondLetter.text = me.caption end if
Whereas app.SecretWord is an app property that the user input
The problem with my code is that when I have words that are longer, such as words with a length of 10, I would have to put in many more lines of code. Additionally, there could be more than two of the same letter, creating even more combinations and more lines of code.
Ultimately, my question is: Is there some way to manipulate the string so that it picks out a certain letter from my app property?

Have a peek at INSTR
That one you can use to find every letter in whatever string a person enters

Better:

Setup:

make a custom canvas with a boolean ShowLetter and a string property LetterToShow
In the paint event draw the letter if ShowLetter is true
Add one custom canvas left of the window (make sure it stays out of the way)
Add an array of CustomCanvas to your window
Add the custom canvas to the array -> it has the index 0 !!!

Take a word.
Use split to turn it into an array of string
Iterate over the array: for each letter add a CustomCanvas to the window and set its LetterToShow property to the letter
Add each CustomCanvas to the array -> has index 1 to the end !!!

Play:

User chooses letter

Iterate over the CustomCanvas array from 1 !!! to the end.
If letter = LetterToShow then set boolean ShowLetter to true

Invalidate the canvas

@Norman Palardy I took a look at InStr, but I am having trouble understanding how I would equate an InStr integer to a letter to be displayed.

Whenever the player guesses a letter, append it to a string that contains all of the guessed letters so far:

guessedLetters = guessedLetters + newGuess

Then, to update the displayed word, loop through the characters of the secret word one at a time

dim outputString as string = ""
for i as integer = 1 to len(secretword)
  dim currentcharacter as string = mid(secretword,i,1)
  'See if the current character is one that has been already guessed
  if instr(guessedLetters, currentcharacter)>0 then
    'Yes so append it to the output
    outputString = outputString + currentcharacter
  else
    'No, hasn't been guessed, so append a hyphen to the output
    outputString = outputString + "-"
  end if
next
' Now check if all the letters have been found
if outputString = secretword then
  MsgBox "You Win!!!"
end if

I haven’t tested this, so there may be bugs.

If your application is slow, you can change these:

  1. Never compute the len of something in the For line of a loop…

[code]Dim secretWordLen As Integer
Dim i As Integer

For i = 1 To Len(secretWordLen)[/code]

  1. String Concatenation is slow, so avoid:
   outputString = outputString + currentcharacter

Prefer the better:

Dim outputString() As String outputString.Append currentcharacter

When done / when you want to use the string you store, use

   outputString.Join() // Check if () is mandatory