?
Solved

Need help in this game..

Posted on 2004-10-18
9
Medium Priority
?
202 Views
Last Modified: 2010-04-23

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
Comment
Question by:swet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:razo
ID: 12344940
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
 

Author Comment

by:swet
ID: 12345060

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
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12345070
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:razo
ID: 12345163
 can u explain what userguess,lblword,let position are?
0
 

Author Comment

by:swet
ID: 12345215

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
 
LVL 8

Assisted Solution

by:razo
razo earned 600 total points
ID: 12345275
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
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 400 total points
ID: 12345290
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
 

Author Comment

by:swet
ID: 12367185

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
 
LVL 8

Expert Comment

by:razo
ID: 12367254
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question