Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Coded by errorseven
- #Persistent
- #SingleInstance, Force
- SetBatchLines -1
- ; Loading Word lists into Variables
- if (FileExist(A_ScriptDir . "\sorted.txt") && FileExist(A_ScriptDir . "\alpha.txt")) {
- FileRead, wordList, %A_ScriptDir%\sorted.txt
- FileRead, alphaList, %A_ScriptDir%\alpha.txt
- }
- Else { ; If Word lists don't exist download, sort, and create files
- MsgBox, 64, Downloading Dictionary File, Downloading a large Dictionary and running cutom sorting functions. `nThis can take some time... Please be patient.
- whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
- whr.Open("GET", "https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt", true)
- whr.Send()
- ; Using 'true' above and the call below allows the script to remain responsive.
- whr.WaitForResponse()
- words := whr.ResponseText
- Sort, words, F SortFunc
- FileAppend, %Words%, %A_ScriptDir%\sorted.txt
- For each, Line in StrSplit(Words, "`n", "`r" ) {
- x .= AlphaSort(Line) . "`n"
- }
- FileAppend, %x%, %A_ScriptDir%\alpha.txt
- FileRead, wordList, %A_ScriptDir%\sorted.txt
- FileRead, alphaList, %A_ScriptDir%\alpha.txt
- }
- InputBox, Mode, Daily Programmer Challenge #198, 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
- }
- Game := New WWE(wordList, alphaList, Mode)
- Gui, Font, s15, Consolas ;Gui stuff
- Gui, Add, Edit, x0 y0 w500 h500 Disabled Border -VScroll vDisplay
- Gui, Add, Edit, x0 y500 w500 h100 Disabled Border -VScroll -Wrap vWordDisplay
- Gui, Font, s10, Consolas
- Gui, Add, Edit, Uppercase x0 y601 w500 h25 vUserinput
- Gui, Show, w500 h625, Words With Enemies
- SetTimer, Running, 1000 ;Updates Display ever 100ms
- Running:
- GuiControl,, Display, % Game.Display
- GuiControl,, WordDisplay, % "Your Score Enemy Score`n`n" . Game.Score . " " . Game.EnemyScore
- If (Game.Round > 5) {
- MsgBox % Game.FinalScore()
- Reload
- }
- Return
- #IfWinActive Words With Enemies ;If Gui is active
- ~Enter::
- Gui, Submit, NoHide
- If (Game.IsWord(UserInput) && Game.InLetters(UserInput) && UserInput != "") {
- Game.Play(UserInput)
- }
- else {
- MsgBox, 4144, Invalid Input, The word you submitted: %UserInput% is invalid.`nYou either used characters that were not present in the selected game`nor your word does not appear in the dictionary. Please try again.
- }
- GuiControl,, Userinput, % "" ;Clears UserInput
- Return
- Class WWE {
- __New(x, y, z) { ;Declare Globals
- Global
- this.Round := 1
- this.wList := x
- this.aList := y
- this.Mode := z
- this.Word :=
- this.EnemyWord :=
- this.DLetters :=
- this.Letters := this.SetLetters()
- this.Display := "Your Letters:`n" . this.DLetters
- this.Score := 0
- this.EnemyScore := 0
- }
- Play(x) { ;Determined order in which to run the various methods that make this game work.
- this.EnemyWord := Format("{:U}", this.setEnemyWord())
- this.battle(this.Word, this.EnemyWord)
- this.Letters := this.SetLetters()
- this.Display := this.UpdateDisplay()
- this.Round++
- Return
- }
- battle(x, y) { ;Meat and bones of the Words With Enemies Easy Challenge, Cut down to fit this challenge.
- For each, Char in StrSplit(x,,"`n") {
- if InStr(y, SubStr(Char, 1,1)) {
- x := StrReplace(x, Char,,, 1)
- y := StrReplace(y, Char,,, 1)
- }
- }
- this.Update_Score(StrLen(x))
- this.Update_EnemyScore(StrLen(y))
- Return
- }
- InLetters(x) {
- y := this.Letters
- For Each, Char in StrSplit(x,,"`r") {
- if InStr(y, SubStr(Char, 1,1)) {
- x := StrReplace(x, Char,,, 1)
- y := StrReplace(y, Char,,, 1)
- }
- }
- If (StrLen(x) != 0) {
- Return False
- }
- Return True
- }
- SetLetters() { ;Random selection of letters
- vowels := "aeiouy"
- consonants := "bcdfghjkllmnpqrstvwxz"
- vowels := StrSplit(vowels,, "`r")
- consonants := StrSplit(consonants,, "`r")
- Loop, 14 {
- Random, out, 1, % consonants.MaxIndex()
- letters .= consonants[out]
- }
- Loop, 6 {
- Random, out, 1, % vowels.MaxIndex()
- letters .= vowels[out]
- letters := Format("{:U}", letters)
- }
- For each, Char in StrSplit(letters,, "`r") {
- y .= Char . " "
- }
- this.DLetters := y
- Return letters
- }
- UpdateDisplay() { ;Main game window
- x := this.Display
- If (this.Round = 5) {
- x .= "`nYour Word:" . this.Word . " vs Enemy Word:" . this.EnemyWord
- Return x
- }
- x := this.Display
- x .= "`nYour Word:" . this.Word . " vs Enemy Word:" . this.EnemyWord . "`n`nYour Letters:`n" . this.DLetters
- Return x
- }
- IsWord(x) { ;If UserInput is a Word in List Return True
- For each, Line in StrSplit(this.wList, "`n", "`r") {
- If (x = Line) {
- this.Word := Format("{:U}", x)
- Return True
- }
- }
- Return False
- }
- setEnemyWord() { ;Returns selected word based on Alpha Sorted list and Mode selected
- sortedletters := AlphaSort(this.Letters)
- z := StrSplit(this.wList, "`n", "`r")
- For each, Line in StrSplit(this.aList, "`n", "`r") {
- Count++
- If (InStr(sortedletters, Line) && StrLen(Line) > 1) {
- x .= z[Count] . "`n"
- }
- }
- x := StrSplit(x, "`n", "`r")
- y := x.MaxIndex() ;Get the Number of words in list
- If (this.Mode == "Hard") {
- Out := (y - 1)
- }
- If (this.Mode == "Intermediate") {
- Out := (y / 2)
- }
- If (this.Mode == "Easy") {
- Out := (y / 4)
- }
- Out := Round(Out)
- Return x[Out]
- }
- Update_Score(x) { ;Update Player Score
- this.Score += x
- Return
- }
- Update_EnemyScore(x) { ;Updates Enemy Score
- this.EnemyScore += x
- Return
- }
- FinalScore() { ;Compares score declares winner
- Return x ((this.Score > this.EnemyScore) ? "You Won with a score of: " . this.Score
- : (this.Score < this.EnemyScore) ? "The Enemy Won with a score of: " . this.EnemyScore
- : "The Game was a Tie!")
- }
- }
- AlphaSort(n) { ;Sort function used to sort var by alphabet
- delimiter := Chr(1)
- delimited := RegExReplace(n, "(.)", "$1" . delimiter)
- option = D%delimiter%
- Sort delimited, %option%
- StringReplace result, delimited, %delimiter%, , All
- return result
- }
- SortFunc(lineA, lineB, offset) { ;Sort function used to sort list smallest to largest
- if (StrLen(lineA) != StrLen(lineB) && StrLen(lineA) > 1)
- return StrLen(lineA)-StrLen(lineB)
- return -offset
- }
- GuiClose:
- ExitApp
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement