Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Can you figure it out?????


Can you figure it out?????

Author
Message
Lamborghini2diab
Lamborghini2diab
Forum God
Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)Forum God (553 reputation)

Group: Forum Members
Posts: 7, Visits: 1

I am making a Hangman game and it has gone bizzerk. Can you help figure out the last part to fix it? I need it to Randomly pick a word then put it in the TxtBox but as question marks. Then you have to guess the word and the legs,arms and finally the head will go "POOF" if you get it wrong. All the letters used go to the a "Used letter" TxtBox and the letter button become disabled. I am stuck help me if you can figure out the finally part.

~Code listed below~

 *Warning it is VERY long*  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Public Class Form1

Inherits System.Windows.Forms.Form

Dim theWord As String

Dim arrayWord(8) As String

Dim wrongGuess As Integer

Dim myrand As Integer

Dim wordcount As Integer

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

Dim arrayWord() As String

Dim myrand2 As Random

Dim myword As String

Dim x As Integer

Dim ff As Integer

wrongGuess = 0 ' reset wrong guesses

pb1.Image = ImageList1.Images(0)

' Get a unique file number

ff = FreeFile()

Dim strAnimal() As String

Dim i As Integer

' Open the file for input to read it

FileSystem.FileOpen(ff, "H:\HangMan\Animalnames.txt", OpenMode.Input)

' Add each line from the file to an arrray

Do While Not EOF(ff)

ReDim Preserve strAnimal(i)

strAnimal(i) = FileSystem.LineInput(ff)

i += 1

Loop

' Close the file

FileSystem.FileClose(ff)

ReDim arrayWord(Len(myrand))

' Initalize the random generator

Randomize()

' Get a random index

i = CType(Rnd() * (i + 1), Integer)

' Show the random line in the text box

lblWord.Text = strAnimal(i)

If Len(myrand) = 0 Then

MessageBox.Show("You must enter a word", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Information)

Exit Sub

Else

Call enableAll() ' enable all letters

End If

theWord = UCase(myrand)

theWord = (strAnimal(i))

lblWord.Text = ""

For x = 1 To Len(myrand)

lblWord.Text += "?"

arrayWord(x) = "?"

Next

btn1.Enabled = False

btn2.Enabled = False

btn3.Enabled = False

btnAgain.Enabled = True

End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click

wrongGuess = 0 ' reset wrong guesses

pb1.Image = ImageList1.Images(0)

Call enableAll() ' enable all letters

Dim x As Integer

Dim ff As Integer

Dim strComput() As String

Dim i As Integer

' Get a unique file number

ff = FreeFile()

' Open the file for input to read it

FileSystem.FileOpen(ff, "H:\HangMan\Computerterms.txt", OpenMode.Input)

' Add each line from the file to an arrray

Do While Not EOF(ff)

ReDim Preserve strComput(i)

strComput(i) = FileSystem.LineInput(ff)

i += 1

Loop

' Close the file

FileSystem.FileClose(ff)

' Initalize the random generator

Randomize()

' Get a random index

i = CType(Rnd() * (i + 1), Integer)

' Show the random line in the text box

'lblWord.Text = strComput(i)

If Len(myrand) = 0 Then

MessageBox.Show("You must enter a word", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Information)

Exit Sub

Else

Call enableAll() ' enable all letters

ReDim arrayWord(Len(myrand))

End If

theWord = (strComput(i))

lblWord.Text = ""

For x = 1 To Len(theWord)

lblWord.Text += "?"

arrayWord(x) = "?"

Next

btn1.Enabled = False

btn2.Enabled = False

btn3.Enabled = False

btnAgain.Enabled = True

End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click

wrongGuess = 0 ' reset wrong guesses

pb1.Image = ImageList1.Images(0)

Call enableAll() ' enable all letters

Dim x As Integer

Dim ff As Integer

Dim strCar() As String

Dim i As Integer

' Get a unique file number

ff = FreeFile()

' Open the file for input to read it

FileSystem.FileOpen(ff, "H:\HangMan\CarNames.txt", OpenMode.Input)

' Add each line from the file to an arrray

Do While Not EOF(ff)

ReDim Preserve strCar(i)

strCar(i) = FileSystem.LineInput(ff)

i += 1

Loop

' Close the file

FileSystem.FileClose(ff)

' Initalize the random generator

Randomize()

' Get a random index

i = CType(Rnd() * (i + 1), Integer)

' Show the random line in the text box

'lblWord.Text = strCar(i)

If Len(myrand) = 0 Then

MessageBox.Show("You must enter a word", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Information)

Exit Sub

Else

Call enableAll() ' enable all letters

ReDim arrayWord(Len(myrand))

End If

theWord = (strCar(i))

lblWord.Text = ""

For x = 1 To Len(theWord)

lblWord.Text += "?"

arrayWord(x) = "?"

Next

btn1.Enabled = False

btn2.Enabled = False

btn3.Enabled = False

End Sub

Private Sub lblWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblWord.Click

End Sub

 

Public Sub guess(ByVal letter As String)

lblWord.Text = ""

Dim goodFlag As Boolean = False

Dim x As Integer

Dim winFlag As Boolean = True

' check to see if the letter fits

For x = 1 To Len(theWord)

If letter = Mid(theWord, x, 1) Then

arrayWord(x) = letter

goodFlag = True

End If

lblWord.Text += arrayWord(x)

Next

' see if its a winner or not

For x = 1 To Len(theWord)

If "?" = arrayWord(x) Then

winFlag = False

Exit For

End If

Next

If winFlag = True Then

MessageBox.Show("YOU WIN!!!!", "A WINNER!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

btnAgain.Enabled = True

disableAll()

Exit Sub

End If

' put the letter in if correct

If goodFlag = False Then

lblWrong.Text += " " & letter

Call SubwrongGuess()

End If

End Sub

Public Sub SubwrongGuess()

wrongGuess += 1

lblWrongcount.Text = wrongGuess

If wrongGuess = 1 Then PictureBox10.Visible = False

If wrongGuess = 2 Then PictureBox9.Visible = False

If wrongGuess = 3 Then PictureBox8.Visible = False

If wrongGuess = 4 Then PictureBox7.Visible = False

If wrongGuess = 5 Then PictureBox6.Visible = False

If wrongGuess = 5 Then pb1.Image = ImageList1.Images(1)

If wrongGuess = 6 Then PictureBox5.Visible = False

If wrongGuess = 7 Then PictureBox4.Visible = False

If wrongGuess = 8 Then PictureBox3.Visible = False

If wrongGuess = 9 Then PictureBox2.Visible = False

If wrongGuess = 10 Then PictureBox1.Visible = False

If wrongGuess = 10 Then pb1.Image = ImageList1.Images(2)

If wrongGuess = 11 Then

pb1.Image = ImageList1.Images(3)

Call disableAll()

MessageBox.Show("You loose...")

End If

End Sub

Public Sub ResetAll()

btnA.Visible = True

btnB.Visible = True

btnc.Visible = True

btnD.Visible = True

btnE.Visible = True

btnF.Visible = True

btnG.Visible = True

btnH.Visible = True

btnI.Visible = True

btnJ.Visible = True

btnK.Visible = True

btnL.Visible = True

btnM.Visible = True

btnN.Visible = True

btnO.Visible = True

btnP.Visible = True

btnQ.Visible = True

btnR.Visible = True

btnS.Visible = True

btnT.Visible = True

btnU.Visible = True

btnV.Visible = True

btnW.Visible = True

btnX.Visible = True

btnY.Visible = True

btnZ.Visible = True

btnGo.Enabled = True

'txtWord.Text = ""

'txtWord.Focus()

lblWord.Text = ""

Call disableAll()

lblWrongcount.Text = "0"

lblWrong.Text = ""

pb1.Image = ImageList1.Images(0)

PictureBox1.Visible = True

PictureBox2.Visible = True

PictureBox3.Visible = True

PictureBox4.Visible = True

PictureBox5.Visible = True

PictureBox6.Visible = True

PictureBox7.Visible = True

PictureBox8.Visible = True

PictureBox9.Visible = True

PictureBox10.Visible = True

End Sub

Public Sub enableAll()

btnA.Enabled = True

btnB.Enabled = True

btnc.Enabled = True

btnD.Enabled = True

btnE.Enabled = True

btnF.Enabled = True

btnG.Enabled = True

btnH.Enabled = True

btnI.Enabled = True

btnJ.Enabled = True

btnK.Enabled = True

btnL.Enabled = True

btnM.Enabled = True

btnN.Enabled = True

btnO.Enabled = True

btnP.Enabled = True

btnQ.Enabled = True

btnR.Enabled = True

btnS.Enabled = True

btnT.Enabled = True

btnU.Enabled = True

btnV.Enabled = True

btnW.Enabled = True

btnX.Enabled = True

btnY.Enabled = True

btnZ.Enabled = True

End Sub

Public Sub disableAll()

btnA.Enabled = False

btnB.Enabled = False

btnc.Enabled = False

btnD.Enabled = False

btnE.Enabled = False

btnF.Enabled = False

btnG.Enabled = False

btnH.Enabled = False

btnI.Enabled = False

btnJ.Enabled = False

btnK.Enabled = False

btnL.Enabled = False

btnM.Enabled = False

btnN.Enabled = False

btnO.Enabled = False

btnP.Enabled = False

btnQ.Enabled = False

btnR.Enabled = False

btnS.Enabled = False

btnT.Enabled = False

btnU.Enabled = False

btnV.Enabled = False

btnW.Enabled = False

btnX.Enabled = False

btnY.Enabled = False

btnZ.Enabled = False

End Sub

Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click

guess(btnA.Text)

btnA.Visible = False

End Sub

Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click

guess(btnB.Text)

btnB.Visible = False

End Sub

Private Sub btnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnc.Click

guess(btnc.Text)

btnc.Visible = False

End Sub

Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click

guess(btnD.Text)

btnD.Visible = False

End Sub

Private Sub btnE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE.Click

guess(btnE.Text)

btnE.Visible = False

End Sub

Private Sub btnF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click

guess(btnF.Text)

btnF.Visible = False

End Sub

Private Sub btnG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnG.Click

guess(btnG.Text)

btnG.Visible = False

End Sub

Private Sub btnH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnH.Click

guess(btnH.Text)

btnH.Visible = False

End Sub

Private Sub btnI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnI.Click

guess(btnI.Text)

btnI.Visible = False

End Sub

Private Sub btnJ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJ.Click

guess(btnJ.Text)

btnJ.Visible = False

End Sub

Private Sub btnK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnK.Click

guess(btnK.Text)

btnK.Visible = False

End Sub

Private Sub btnL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnL.Click

guess(btnL.Text)

btnL.Visible = False

End Sub

Private Sub btnM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnM.Click

guess(btnM.Text)

btnM.Visible = False

End Sub

Private Sub btnN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnN.Click

guess(btnN.Text)

btnN.Visible = False

End Sub

Private Sub btnO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnO.Click

guess(btnO.Text)

btnO.Visible = False

End Sub

Private Sub btnP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnP.Click

guess(btnP.Text)

btnP.Visible = False

End Sub

Private Sub btnQ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQ.Click

guess(btnQ.Text)

btnQ.Visible = False

End Sub

Private Sub btnR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnR.Click

guess(btnR.Text)

btnR.Visible = False

End Sub

Private Sub btnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnS.Click

guess(btnS.Text)

btnS.Visible = False

End Sub

Private Sub btnT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT.Click

guess(btnT.Text)

btnT.Visible = False

End Sub

Private Sub btnU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnU.Click

guess(btnU.Text)

btnU.Visible = False

End Sub

Private Sub btnV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnV.Click

guess(btnV.Text)

btnV.Visible = False

End Sub

Private Sub btnW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnW.Click

guess(btnW.Text)

btnW.Visible = False

End Sub

Private Sub btnX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnX.Click

guess(btnX.Text)

btnX.Visible = False

End Sub

Private Sub btnY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnY.Click

guess(btnY.Text)

btnY.Visible = False

End Sub

Private Sub btnZ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZ.Click

guess(btnZ.Text)

btnZ.Visible = False

End Sub

Private Sub btnAgain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgain.Click

Call ResetAll()

btnAgain.Enabled = False ' disable play again until game over

btn1.Enabled = True

btn2.Enabled = True

btn3.Enabled = True

End Sub

End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



I live my life A quarter mile at a time.


BaNdNeRd
BaNdNeRd
Forum God
Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)Forum God (2K reputation)

Group: Forum Members
Posts: 8, Visits: 3
F*CK that. lol

Wise man once said, man who goes to bed with it

Booooze
Booooze
Forum God
Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)Forum God (153K reputation)

Group: Forum Members
Posts: 827, Visits: 132
lol

upload the project

API Guide|Inno Setup Wizard|Winsockvb|StudioTraffic|Firefox
waynespangler
waynespangler
Forum God
Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)Forum God (98K reputation)

Group: Forum Members
Posts: 910, Visits: 5.4K

This is VB.NET and I know next to NILL about it. You don't say what the problem is just that there is a problem. If someone here has to run the program to check it then they will need your form and images.

Program suggestions:

If you use a control array for your buttons you will save a lot of typing and make your code more readable.

Such as in your sub to disable all buttons you use:

btnA.Enabled = False

btnB.Enabled = False

btnc.Enabled = False

btnD.Enabled = False

etc,etc,etc

With a control array array named btnAlpha you would type:

For X = 0 To 25

btnAlpha(X).Enabled=False

Next X

As you can see this takes 3 line where yours takes 26. This also holds true for your enabled sub.

You can further decrease it by combining the two subs like this

Private Sub EnableAll( ByValue En as Boolean)

Dim X as Integer

For X=0 To 25

btnAlpha(x).Enabled=En

Next X

End Sub

You now have taken 52 lines of code to 3 lines.

Also your button clicks for the alphabet:

Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click

guess (btnA.Text)

btnA.Visible = False

End Sub

Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click

guess (btnB.Text)

btnB.Visible = False

End Sub

etc, etc, etc

Without the control array you need 4 lines for each click event times 26 buttons you have 104 lines of code

Using this:

Private Sub BtnAlpha_Click(Index as Integer)

guess btnAlpha(Index)

btnAlpha(Index).Visible=False

End Sub

Taken with the other you have saved 149 lines. That is a lot of typing (just like this reply).

I will stop now. Take this in the spirit it is given. I am not  criticizing you just showing another way to program it.

 



Wayne

I know enough to be dangerious.

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.

GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search