-
Mar 8th, 2018, 03:38 PM
#1
Thread Starter
New Member
Help with hangman project.
Hey guys,
I am doing a class project for hangman and I having trouble figuring out how to properly work with the button counter, I want it to count every time I push a button and a letter appears, if a letter doesn't appear I want it to pull up the next Image. I spend the last few hours trying figure it out but I am not understanding what to do properly. Any help would be much appreciated!
Code:
Option Explicit On
Option Strict On
Public Class frmHangman
'-----------------------------------------------------------------------------------------
'Program: Final Project
'Author: Joseph
'Date: 3/6/2018
'Description: Game will start with and user will have to press start, user guesses the word if too many incorrect guesses, game over. if user guesses the word in
' 7 tries, user wins, victory screen shows up.
'-------------------------------------------------------------------------------------------------
' ============= Arrays =================
'Alphabet array
Dim strAlphabet() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
'Button array
Dim btnAlphabet(25) As Button
' Guess array
'Dim strGuessArray(30) As String
' Dashes
Dim strDashes(30) As String
' Word to be guessed
Dim strWord As String = ""
Dim wordLength As Integer
'Dictionary array
Dim strDictionary() As String = {"Token", "Ballroom", "Cheese", "Telephone", "Russia", "America",
"Rose", "Tarantula", "Positive", "Negative", "Explosion", "Machine", "Contraband", "Lemonade",
"Raspberry", "Operator", "Doctor", "Officer", "Soldier", "Marina"}
'Image array
Dim imgHangman(7) As Image
'=======================================
'random number generator
Dim rnd As New Random
Dim rndmX As Integer
Dim intMyRandomNumber As Integer
'counter
Dim intX As Integer = 0
Dim CountButton As Integer = 0
' Try Counter?
Dim wrgTry As Integer = 0
Dim rghtTry As Integer = 0
' ======================================
Sub ButtonHandler() Handles btnA.Click, btnB.Click, btnC.Click, btnD.Click, btnE.Click, btnF.Click,
btnG.Click, btnH.Click, btnI.Click, btnJ.Click, btnK.Click, btnL.Click, btnM.Click, btnN.Click,
btnO.Click, btnP.Click, btnQ.Click, btnR.Click, btnS.Click, btnT.Click, btnU.Click, btnV.Click,
btnW.Click, btnX.Click, btnY.Click, btnZ.Click
'-----------------------------------------------------------------------------------------
'Subroutine: Button Handeler
'Author: Joseph
'Date: 3/6/2018
'Description: Handles each button.
'-------------------------------------------------------------------------------------------------
Dim strInput As String
Dim strOutput As String = String.Empty
Dim blnActiveFound As Boolean = False
Dim intIndex As Integer = 0
'============================
'Sets buttonAlphabet to the active button.
Dim blnActiveFound2 As Boolean = True
Dim intB As Integer = 0
Do While blnActiveFound2 = True
If Me.ActiveControl.Name = btnAlphabet(intB).Name Then
blnActiveFound2 = False
Else
intB = intB + 1
End If
Loop
'============================
strInput = btnAlphabet(intB).Text
' TO UPPER
strInput = strInput.ToUpper
strOutput = strOutput.ToUpper
strWord = strWord.ToUpper
' call subroutine to search for character in secret word
Call SearchAndReplace(strInput)
'Do Something
Do Until blnActiveFound = True
If Me.ActiveControl.Name = btnAlphabet(intIndex).Name Then
btnAlphabet(intIndex).Enabled = False
For intX = 0 To strWord.Length - 1
strOutput = strOutput & strDashes(intX) + " "
Next
lblOutput.Text = strOutput
blnActiveFound = True
Else
intIndex = intIndex + 1
End If
Loop
' Label2.Text = wrgTry.ToString
CheckForWinning()
End Sub
'================================================================
Private Sub frmHangman_Load(Sender As Object, e As EventArgs) Handles Me.Load
'-----------------------------------------------------------------------------------------
'Subroutine: Form Load
'Author: Joseph
'Date: 3/6/2018
'Description: Enter what happens when form loads
'-------------------------------------------------------------------------------------------------
boxAlphabet.Visible = False
btnExit.Visible = True
btnReset.Visible = False
lblOutput.Visible = False
lblInformation.Visible = False
lblInstruction.Visible = False
picHangmanStart.Visible = True
'Initialize word randomizer
WordRandomizer()
'Start Picture
picHangmanStart.Image = Image.FromFile("..\Images\" & "StartupImage" & ".png")
'For Next loop to put each image into the Image array
'Now you can reference each image by indexing into the Image array
'Btn Array
btnAlphabetArray()
'Dash reset
For intX = 0 To strWord.Length - 1
strDashes(intX) = "-"
Next
'
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
'-----------------------------------------------------------------------------------------
'Subroutine: button reset
'Author: Joseph
'Date: 3/6/2018
'Description: Resets the form.
'-------------------------------------------------------------------------------------------------
Dim intIndex As Integer = 0
'===================
lblOutput.Text = ""
boxAlphabet.Visible = False
btnExit.Visible = False
btnReset.Visible = False
lblOutput.Visible = False
lblInformation.Visible = False
lblInstruction.Visible = False
btnStartGame.Visible = True
picHangmanStart.Visible = True
picHangmanImage.Image = imgHangman(0)
picHangmanImage.Visible = False
'initImages()
'
rghtTry = 0
wrgTry = 0
'==========================
'Picks a random word from the dictionary
WordRandomizer()
'For loop to reset all the buttons?
For intX = 0 To btnAlphabet.Length - 1
If btnAlphabet(intIndex).Enabled = False Then
btnAlphabet(intIndex).Enabled = True
End If
intIndex = intIndex + 1
Next
'For loop to reset the word string
For intX = 0 To strWord.Length - 1
strDashes(intX) = "-"
Next
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'-----------------------------------------------------------------------------------------
'Subroutine: button exit
'Author: Joseph
'Date: 3/6/2018
'Description: Exits the form
'-------------------------------------------------------------------------------------------------
Me.Close()
End Sub
Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
btnStartGame.Visible = False
boxAlphabet.Visible = True
picHangmanStart.Visible = False
btnExit.Visible = True
btnReset.Visible = True
lblOutput.Visible = True
lblInformation.Visible = True
lblInstruction.Visible = True
btnA.Focus()
'Load first image into picture box
picHangmanImage.Visible = True
picHangmanImage.Image = imgHangman(0)
initImages()
End Sub
Sub initImages()
'-----------------------------------------------------------------------------------------
'Subroutine: Initialize Images
'Author: Joseph
'Date: 3/6/2018
'Description: Initializes the images for hangman
'-------------------------------------------------------------------------------------------------
picHangmanImage.Visible = True
For intX = 0 To imgHangman.Length - 1
imgHangman(intX) = Image.FromFile("..\Images\" & "hangman" & intX.ToString & ".png")
Next
picHangmanImage.Image = imgHangman(0)
'load first image into picture box
'picHangmanImage.Image = imgHangman(0)
'Dim blnButtonFound As Boolean = False
'Dim intCounter As Integer = 0
'Do While blnButtonFound = False Or intCounter > 4
' If radSelectPic(intCounter).disabled Then
' blnButtonFound = True
' picHangmanImage.Image = imgHangman(intCounter)
' Else
' intCounter = intCounter + 1
' End If
'Loop
End Sub
Sub btnAlphabetArray()
'-----------------------------------------------------------------------------------------
'Subroutine: Button Alphabet array
'Author: Joseph
'Date: 3/6/2018
'Description: assigns each button a val of alphabet.
'-------------------------------------------------------------------------------------------------
btnAlphabet(0) = btnA
btnAlphabet(1) = btnB
btnAlphabet(2) = btnC
btnAlphabet(3) = btnD
btnAlphabet(4) = btnE
btnAlphabet(5) = btnF
btnAlphabet(6) = btnG
btnAlphabet(7) = btnH
btnAlphabet(8) = btnI
btnAlphabet(9) = btnJ
btnAlphabet(10) = btnK
btnAlphabet(11) = btnL
btnAlphabet(12) = btnM
btnAlphabet(13) = btnN
btnAlphabet(14) = btnO
btnAlphabet(15) = btnP
btnAlphabet(16) = btnQ
btnAlphabet(17) = btnR
btnAlphabet(18) = btnS
btnAlphabet(19) = btnT
btnAlphabet(20) = btnU
btnAlphabet(21) = btnV
btnAlphabet(22) = btnW
btnAlphabet(23) = btnX
btnAlphabet(24) = btnY
btnAlphabet(25) = btnZ
End Sub
Sub SearchAndReplace(strSearchLetter As String)
'--------------------------------------------------------------
'Subroutine: SearchAndReplace
'Description: Search the String and determine if it contains the letter you are looking for
' if found replace the corresponding "-" in the array
'Parms: one-character String
'------------------------------------------------------------
'Search for letter
Dim count As Integer = strWord.Length - 1
For intX = 0 To strWord.Length - 1
If strWord.Substring(intX, 1) = strSearchLetter Then
strDashes(intX) = strSearchLetter
rghtTry = rghtTry + 1
Label1.Text = rghtTry.ToString
End If
Next
End Sub
Sub WordRandomizer()
'-----------------------------------------------------------------------------------------
'Subroutine: Word Randomizer
'Author: Joseph
'Date: 3/6/2018
'Description: Picks a random word from the dictionary.
'-------------------------------------------------------------------------------------------------
'------------Random number generator
intMyRandomNumber = rnd.Next(0, 20)
rndmX = intMyRandomNumber
'------------------------------
strWord = strDictionary(rndmX)
End Sub
Sub CheckForWinning()
'-----------------------------------------------------------------------------------------
'Subroutine: Check for Winning
'Author: Joseph
'Date: 3/6/2018
'Description: Checks if the user won the game or not, if user won, promts user with msgbox to reset or exit game.
'-------------------------------------------------------------------------------------------------
wordLength = strWord.Length
If rghtTry = wordLength Then
'MsgBox("You Won")
If MsgBox("You Won! Do you want to play again? Click Yes to reset the game, Click No to exit.", MsgBoxStyle.YesNo, "Winner!") = MsgBoxResult.Yes Then
btnReset.PerformClick()
Else
Me.Close()
End If
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|