Option Explicit On
Option Strict On
'--------------------------------
' Steve Taniguchi
' CS115-B Summer '15
' Assignment 3 - Hangman
'--------------------------------
Public Class frmHangman
    'Class level variables
    Dim rand As New Random
    Dim intWord As Integer
    'Declare alphabet buttons array
    Dim btnAlphabet(25) As Button
    'Declare images array
    Dim imgHangmanDrawing(7) As Image
    'Declare dictionary array
    Dim strDictionary() As String = {"APPLE", "BANANA", "CARROT", "DAIKON",
                                     "EGGPLANT", "FIG", "GRAPEFRUIT", "HONEYDEW",
                                     "JICAMA", "KIWI", "LEMON", "MANGO",
                                     "NECTARINE", "ORANGE", "PINEAPPLE", "PUMPKIN",
                                     "RADISH", "STRINGBEAN", "TOMATO", "ZUCCHINI"}
    'Declare dashes array
    Dim strDashes() As String = {"-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
                             "-", "-", "-", "-"}
    'Declare word - word pulled from dictionary
    Dim strWord As String
    'Declare guess string - string for adding guesses to dashes
    Dim strGuess As String
    'Declare pic count
    Dim intPicCount As Integer = 0
    Private Sub frmHangman_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Assign alphabet buttons array
        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
        'Show opening screen controls visible
        lblTitle.Visible = True
        btnStart.Visible = True
        btnExit.Visible = True
        'Hide game controls
        grpLetters.Visible = False
        picHangman.Visible = False
        lblWord.Visible = False
        'Hide game over dialog
        lblGameOver.Visible = False
        grpPlayAgain.Visible = False
    End Sub
    '---------------------------------------------------------------
    'Load Hangman drawings into Image array
    'Set PictureBox to first image
    '---------------------------------------------------------------
    Sub InitImageArray()
        For intX = 0 To imgHangmanDrawing.Length - 1
            imgHangmanDrawing(intX) = Image.FromFile("..\Images\" & "hangman" &
           intX.ToString & ".png")
        Next
        picHangman.Image = Image.FromFile("..\Images\hangman0.png")
    End Sub
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        Call NewGame()
    End Sub
    Sub NewGame()
        'Hide opening screen controls
        lblTitle.Visible = False
        btnStart.Visible = False
        'Show game controls
        grpLetters.Visible = True
        picHangman.Visible = True
        lblWord.Visible = True
        'Hide game over controls
        lblGameOver.Visible = False
        grpPlayAgain.Visible = False
        'Initialize image array
        Call InitImageArray()
        'Clear word
        strGuess = String.Empty
        'Enable alphabet buttons
        For intX = 0 To btnAlphabet.Length - 1
            btnAlphabet(intX).Enabled = True
            btnAlphabet(intX).BackColor = Color.White
        Next
        'Get new random word
        intWord = rand.Next(20)
        strWord = strDictionary(intWord)
        'Fill word with dashes
        ReDim strDashes(strWord.Length - 1)
        For intX = 0 To strWord.Length - 1
            strDashes(intX) = "-"
            strGuess = (strGuess + strDashes(intX))
        Next
        lblWord.Text = strGuess

        '------------------------------------
        ' * Output to test box *
        lblTEST.Text = strDictionary(intWord)
        '------------------------------------

    End Sub
    '-------------------------------------
    'Handler for the letter buttons
    '-------------------------------------
    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
        Dim intCount As Integer = 0
        Dim blnActiveButtonFound As Boolean = False
        'Find the button that was selected
        'Disable it so it cannot be picked again
        Do Until blnActiveButtonFound = True Or intCount = btnAlphabet.Length - 1
            If Me.ActiveControl.Name = btnAlphabet(intCount).Name Then
                btnAlphabet(intCount).Enabled = False
                btnAlphabet(intCount).BackColor = Color.Gray
                blnActiveButtonFound = True
            Else
                intCount += 1
            End If
        Loop
        Call Search(btnAlphabet(intCount).Text)
    End Sub
    Sub Search(strValue As String)
        Dim strLetters As String
        Dim blnGuess As Boolean = False
        strLetters = String.Empty
        strGuess = String.Empty
        For intX = 0 To strWord.Length - 1
            If strWord.Substring(intX, 1) = strValue Then
                strDashes(intX) = strWord.Substring(intX, 1)
                blnGuess = True
            End If
            strGuess = (strGuess + strDashes(intX))
            lblWord.Text = strGuess
        Next
        If blnGuess = False Then
            intPicCount += 1
            picHangman.Image = imgHangmanDrawing(intPicCount)
        End If
        If strGuess = strWord Or intPicCount = 7 Then
            Call GameOver()
        End If
    End Sub
    Sub GameOver()
        '-------------------------------
        ' ** BUILD OUT A BOX TO SHOW GAME OVER / PLAY AGAIN OPTIONS
        '--------------------------------
        'Disable all alphabet buttons
        For intX = 0 To btnAlphabet.Length - 1
            btnAlphabet(intX).Enabled = False
            btnAlphabet(intX).BackColor = Color.Gray
        Next
        If intPicCount = 7 Then
            lblGameOver.Visible = True
            lblGameOver.BackColor = Color.Pink
            grpPlayAgain.Visible = True
            grpPlayAgain.BackColor = Color.Pink
            lblGameOver.Text = ("GAME OVER" & vbCrLf & "The word was " & strWord & "." & vbCrLf & vbCrLf & "Play again?")
        ElseIf strGuess = strWord Then
            lblGameOver.Visible = True
            lblGameOver.BackColor = Color.LimeGreen
            grpPlayAgain.Visible = True
            grpPlayAgain.BackColor = Color.LimeGreen
            lblGameOver.Text = ("YOU WIN!" & vbCrLf & vbCrLf & "Play again?")
        End If
        radYes.Checked = True
    End Sub
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    Private Sub btnNEWGAME_Click(sender As Object, e As EventArgs) Handles btnNEWGAME.Click
        Call NewGame()
    End Sub
    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        If radYes.Checked = True Then
            'Call NewGame subroutine
            Call NewGame()
        ElseIf radNo.Checked = True Then
            'Display thank you message box
            MessageBox.Show("Thank you for playing.")
            'Close application
            Me.Close()
        End If
    End Sub
End Class
