Gday i already asked for help in the vb.net forum,im new so im guessing i was in the wrong forum as i didnt receive much help.ive posted all the original text from the thread below.

G day ,im a parent from Australia.
My child is 7 turning 8 and i would like to create a simple hangman game in visual basic that he can use.

id please like to know the logistics of making the game.
This is how id like it to work,ive investigated different games....
i would like it to have a text box where you enter a single letter into it at any time and then obviously if its wrong, the wrong letter disappears, no longer being able to click.

Would someone please be able to think of the simplest possible way for me to do this,its important for me to not have it done by someone else and to make this game for my son myself.
I would like to be able to change/add the words in the game in the code,so that my son wont get bored

this is my first problem and i have heard this is a very friendly board,if you can assist me then thank you very much!

id just like to clarify that id like to solely use VB to make the program.i think ill just go into the code and change/add new words when my son gets bored etc.

Id like for the game to be really simple ;with the words being randomly selected ,each letter of the random word being part of an array,and having the images of the hangman appear when a guessed letter matches one of the letters in the array.

im guessing part of the code would be something like this..

Help with Code Tags
(Toggle Plain Text)

If text1 = text2
Then Image.Show

If text1 = text2 Then Image.Show

i have also written some pseudo code to start it off
Help with Code Tags
(Toggle Plain Text)

fullword = "jungle";
word = ("j","u","n","g","l","e");
player = ("-","-","-","-","-","-");

while (win = false) {
     print "So far, you have.. " + player;
     print "What is your next guess? ";
     input guess;

     for (i = 0; i < 6; i++) {
          if (guess = word[i]) {
               player[i] = guess;
          }
     }

     checker = player[0] + player[1] + player[2] + player[3] + player[4] + player[5];
     if (checker = word[6]) {
          win = true;
     }
}

print "You win!";

fullword = "jungle"; word = ("j","u","n","g","l","e"); player = ("-","-","-","-","-","-"); while (win = false) { print "So far, you have.. " + player; print "What is your next guess? "; input guess; for (i = 0; i < 6; i++) { if (guess = word[i]) { player[i] = guess; } } checker = player[0] + player[1] + player[2] + player[3] + player[4] + player[5]; if (checker = word[6]) { win = true; } } print "You win!";

i have got a visual basic book from a library and it has all the basics on everything ,but im still stumped as how to start things off in terms of actually writing the code.What things in the book should i focus on in this type of game???

as you can see from my pseudo code,each letter from the word will be split up into an array.

i know most of you are experts at visual basic etc. but im just a novice please assist me with what ive done so far,thank you!

Recommended Answers

All 28 Replies

im guessing part of the code would be something like this..

Help with Code Tags
(Toggle Plain Text)

If text1 = text2
Then Image.Show

If text1 = text2 Then Image.Show

i have also written some pseudo code to start it off
Help with Code Tags
(Toggle Plain Text)

 fullword = "jungle";
word = ("j","u","n","g","l","e");
player = ("-","-","-","-","-","-");

while (win = false) {
     print "So far, you have.. " + player;
     print "What is your next guess? ";
     input guess;

     for (i = 0; i < 6; i++) {
          if (guess = word[i]) {
               player[i] = guess;
          }
          ' just add the folliwing
          else
          [B][COLOR="red"]{
            textbox.text=""
            textbox.disable=true
           }[/COLOR][/B]     }

     checker = player[0] + player[1] + player[2] + player[3] + player[4] + player[5];
     if (checker = word[6]) {
          win = true;
     }
}

print "You win!";

fullword = "jungle"; word = ("j","u","n","g","l","e"); player = ("-","-","-","-","-","-"); while (win = false) { print "So far, you have.. " + player; print "What is your next guess? "; input guess; for (i = 0; i < 6; i++) { if (guess = word[i]) { player[i] = guess; } } checker = player[0] + player[1] + player[2] + player[3] + player[4] + player[5]; if (checker = word[6]) { win = true; } } print "You win!";

i have got a visual basic book from a library and it has all the basics on everything ,but im still stumped as how to start things off in terms of actually writing the code.What things in the book should i focus on in this type of game???

as you can see from my pseudo code,each letter from the word will be split up into an array.

i know most of you are experts at visual basic etc. but im just a novice please assist me with what ive done so far,thank you!

im guessing part of the code would be something like this..

Help with Code Tags
(Toggle Plain Text)

If text1 = text2
Then Image.Show

If text1 = text2 Then Image.Show

i have also written some pseudo code to start it off
Help with Code Tags
(Toggle Plain Text)

 fullword = "jungle";
word = ("j","u","n","g","l","e");
player = ("-","-","-","-","-","-");

while (win = false) {
     print "So far, you have.. " + player;
     print "What is your next guess? ";
     input guess;

     for (i = 0; i < 6; i++) {
          if (guess = word[i]) {
               player[i] = guess;
          }
          ' just add the folliwing
          else
          [B][COLOR="red"]{
            textbox.text=""
            textbox.disable=true
            break;

           }[/COLOR][/B]     }

     checker = player[0] + player[1] + player[2] + player[3] + player[4] + player[5];
     if (checker = word[6]) {
          win = true;
     }
}

print "You win!";

Your code should be like

counter = 0
While  (win = false)
  print "So far, you have.. " + player;
  print "What is your next guess? ";
  input guess;
  if counter < word.length {
         if (guess = word[counter]) {
               player[counter] = guess;
          }
           else
          {
            textbox.text=""
            textbox.disable=true
            break;
            win=true
             }
          }
      else 
         {
          break;
         }
      counter =  counter + 1
}
If counter = word.length
{
  print "you win"
}

That doesn't look like Classic VB (4/5/6) to me...
Is it .NET?

^^^i dont know ,all i know is that i need help with making a Hangman game for my 7 year old kid and its in microsoft visual basic 6.0 ,can someone please help me, i asked for help a week ago and still have recieved none!!
no1 has guided me to the correct forums or anything !!

Sorry, left my eyes in bed yesterday... I just re-read through and saw you did say psuedo code... duh @ me :(

For information, this is the right forum for VB6.

I'll have a read through it tomorrow and see if I can come up with something for you. It's just after 1am here now and I've had a few drinks, so chances are I'd fail miserably if I tried coding now! :D

Sorry for my earlier dozy post...

Hm, can't edit my last post... I presume it was a while ago at least.

Apologies if this is a bit rough around the edges, but as I said before, I am a little rough around the edges myself at the moment *hic* ;)

I've got the concept working, I'll have another look tomorrow evening, but it should get you started.

I've created a form which contains the following:
2x labels: Label1 and lblGuesses
3x text boxes: txtWord, txtGuessWord and txtLetterGuess
2x command buttons: Command1 and Command2

My source:

Option Explicit

Private strWord As String
Private strGuessLetter As String
Private arrWord As Variant
Private arrLetters As Variant
Private intTotalLetters As Integer

Private Sub Command1_Click()
    Dim i As Integer

    strWord = txtGuessWord.Text
    txtGuessWord.Text = "-"
    
    intTotalLetters = Len(strWord)
    
    ReDim arrWord(intTotalLetters) As String
    ReDim arrLetters(intTotalLetters) As String
    
    For i = 1 To intTotalLetters
        arrWord(i) = Mid(strWord, i, 1)
        arrLetters(i) = "-"
        txtWord.Text = txtWord.Text & arrLetters(i) & " "
    Next
    
    
    
End Sub

Private Sub Command2_Click()
    Dim i As Integer, o As Integer
    Dim blnGuess As Boolean
    
    strGuessLetter = txtLetterGuess.Text
    txtLetterGuess.Text = vbNullString
    
    For i = 1 To intTotalLetters
        If arrWord(i) = strGuessLetter Then
            arrLetters(i) = strGuessLetter
            txtWord.Text = vbNullString
            For o = 1 To intTotalLetters
                txtWord.Text = txtWord.Text & arrLetters(o) & " "
            Next
            blnGuess = True
        Else
            If blnGuess Then
                'nothing
            Else
                'letter is not in list
                blnGuess = False
            End If
        End If
    Next
    If blnGuess = False Then
        lblGuesses.Caption = lblGuesses.Caption & " " & strGuessLetter
    End If
    
    If InStr(1, txtWord.Text, "-") = 0 Then
        Label1.Caption = "winner!"
    Else
        Label1.Caption = "enter your next letter"
    End If
End Sub

Basically, the idea is you enter a word into txtGuessWord (while the player is not looking - there could be alternative methods of getting a new word in... this is just a concept for now) and click Command1.
This will populate the txtWord field with hyphens ("-").
Enter a letter in txtLetterGuess and press Command2, it will check the array (arrWord) which contains the full word, and if it exists, it will update txtWord and another array containing the revealed word so far (arrLetters).
If the letter does not exist, it will mark it in lblGuesses (I used a font with strike-through for this label to show it does not exist - you may choose to do this differently)

Label1 will update to ask for the next letter until all letters have been revealed when it says "winner!".


Hope that helps a bit, I will look into it in more detail tomorrow when I get chance and should be in a position to post some updated code.

^^thank u so far im trying it myself now,ill post later

This actually got me thinking... and gave me something to try out.
To be honest, it will come in extremely useful for my son to play on when he gets a little older :)

I've completed it with a bit of a hangman aswell.

Again, perhaps not the cleanest code, I would have used a control array for the shapes but for the head being a circle, but the rest being lines so it wouldn't work hence the select statement.

I've attached the project on here rather than posting more code (as I've added the hangman on and relabelled some of the control objects).

If it is what you are looking for and it solves your problem, please mark the thread as solved to boost my ego/make my head bigger ;)


Any problems, give me a shout.
Jon

^^gr8 job so far,but there are a few things that could be improved ,id do them myself but im a novice...
is it possible to not be able to type into the top text box,as i dont think its neccessary because it can be wiped etc.
i think it would be better if there was a button that can clear the text box.Maybe it should be a label instead so there is no interference from users.

ummm how would u change the code so that u can only enter in one letter at a time??
for instance if the word is "jungle" and my son enters in the letters "ja" it is seen as an incorrect guess and there are strikes throughout the letters "Ja" so naturally my son would think that the two letters are incorrect.
Also,if u enter in a word ,more letters can be added to that word in the "enter word" text box making unreal words that would be impossible to guess,this can confuse my son and may create many problems , i think...

Also only letters should be able to be entered other wise,numbers and symbols could be in the word and could potentially confuse my son and urs...

You could lock the top text field ("Locked" in the properties when looking at the form in VB) to stop the text field being edited.
Needless to say, if a correct character is entered after the top box is changed, it would reset it to what it should be anyway. Changing what is in that box would not give you the ability to cheat, put it that way.
If you preferred a label, it would be easy to change it to that. Just update the code so anything which has "txtWord.Text" in, it is changed to "Label1.Caption" ("Label1" can be changed to whatever you named your label).


Unsure how you would stop people entering more than one letter in the guess field. I suppose you could use

Left(txtLetterGuess.Text, 1)

when getting the letter that the user entered. That would then only take the first letter entered, and would only put one in the wrong guesses label.
You could put in a check for if the entered character is a letter or if it is a number or other symbol and alert the user to a wrongly entered character.

Someone may know a better way of doing this though... fingers crossed someone sees this thread and can help with this part.


Finally, I get an email alert when a thread I have participated in has been updated, so you don't need to send me a PM to let me know if you have added something new :)

Mr. jonifen u have a good knowledge of VB. its gr8. try to make the game more interesting. you have made the hangman to appear letter by letter. ok. if the word is only 4 letters long? what if the word would have to be 7 letters long? i am also trying out a little bit on this. u can plz find some better soln very soon from me.


regards,
Shaik Akthar

To limit the entry of more than one letter, just set the maxlength property of the textbox to 1.

to limit the entry of characters from "a through z" only use the
TextBox_KeyPress() event.

aktharshaik,

I had made my version to work with any length of word. What I didn't take into account was multiple words (to form phrases etc.) - I only really noticed that when I looked at the URL you posted for a Hangman game in complete form (which I have to say is an excellent example :) Thanks for sharing that link).

Finally, thanks for the advice on the maximum length of text boxes. I'd never used that functionality before, but cannot believe it is so simple to put in place!


Thanks again,

Jon

thank u guys for the help so far im currently attempting a hangman game for my son which is 1 player with preset words.Its all good so far,ill post it wen its completed.

But i need help with...

making a label that shows the amount of guesses,and one that shows the correct guesses
any tips??

For capturing good/wrong guesses, you could piggy back onto the code which either draws another limb onto the Hangman, or updates the text boxes for a valid guess.

There is something similar in the code which aktharshik linked us to which tracks the amount of games played, and the amount won. You could use similar functionality for the guesses.

cool thanx so far,im keen for new ways for me to improve the game ,any suggestions??

i have fixed the problem already ,don't worry guys!

I am still updating the game so keep checking back !!

Ive just hit a wall in the game that a user submitted here ,ive made some changes to it such as not being able to interfere with the label at the top,not being able to type in the guess box until a word is entered,and ive made a "new word" button that clears the word text at the top .BUT when it is clicked,the whole game freezes and i have no idea why if anyone can help me as quicly as possible id be grateful

heres the code for the new word button and the start button

Private Sub cmdNewWord_Click()
cmdStart.Enabled = True
txtGuessWord.Enabled = True
txtWord.Caption = ""
lblGuesses = ""
frmHangman.Enabled = False
cmdGuess.Enabled = False
txtLetterGuess.Enabled = False

End Sub


'----------------------------------------------------------------------
Private Sub cmdStart_Click()
'----------------------------------------------------------------------------
    Dim i As Integer
    'start button and text box cannot be pressed and interfered with twice in a row
    cmdStart.Enabled = False
    txtGuessWord.Enabled = False
   ' Reset wrong guesses counter
    intWrongGuesses = 0
   ' Enable letter guess field and button
    txtLetterGuess.Enabled = True
    cmdGuess.Enabled = True
    txtLetterGuess.Enabled = True
    cmdGuess.Enabled = True
   ' Get word from guess word field
    strWord = txtGuessWord.Text
    txtGuessWord.Text = vbNullString
   ' Get length of word
    intTotalLetters = Len(strWord)
   ' Re-Initialise the arrays for the word and letters guessed
    ReDim arrWord(intTotalLetters) As String
    ReDim arrLetters(intTotalLetters) As String
   'Populate word field with hyphens to symbolise letters to be guessed
    For i = 1 To intTotalLetters
        arrWord(i) = Mid(strWord, i, 1)
        arrLetters(i) = "-"
        txtWord.Caption = txtWord.Caption & arrLetters(i) & " "
    Next
End Sub

and for the Guess button

'Start button cannot be pressed if guess button has been pressed
    cmdStart.Enabled = False
    'Text box cannot be interfered with if guess button has been pressed
    txtGuessWord.Enabled = False

i cant work out what is the problem,can anyone apeculate as to why the game freezes when the 'new word' button is pressed??
thanx

I noticed for your new method you did not have an End Sub and the line of hyphens above the next method header did not have a preceding apostrophe to let VB know it is a comment. I've made those changes.

You've also got a line in there saying:

frmHangman.Enabled = False

I'm unsure that is required, and could be why your game appears to freeze.

commented: thanx so far +1

have found a really interesting ,but simple hangman game on the net but i am really confused to how it actually works ,i would like to re-create it for myself but first i would need to understand it.it seems to use complicated code ,but there is much of it and the creator hasnt labeled all the pieces of code so it doesnt help.If u cud help me to disect the code then id be grateful .

The most confusing part of the code is how the creator actually got the letter and characters to appear like they do.Also would you be able to explain "CheckChar" ,"String$","UCase" , "Call" and "QuestionHandler"

http://www.sendspace.com/file/a4wye8

thanx

Hi!
String$ is used to output a string by the specified length for the given character in the second parameter.

Ex:

text1.text = String$(10,"*")

will fill the textbox with ten *

Regards
Shaik Akthar

UCASE Converts the given string to uppercase.


Ex
Dim x as string
x = "America"
text1.text = UCase(x)

here text1 will display the word in variable x but in upper case "AMERICA"

Regards
Shaik Akthar

commented: thanx +1

Call keyword is used to call any procedure by its name but its purely optional

like u can write

Call txtGuess_Change

or just

txtGuess_Change

Regards
Shaik Akthar

^^ok so u dont need to use it ???thats good thanx.

still unsolved!!!check the above problem,there have been nearly 1000 views and no-1 can help me??I will rep if someone helps with this LAST problem!!!

'Posi ' variable is used to track as when to move to the next line to print the body or arms or legs of the hangman.

when 'Posi' is 2 (i.e) when 2nd wrong guess is made, the '|' character must go to next line. hence 'Posi' is checked and if it is 2 a new line (vbCrLf) is appended.

Similarly when 'Posi' is 3 again we have to move to the next line to start the hands.
and when 'Posi' is 6 we have to move to the next line to start the legs.

Finally when 'Posi' is 8 (i.e) the hangmans last leg is to be displayed, then will be the end of the guesses because man hanging is complete with the last leg.

This is the point where the user is failed to guess the correct answer.

CheckChar function is used to check if the character typed in the guess box is present in the string pertaining to the 'intQuestionNumber'th value in the array. if not the guess is wrong. hence clear the Guess TextBox and go for the next try. If all tries are over i.e. 'Posi' is reached 8 as said above the game is lost. if before the 'Posi' reaches 8 the string the Question label with '_' matches the 'intQuestionNumber'th value in the array, the game is won.

Also u can count the wins and losses also.

Take 2 global integer variables and make then equal to 0 during form load.
One for Win Count and one for Lost Count.
The sum of these two will be Played Count.

Check out the attachment i have modified.

Some other suggestions
================
1. Change the words now and then
or
2. increase the number of words
or
3. put the strings in a text file and read the values from them

No 3. is better because u need not compile ur project again and again whenever u change the words.

Regards
Shaik Akthar

commented: thanx a lot !!! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.