
Well, today was one of these days, were the boredom takesover... as a way to entertain myself, I thought I'd program something funny, that's what this script is all about.
The script gathers it's word list from a defineable file, the default filename is "Hangman_Words.txt".
Every line of the file will be a new word.
Script + Resources:
http://www.autohotke...ngman-1.0.1.zip
Script:
;===========================================================================================================================================================
;Directives
;===========================================================================================================================================================
#SingleInstance, Force
#NoTrayIcon
#NoEnv
;===========================================================================================================================================================
;Setup base variables
;===========================================================================================================================================================
;Application Info
App_Name := "Hangman"
App_Version := "1.0.1"
;Gui Numbers
Gui_Hangman := 1
;File Paths
Files_Pictures := "Images"
Files_Hangman := "Hangman_{Nr}.png" ;{Nr} will be replaced by count of wrong answers
Files_Wordlist := "Hangman_Words.txt"
;Letters
Letters_Alpha := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;Letters the player has to guess
;Word specific
Words0 := 0 ;Count of Array Entries
Word_Game := "" ;Word as visible in game
Word_Full := "" ;Word as loaded from file
Word_Count := 0 ;Last replacement count of a letter
Word_AllLt := 0 ;Count of all revealed letters
Word_Wrong := 0 ;Count of wrong guessings
;Gameplay
Game_MaxWrong := 6 ;Count of wrong answers for gameover
;===========================================================================================================================================================
;Setup the program
;===========================================================================================================================================================
Main:
;Load all words into memory
Hangman_LoadWordList()
;Create all windows
GoSub, Hangman_CreateGui
;Load the first random word
GoSub, Hangman_NewWord
Return
;===========================================================================================================================================================
;Hangman Labels
;===========================================================================================================================================================
Hangman_CreateGui:
;Setup the gui options
Gui, %Gui_Hangman%:-Border +ToolWindow +LastFound +LabelHangman_ -Theme
;Setup the font setting
Gui, %Gui_Hangman%:Font, Bold, Arial
;Create the titlebar
Gui, %Gui_Hangman%:Add, Text, x5 y5 w350 h20 +0x1000, % " " App_Name " [V" App_Version "] - " Words0 " Words loaded"
Gui, %Gui_Hangman%:Add, Text, x5 y5 w330 h20 +BackgroundTrans gHangman_MoveGui
Gui, %Gui_Hangman%:Add, Button, x335 y8 w15 h15 gHangman_Close, X
;Create hangman picture
Gui, %Gui_Hangman%:Add, Picture, x5 y30 w350 h350 +0x1000 vHangman_Pic
;Create the word container
Gui, %Gui_Hangman%:Add, Text, x5 y395 w350 h40 +0x1000 vHangman_Word
Gui, %Gui_Hangman%:Add, Text, x5 y440 w350 h65 +0x1000
;Only use hotkeys on this window
Hotkey, IfWinActive, Hangman
;Create the alphabetical buttons
Loop, Parse, Letters_Alpha
{
;If it's first button, or first button of second line, use 40
If (A_Index = 1 || A_Index = 14)
XPos := 40
;Increment the horizontal position by 22 otherwise
Else XPos := "p+22"
;Use vertical position 40 on first line, on second line 65
YPos := (A_Index < 14) ? 450 : 475
Gui, %Gui_Hangman%:Add, Button, x%XPos% y%YPos% w20 h20 vHangman_Letter%A_Index% gHangman_NewLetter, % A_LoopField
;Create a hotkey as well
Hotkey, % A_LoopField, Hangman_NewLetterByHotkey, On
}
;Create gameplay specific controls
Gui, %Gui_Hangman%:Add, Text, x5 y512 w195 h20 vHangman_Stat, Wrong: 0/0 Letters: 0
Gui, %Gui_Hangman%:Add, Button, x200 y510 w75 h20 gHangman_SolveWord, Solve Word
Gui, %Gui_Hangman%:Add, Button, x280 y510 w75 h20 gHangman_NewWord, New Word
;Create and show the gui
Gui, %Gui_Hangman%:Show, w360 h535, Hangman
Return
Hangman_NewWord:
;Ensure that everything's in the start state
Hangman_Reset()
;Load the random word
Hangman_LoadWord()
Return
Hangman_SolveWord:
InputBox, Word, Hangman - Solve Word, Enter the word:,, 300, 120
If (Word = Word_Full) {
;Show the full word
GuiControl, %Gui_Hangman%:, Hangman_Word, % Word_Full
;Disable all buttons
Hangman_LetterState(0)
;Show "Game Won" Message
MsgBox,, Hangman, Congratulations! You've won the game!
} Else MsgBox,, Hangman, Sorry`, that wasn't correct!
Return
Hangman_NewLetter:
;Retrieve the letter of the pressed control
GuiControlGet, Letter,, % A_GuiControl
;Check if this letter is within the word
Hangman_CheckLetter(Letter)
;Disable control
GuiControl, %Gui_Hangman%:Disable, % A_GuiControl
Return
Hangman_NewLetterByHotkey:
;Use the hotkey button name
Letter := A_ThisHotkey
;Check if this letter is within the word
Hangman_CheckLetter(Letter)
;Loop through all allowed characters
Loop, Parse, Letters_Alpha
;If the current character was the pressed key
If (A_LoopField = Letter) {
;Disable the control
GuiControl, %Gui_Hangman%:Disable, % "Hangman_Letter" A_Index
;Break from the loop
Break
}
Return
Hangman_MoveGui:
PostMessage, 0xA1, 2,,, A
Return
Hangman_Escape:
Hangman_Close:
MsgBox, 64, Hangman, % App_Name " [V" App_Version "]`n`nCreated by: Banane"
ExitApp
;===========================================================================================================================================================
;Functions
;===========================================================================================================================================================
Hangman_Reset() {
global
;Reset all variables
Word_Game := ""
Word_Full := ""
Word_Count := 0
Word_Wrong := 0
;Enable letter buttons
Hangman_LetterState(1)
;Restore standard values
GuiControl, %Gui_Hangman%:, Hangman_Word,
GuiControl, %Gui_Hangman%:, Hangman_Stat, % "Wrong: 0/" Game_MaxWrong " Letters: 0"
Hangman_HangmanImage()
}
Hangman_HangmanImage() {
global
local Image
StringReplace, Image, Files_Hangman, {Nr}, % Word_Wrong
GuiControl, %Gui_Hangman%:, Hangman_Pic, % Files_Pictures "\" Image
}
Hangman_LoadWordList() {
global
local Currword
;Read the file line by line
Loop, Read, % Files_Wordlist
;Only create array entry for this line, if not empty
If (A_LoopReadLine <> "")
;Increase array count and save current word in pseudo array
Words0 ++,Words%Words0% := A_LoopReadLine,Currword := ""
}
Hangman_LoadWord() {
global
local Currword
;Select a random word from array
Random, Currword, 1, % Words0
;Set currword to the word in Words%Currword%
Currword := Words%Currword%
;Change all alphabetical letters to _
Loop, Parse, Currword
Word_Game .= (InStr(Letters_Alpha,A_LoopField)) ? "_" : A_LoopField
;Make every letter uppercase
StringUpper, Word_Full, Currword
;Change the content of the word control
GuiControl, %Gui_Hangman%:, Hangman_Word, % Word_Game
}
Hangman_CheckLetter(Input) {
global
;Reset the found letter count
Word_Count := 0
;Parse through every letter of the full word
Loop, Parse, Word_Full
;If the current letter is equal to the specified one
If (A_LoopField = Input)
;Change the _ of this position with the letter and increase count
Word_Game := SubStr(Word_Game,1,A_Index - 1) Input SubStr(Word_Game,A_Index + 1,StrLen(Word_Game)),Word_Count ++
;Increase the count of wrong guessings if no letter was replaced, change control text otherwise
If (Word_Count = 0)
Word_Wrong ++,Hangman_HangmanImage()
Else GuiControl, %Gui_Hangman%:, Hangman_Word, % Word_Game
Word_AllLt := Word_AllLt + Word_Count
;Update the statistics
GuiControl, %Gui_Hangman%:, Hangman_Stat, % "Wrong: " Word_Wrong "/" Game_MaxWrong " Letters: " Word_AllLt
;If the count is equal to the maximum count of wrong guessings (6 by default)
If (Word_Wrong = Game_MaxWrong || Word_Game = Word_Full) {
;Show the full word
GuiControl, %Gui_Hangman%:, Hangman_Word, % Word_Full
;Disable all buttons
Hangman_LetterState(0)
;Show "Game Over" / "Game Won" Message
MsgBox,, Hangman, % (Word_Wrong = Game_MaxWrong) ? "Game Over!" : "Congratulations! You've won the game!"
}
}
Hangman_LetterState(Mode) {
global
;Check whether the buttons should be enabled or disabled
Mode := (Mode = 1) ? "Enable" : "Disable"
;Apply the state to every button
Loop, % StrLen(Letters_Alpha) {
;Change the control state
GuiControl, %Gui_Hangman%:%Mode%, Hangman_Letter%A_Index%
;Change the hotkey state
Hotkey, % SubStr(Letters_Alpha,A_Index,1), % (Mode = "Enable") ? "On" : "Off"
}
}




