Last active
February 17, 2018 01:30
-
-
Save errorseven/b10db5b2e4d0405af8c1 to your computer and use it in GitHub Desktop.
/r/DailyProgrammer [2014-11-17] Challenge #189 [Easy] Hangman!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Persistent | |
#SingleInstance, Force | |
SetTitleMatchMode, 2 | |
SetWorkingDir, %A_ScriptDir% | |
;Ask player to Enter Mode of play Easy, Medium, Hard | |
InputBox, Mode, Daily Programmer Challenge #189, Please enter [Easy] [Intermediate] or [Hard] , 640, 480 | |
If (ErrorLevel = 1) ;If Cancel or Close Exit | |
ExitApp | |
If !(Mode == "Easy" Or Mode == "Intermediate" or Mode == "Hard") { ;If not Easy, Intermediate, or Hard Reload! | |
Reload | |
} | |
Display = ;Sets variable to contain Gallows | |
( | |
.....+----+... | |
.....|....|... | |
.....|........ | |
.....|........ | |
.....|........ | |
...__|__...... | |
) | |
Display := StrSplit(Display, "`n", "`r") ;Sets Gallows as an Array | |
Game := New Hangman(Mode, Display*) ;Sets Game as Class passes Mode and Array Display | |
Gui, Font, s40, Consolas ;Gui stuff | |
Gui, Add, Edit, x0 y0 w500 h400 Disabled Border -VScroll vDisplay, % Game.Display | |
Gui, Font, s15, Consolas | |
Gui, Add, Edit, x0 y400 w500 h100 Disabled Border -VScroll vWordDisplay, % Game.Dword | |
Gui, Font, s10, Consolas | |
Gui, Add, Edit, Limit1 Uppercase x0 y501 w500 h25 vUserinput | |
Gui, Show, w500 h525, Hangman | |
SetTimer, Running, 100 ;Updates Display ever 100ms | |
Running: | |
GuiControl,, Display, % Game.Display | |
GuiControl,, WordDisplay, % Game.Dword . "`n`n" . Game.Misses | |
Return | |
#IfWinActive Hangman ;If Gui is active | |
~Enter:: | |
Gui, Submit, NoHide | |
If (InStr(Game.Misses, UserInput) == 0 && InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", UserInput)) { ;If Input is NOT in Misses AND Input is = to A-Z | |
Game.InWord(UserInput) ? Game.UpdateWord(UserInput) ;Call | |
: Game.UpdateDisplay(UserInput) | |
} | |
GuiControl,, Userinput, % "" ;Clears UserInput | |
Return | |
Class Hangman { ;Main Game Class | |
__New(Mode, Default*) { | |
Global | |
this.word := | |
this.Misses := | |
this.Dword := this.Set(Mode) | |
Loop % Default.MaxIndex() | |
tmp .= Default[A_Index] . "`n" | |
StringReplace, tmp, tmp, ., %A_Space%, ALL | |
this.Display := tmp | |
} | |
Set(Mode) { ;Based on mode, returns Random Word selected from list | |
if FileExist(A_ScriptDir . "\sorted.txt") { ;Read sorted.txt | |
FileRead, MyList, %A_ScriptDir%\sorted.txt | |
} | |
Else If FileExist(A_ScriptDir . "\words.txt") { ;If words.txt Sort by Character Length Save as sorted.txt | |
MsgBox, 0, Informational - Please read., Hangman has detected words.txt `nIt will now be Sorted and saved as sorted.txt.`nThis can take up to 30 seconds or longer depending on your system speed.`nPlease be patient. | |
FileRead, MyList, %A_ScriptDir%\words.txt | |
Sort, MyList, F SortFunc | |
FileAppend, %MyList%, %A_ScriptDir%\sorted.txt | |
FileDelete, %A_ScriptDir%\words.txt | |
} | |
Else { ;No list found. | |
MsgBox, 16, Error 7 , No Word List was found!`nPlease download or create a word list.`nPlace it in the same directory as this game.`nTitle it: word.txt | |
ExitApp | |
} | |
Words := StrSplit(MyList, "`n", "`r") ;Put wordlist into array | |
y := Words.MaxIndex() ;Get the Number of words in list | |
If (Mode == "Easy") { ;Determine mode, set Min, Max range for use in Random | |
Min := (y / 2) | |
Max := y | |
} | |
If (Mode == "Intermediate") { | |
Min := (y / 4) | |
Max := (y / 2) | |
} | |
If (Mode == "Hard") { | |
Min := 1 | |
Max := (y / 4) | |
} | |
Min := Round(Min) ;Round to a whole number | |
Max := Round(Max) | |
Random, OutPutVar, Min, Max ;Generates a Random number based on Mininum and Maximum selected by Mode | |
this.word := Words[OutPutVar] ;Game Word Chosen and assigned | |
this.word := Format("{:U}", this.word) ;Uppercase | |
Loop % StrLen(this.word) { ;Genereates Displayed Word as Underscores | |
x .= "_ " | |
} | |
return x ;Returns x which stores in Dword, ie Underscored Word to be displayed | |
} | |
InWord(np) { ;Return True if Input is in this.word | |
Return InStr(this.word, np) ? True : False | |
} | |
UpdateWord(x) { ;Find All instances of Letter in Word and Replace Underscores in correct places | |
StringReplace, y, % this.Dword, %A_SPACE%, , All | |
y := StrSplit(y,,"`r") | |
For Each, Char in StrSplit(this.word,,"`r") { | |
Count++ | |
InStr(Char, x) ? y[Count] := x : Continue | |
} | |
Loop % y.MaxIndex() | |
p .= y[A_Index] . " " | |
this.Dword := p | |
StringReplace, p, p, %A_SPACE%, , All ;If Current Display Word matches Game Word you Win! | |
If (p == this.Word) { | |
MsgBox % 4144, You Win, You survived by solving the word : %p%`nPlease Play Again! | |
Reload | |
} | |
Return | |
} | |
UpdateDisplay(x) { ;Changes scene based on Count | |
Static Count ;Count doesn't change | |
this.Misses .= x . " " ;Update Misses | |
Count++ | |
Lines := StrSplit(this.Display, "`n", "'r") | |
(Count == 1 ? (Lines[3] := ".....|....0...") | |
: Count == 2 ? (Lines[4] := ".....|....|...") | |
: Count == 3 ? (Lines[4] :=".....|.../|...") | |
: Count == 4 ? (Lines[4] := ".....|.../|\..") | |
: Count == 5 ? (Lines[5] := ".....|.../....") : (Lines[5] := ".....|.../.\..")) | |
Loop % Lines.MaxIndex() | |
tmp .= Lines[A_Index] . "`n" | |
StringReplace, tmp, tmp, ., %A_Space%, ALL ;Replaces Periods with Spaces | |
this.Display := tmp | |
If (Count == 6) { ;After Six Misses you lose | |
z := this.word | |
MsgBox % 4144, You Lose, You died to the Word : %z%`nPlease Try Again! | |
Reload | |
} | |
Return | |
} | |
} | |
SortFunc(lineA, lineB, offset) ;Sort function used to sort list smallest to largest | |
{ | |
if StrLen(lineA) != StrLen(lineB) | |
return StrLen(lineA)-StrLen(lineB) | |
return -offset | |
} | |
GuiClose: | |
ExitApp | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment