Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Need help in this game..


Hi everyone,

I am trying to develop "Hangman" game using VB.NET. Stars will be used to introduce the number of the letters in the word, and the application will have 26 buttons for the alphabets letters.

The problem is that when the word has the same letter in more than one place

for example the "a" in "Natasha"

and the user click on the letter "a", only the second "a" ,in position 4, appears...I want the "a" to be displayed in all the positions where it appears in the word..

this is part of my code

---------------------------

        Else 'replace the stars with letters guessed correctly

            userGuess = lblWord.Text
            begin = userGuess.Substring(0, letPosition - 1)
            end = useGuess.Substring(letPosition)
            userGuess = begin & sletter & end
            lblWord.Text = useGuess

        End If

---------------------------------------
0
swet
Asked:
swet
  • 4
  • 3
  • 2
2 Solutions
 
razoCommented:
u should loop on the string to find all a
u can use
s="natasha"

dim startindex=0
while startindex<s.length
startindex=s.indexof(userGuess,startindex)
if startindex<>-1 then
'start index give u an occurance
startinedx=startinedx+1
else
exit while ' u got all occurances
end if
end while
0
 
swetAuthor Commented:

thank you a lot,

but Im still having trouble with this..the word is coming (being read) from a file

----

Else 'replace star with letter guessed correctly


            userGuess = lblWord.Text

            While startIndex < sWord.Length
                startIndex = sWord.IndexOf(userGuess, startIndex)
                If startIndex <> -1 Then
                    begin = user_guess.Substring(0, letPosition - 1)
                    end = user_guess.Substring(letPosition)
                    user_guess = begin & sletter & end
                    lblWord.Text = userGuess
                    startIndex = startIndex + 1
                Else
                    Exit While ' u got all occurances
                End If
            End While

--------
0
 
arif_eqbalCommented:
Well I think you need to go in Loop to check for all ocuurances of the user selected character try this



        Dim Answer As String = "Natasha"
        Dim Show As String = "*******" 'As many stars as no of chars in Answer create it dynamically
        Dim UserGuess As String = "a" 'Would come from textBox
        Dim Index As Integer = Answer.IndexOf(UserGuess)
        While (Index > -1)
            Mid(Show, Index + 1, 1) = UserGuess
            Index = Answer.IndexOf(UserGuess, Index + 1)
        End While
        MessageBox.Show(Show) 'Now Show contains a string with Stars replaced with 'a' as in Answer string

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
razoCommented:
 can u explain what userguess,lblword,let position are?
0
 
swetAuthor Commented:

lblWord is a label which has the stars - userGuess is a string which will have the letter guessed by the user and the beginning and the ending of the word - letposition is the position of the letter guessed by the user
0
 
razoCommented:
but at the beginning u r setting userguess to lblword.text
and u cant use letposition we need to know the character so we can search in all the string
can u post all ur code?
or just use this as an algo
1.get the character entered by the user
2. find the index of all occurances using the while loop
3. for each occurance change the * into the charcter by using
lbltext.text.chars(position)=character
0
 
arif_eqbalCommented:
I think there's a problem with

 begin = user_guess.Substring(0, letPosition - 1)
 end = user_guess.Substring(letPosition)

User_Guess will be a single Char right???

I have modified my code sent earlier using your variables

   
        LblWord.Text = Space(sWord.Length)
        LblWord.Text = LblWord.Text.Replace(" ", "*")

        Dim Index As Integer = sWord.IndexOf(UserGuess)
        While (Index > -1)
            Mid(LblWord.Text, Index + 1, 1) = UserGuess
            Index = sWord.IndexOf(UserGuess, Index + 1)
        End While


       
0
 
swetAuthor Commented:

thank you all I used your logic and it works

now I have another question

I am reading the mystery words from a txt file using this method

----
        Dim randomWord As Random = New Random
        num = randomWord.Next(0, num - 1)
        sWord = Line(num)
        lstWord.Items.Add(sWord)
        lenWord = sWord.Length
        Return sWord
----

I want the computer to choose the word randomly only once..so the user can see the same word twice..can any one please help me with this..
0
 
razoCommented:
well i think random means there is a very small probability to choose the same word twice
anyways if that doesnt answer ur question i suggest u post this as a new question so u can get feedback from all members
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now