Question Extremely simple hangman game

Benjoh

New member
Joined
Jan 5, 2012
Messages
1
Programming Experience
Beginner
Hey guys,

basically i have an assignment for school which is tro produce a game of hangman. I dont have to use all the buttons etc etc, it is just tobe extremely simple.
I have stored 10 words in a txt file and have got the program to read the txt file and randomly select a word from that list of 10 and display how many characters there are in that word.

This is where im stuck! Now i need to split the randomly selected word into individual characters, so when the user guesses a letter they can either be right or wrong dependant on whether it is in the word.
The user has 6 lives (6 attempts) to guess the word!

Id extremely appreciate it if someone could help me or even finish it? it only has to be so so simple!!

Thanks guys remember it is an ever so simple program!!

Ben

 
This is where im stuck! Now i need to split the randomly selected word into individual characters
String class has a Chars property that will let you access each char in the word.
 
Use either Chars or Substring

The length of a string starts counting with 1, but the Chars and Substring methods are 0-based so the first character is counted as 0.

Here is some code to show you how it works:

VB.NET:
    Sub Main()
        Dim word, ans, ch As String, x, n As Integer
        Console.Write("Enter a word:  ")
        word = Console.ReadLine
        n = word.Length
        Console.WriteLine()
        Console.WriteLine("Separate each character of your word:")
        For x = 0 To n - 1
            ch = word.Chars(x)          'Same as	ch = word.Substring(x, 1)
            Console.Write(ch & "  ")
        Next x
        Console.WriteLine()
        Console.Write("There are " & n & " letters in your word. Which one do you want to extract?  ")
        ans = Console.ReadLine()
        Integer.TryParse(ans, x)
        If x <= n Then
            ch = word.Chars(x - 1)      'Same as	ch = word.Substring(x - 1, 1)
            Console.WriteLine("Letter # " & x & " is " & ch)
        End If
        Console.ReadLine()
    End Sub

PS: Just because the problem is "simple" for experienced programmers, don't expect someone else to do your homework for you. If it's that simple, you should be able to work it out for yourself. The above sample is a Console application you can run to help you understand how to use the Substring and Chars string methods. The rest is up to you.
 
Last edited:
Back
Top