Help with Hangman

I need help with code for hangman

Page 1 of 1

3 Replies - 1264 Views - Last Post: 16 December 2009 - 02:12 AM Rate Topic: -----

#1 ineedhelpwithcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-November 09

Help with Hangman

Posted 14 December 2009 - 07:03 PM

Hello, i have mostly completed my visual basic hangman game. I just need some help with a few more things to complete it. I just need to know how to get the different body parts to appear one at a time in correspondence with the number of guesses. Whenever i guess a letter wrong all my images appear instead of one at a time. I need help fixing this one little flaw. I will post my code below.

Dim wordlist(20) As String
Dim hintlist(20) As String
Dim randomNumber, lengthword As Integer
Dim letter As String

Private Sub Check1_Click()
If Check1.Value = 1 Then
MsgBox (hintlist(randomNumber))
End If
Check1.Value = 0

Check1.Visible = False

End Sub

Private Sub cmdE_Click()
End
End Sub




Private Sub cmdNW_Click()
Check1.Visible = True

For y = 0 To 11
Text1(y).Visible = False
Text1(y).Text = ""
Next y



randomNumber = Int(Rnd * 20) 'eventually needs to be changed
Print randomNumber, wordlist(randomNumber)
lengthword = Len(wordlist(randomNumber))
'show hint

For y = 0 To lengthword - 1
Text1(y).Visible = True
Next y
 p = InStr(1, wordlist(randomNumber), " ")
 If p <> 0 Then
Text1(p - 1).Text = " "
 End If
End Sub

Private Sub command1_click(Index As Integer)
letter = Command1(Index).Caption
Call checkletter

End Sub


Private Sub Form_Load()
Randomize

wordlist(0) = "pencil sharpener"
wordlist(1) = "workbook"
wordlist(2) = "ruler"
wordlist(3) = "calculator"
wordlist(4) = "binder"
wordlist(5) = "notebook"
wordlist(6) = "pencil"
wordlist(7) = "pen"
wordlist(8) = "textbook"
wordlist(9) = "chalk board"
wordlist(10) = "paper"
wordlist(11) = "folder"
wordlist(12) = "desk"
wordlist(13) = "chair"
wordlist(14) = "computer"
wordlist(15) = "student"
wordlist(16) = "teacher"
wordlist(17) = "chalk"
wordlist(18) = "keyboard"
wordlist(19) = "assignment book"

hintlist(0) = "something to sharpen your pencil with"
hintlist(1) = "activities in book form"
hintlist(2) = "measures objects"
hintlist(3) = "calculates math problems"
hintlist(4) = "used to store papers or folders in"
hintlist(5) = "you take notes in this"
hintlist(6) = "you use this to write"
hintlist(7) = "you use this to write"
hintlist(8) = "gives information on a certain subject"
hintlist(9) = "teacher writes on this"
hintlist(10) = "something you do assignments on"
hintlist(11) = "you store papers in this"
hintlist(12) = "you use this to write on"
hintlist(13) = "you sit on this"
hintlist(14) = "a machine that helps you out with various activities"
hintlist(15) = "the people that teachers teach"
hintlist(16) = "the person that teaches students"
hintlist(17) = "you use this to write on the chalkboard"
hintlist(18) = "you use this to type"
hintlist(19) = "used to keep track of assignments"

End Sub




Public Sub checkletter()
Dim guess As Boolean
guess = False
Dim p, start As Integer
start = 1
p = 0

For y = 1 To lengthword
 p = InStr(start, wordlist(randomNumber), letter, 1)
 start = p + 1
	If p > 0 Then
	guess = True
	Text1(p - 1).Text = letter
	End If
Next y
'if you miss it then it will add parts to hangman add here
'If guess = False Then

'count
If guess = False Then
d = guess
For d = 1 To 6
For x = d To 1 Step -1
Select Case x
Case 1
Image1.Visible = True
Case 2
Image2.Visible = True
Case 3
Image3.Visible = True
Case 4
Image4.Visible = True
Case 5
Image5.Visible = True
Case 6
Image6.Visible = True
End Select
Next x
Next d
End If


End Sub



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code: Thanks, PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Hangman

#2 FungalMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 13-December 09

Re: Help with Hangman

Posted 15 December 2009 - 06:57 AM

Now i'm normally not one to criticise, but that is some damned hard code to read...

I'll take a shot and say your "Amount of Bad Guesses" variable needs to be public and you need to modify your checkletter code as follows.

Public amountOfBadGuesses as Integer = 0	 'This goes in the General Declarations section of the Form

Public Sub checkletter()
	Dim guess As Boolean
	guess = False
	Dim p, start As Integer
	start = 1
	p = 0

	For y = 1 To lengthword
		p = InStr(start, wordlist(randomNumber), letter, 1)
		start = p + 1
		If p > 0 Then
			guess = True
			Text1(p - 1).Text = letter
		End If
	Next y

'if you miss it then it will add parts to hangman add here
'count

	If guess = False Then
		amountOfBadGuesses = amountOfBadGuesses + 1
			
		Select Case amountOfBadGuesses
			Case 1
				Image1.Visible = True
			Case 2
				Image2.Visible = True
			Case 3
				Image3.Visible = True
			Case 4
				Image4.Visible = True
			Case 5
				Image5.Visible = True
			Case 6
				Image6.Visible = True
				'Maybe Reset Amount of Bad Guesses Here?
				amountOfBadGuesses = 0
		End Select
	End If

End Sub




Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 ineedhelpwithcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-November 09

Re: Help with Hangman

Posted 15 December 2009 - 03:07 PM

Yes indeed this did solve my problem. I'm sorry that my code was hard to read but you seemed to be able to figure out what was wrong anyway so its all good. Thanks for your time and effort to help me correct my problem.


View PostFungalMonkey, on 15 Dec, 2009 - 05:57 AM, said:

Now i'm normally not one to criticise, but that is some damned hard code to read...

I'll take a shot and say your "Amount of Bad Guesses" variable needs to be public and you need to modify your checkletter code as follows.

Public amountOfBadGuesses as Integer = 0	 'This goes in the General Declarations section of the Form

Public Sub checkletter()
	Dim guess As Boolean
	guess = False
	Dim p, start As Integer
	start = 1
	p = 0

	For y = 1 To lengthword
		p = InStr(start, wordlist(randomNumber), letter, 1)
		start = p + 1
		If p > 0 Then
			guess = True
			Text1(p - 1).Text = letter
		End If
	Next y

'if you miss it then it will add parts to hangman add here
'count

	If guess = False Then
		amountOfBadGuesses = amountOfBadGuesses + 1
			
		Select Case amountOfBadGuesses
			Case 1
				Image1.Visible = True
			Case 2
				Image2.Visible = True
			Case 3
				Image3.Visible = True
			Case 4
				Image4.Visible = True
			Case 5
				Image5.Visible = True
			Case 6
				Image6.Visible = True
				'Maybe Reset Amount of Bad Guesses Here?
				amountOfBadGuesses = 0
		End Select
	End If

End Sub




Hope this helps.

Was This Post Helpful? 0
  • +
  • -

#4 FungalMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 13-December 09

Re: Help with Hangman

Posted 16 December 2009 - 02:12 AM

Sorry about the criticism, it was a Loooong Monday! lol

Glad I could help, please feel free to click my "Helpful post link" I'm new to DIC!!

Regards,


View Postineedhelpwithcode, on 15 Dec, 2009 - 02:07 PM, said:

Yes indeed this did solve my problem. I'm sorry that my code was hard to read but you seemed to be able to figure out what was wrong anyway so its all good. Thanks for your time and effort to help me correct my problem.

This post has been edited by FungalMonkey: 16 December 2009 - 02:13 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1