Solved

Need help in this game..

Posted on 2004-10-18
9
161 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
 can u explain what userguess,lblword,let position are?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:swet
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

772 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

10 Experts available now in Live!

Get 1:1 Help Now