'////////////////////////////////////////////////////////////////////////
'// HANGMAN SCRIPT FOR 9.4
'// WRITTEN BY: Chriso
'// This script will register the command 'hangman' so that users can
'// whisper the bot a specific phrase for players to guess letters out of
'// or the entire phrase.
'// Based loosely off of the StealthBot hangman game.
'////////////////////////////////////////////////////////////////////////

Dim Phrase, Reveal, Missed, Letters, Enabled
Dim ArrMiss(8)

'// Return Script Information
Sub Script(Name, Major, Minor, Build, Author, Commands, Description)
	Name = "Hangman Script"
	Major = 1
	Minor = 0
	Build = 0
	Author = "Chriso"
	Commands = "hangman"
	Description = "Play the classic 'hangman' pencil and paper game on MirageBot"
End Sub

'// Called when the script is loaded for a particular bot
Sub Event_Load()
    Enabled = False
	ArrMiss(1) = "gallows drawn"
    ArrMiss(2) = "head drawn"
    ArrMiss(3) = "neck drawn"
    ArrMiss(4) = "left arm drawn"
    ArrMiss(5) = "right arm drawn"
	ArrMiss(6) = "torso drawn"
    ArrMiss(7) = "left leg drawn"
    ArrMiss(8) = "right leg drawn"
    CreateCommand "hangman", "Hangman", "Command_Hangman", "", "", "Start or stop a hangman game", 0, True
End Sub

'// Callback for "test" Command
Sub Command_Hangman(CS)
	Enabled = Not Enabled
	If Enabled Then
	    StartNewGame
	Else
		Send "Hangman drawing erased."
		Phrase = ""
		Reveal = ""
		Missed = 0
		Letters = ""
	End If
End Sub

'// Called when a user speaks
Sub Event_UserTalk(CE)
    If Len(Phrase) = 0 Then Exit Sub
    If Len(CE.Message) = 1 Then
		Dim l, f, i
		l = LCase(CE.Message)
		f = False
		For i = 1 To Len(Phrase)
			If Mid(Phrase,i,1) = l Then
				b = b & l
				f = True
			Else
				b = b & Mid(Reveal, i, 1)
			End If
		Next
		Reveal = b
		If Not f Then
            If InStr(Letters, l) Then
               '// Already guessed!
               Exit Sub
            End If
			Missed = Missed + 1
			Letters = Letters & l
			If Missed >=8 Then
				Send "Hangman executed! The answer was: " & UCase(Phrase)
				Phrase = ""
				StartNewGame
			Else
				Send Reveal & " [" & ArrMiss(Missed) & ", " & Missed & "/8] [Guessed: " & Letters & "]"
			End If
		Else
			If LCase(Reveal) = LCase(Phrase) Then
				Send "Hangman spared! The answer was: " & UCase(Phrase)
				Phrase = ""
				StartNewGame
				Exit Sub
			End If
			Send Reveal
        End If
    Else
       If LCase(CE.Message) = LCase(Phrase) Then
			Send "Hangman spared! The answer was: " & UCase(Phrase)
			Phrase = ""
			StartNewGame
       End If
    End If
End Sub

Sub StartNewGame()
	    Phrase = LCase(Split(Split(scInet.OpenUrl("http://watchout4snakes.com/" & _
                                "creativitytools/RandomWord/RandomWordPlus.aspx"), _
                                "<span id=""tmpl_main_lblWord"" class=""randomWord"">")(1), "<")(0))
		Reveal = String(Len(Phrase), "_")
		Missed = 0
		Letters = ""
		AddChat vbCyan, "Word: " & Phrase
		Send "Hangman started, word is: " & Reveal & " [" & Len(Reveal) & "]"
End Sub