Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help in this game..

Posted on 2004-10-18
9
Medium Priority
?
208 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

597 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