Solved

Need help in this game..

Posted on 2004-10-18
9
171 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
  • 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
 
LVL 8

Expert Comment

by:razo
ID: 12345163
 can u explain what userguess,lblword,let position are?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 150 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Showdialog 8 29
How do I return the next Alpha Character when a specific text string is found? 4 36
Saveas need to save a copy 16 26
VB.NET Repostiory Pattern 7 13
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now