Guest User

Words With Enemies - The Game

a guest
May 31st, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Persistent
  2. #SingleInstance, Force
  3.                                                             ; Loading Word lists into Variables
  4. if (FileExist(A_ScriptDir . "\sorted.txt") && FileExist(A_ScriptDir . "\alpha.txt")) {
  5.             FileRead, wordList, %A_ScriptDir%\sorted.txt
  6.             FileRead, alphaList, %A_ScriptDir%\alpha.txt
  7.         }
  8.     Else {                                                                             ; If Word lists don't exist download, sort, and create files
  9.         MsgBox, 64, Downloading Dictionary File, Downloading a large Dictionary and running cutom sorting functions. `nThis can take some time... Please be patient.
  10.         whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  11.         whr.Open("GET", "https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt", true)
  12.         whr.Send()
  13.         ; Using 'true' above and the call below allows the script to remain responsive.
  14.         whr.WaitForResponse()
  15.         words := whr.ResponseText
  16.         Sort, words, F SortFunc
  17.         FileAppend, %Words%, %A_ScriptDir%\sorted.txt
  18.         For each, Line in StrSplit(Words, "`n", "`r" ) {               
  19.                 x .= AlphaSort(Line) . "`n"
  20.         }
  21.         FileAppend, %x%, %A_ScriptDir%\alpha.txt
  22.         FileRead, wordList, %A_ScriptDir%\sorted.txt
  23.         FileRead, alphaList, %A_ScriptDir%\alpha.txt
  24.     }
  25.  
  26. InputBox, Mode, Daily Programmer Challenge #198, Please enter [Easy] [Intermediate] or [Hard] , 640, 480
  27. If (ErrorLevel = 1)                   ;If Cancel or Close Exit
  28.     ExitApp
  29.    
  30.     If !(Mode == "Easy" Or Mode == "Intermediate" or Mode == "Hard") {  ;If not Easy, Intermediate, or Hard Reload!
  31.         Reload
  32.     }
  33.    
  34. Game := New WWE(wordList, alphaList, Mode)
  35.  
  36. Gui, Font, s15, Consolas                            ;Gui stuff
  37. Gui, Add, Edit, x0 y0 w500 h500 Disabled Border -VScroll vDisplay
  38. Gui, Add, Edit, x0 y500 w500 h100 Disabled Border -VScroll -Wrap vWordDisplay
  39. Gui, Font, s10, Consolas
  40. Gui, Add, Edit, Uppercase x0 y601 w500 h25 vUserinput
  41.  
  42. Gui, Show, w500 h625, Words With Enemies
  43.  
  44. SetTimer, Running, 1000                     ;Updates Display ever 100ms
  45.  
  46. Running:
  47. GuiControl,, Display, % Game.Display                       
  48. GuiControl,, WordDisplay, % "Your Score                       Enemy Score`n`n" . Game.Score . "                                " . Game.EnemyScore
  49. If (Game.Round > 5) {
  50.             MsgBox % Game.FinalScore()
  51.             Reload
  52.         }
  53. Return
  54.  
  55. #IfWinActive Words With Enemies ;If Gui is active
  56. ~Enter::
  57. Gui, Submit, NoHide
  58. If (Game.IsWord(UserInput) && Game.InLetters(UserInput) && UserInput != "") {  
  59.       Game.Play(UserInput)  
  60. }
  61. else {
  62.     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.
  63. }
  64.     GuiControl,, Userinput, % ""                    ;Clears UserInput
  65. Return
  66.  
  67.  
  68. Class WWE {
  69.    
  70.     __New(x, y, z) {                       ;Declare Globals
  71.         Global
  72.         this.Round := 1
  73.         this.wList := x
  74.         this.aList := y
  75.         this.Mode := z
  76.         this.Word :=
  77.         this.EnemyWord :=
  78.         this.DLetters :=
  79.         this.Letters := this.SetLetters()
  80.         this.Display := "Your Letters:`n" . this.DLetters
  81.         this.Score := 0
  82.         this.EnemyScore := 0   
  83.     }
  84.    
  85.     Play(x) {                                                   ;Determined order in which to run the various methods that make this game work.
  86.         this.EnemyWord := Format("{:U}", this.setEnemyWord())
  87.         this.battle(this.Word, this.EnemyWord)
  88.         this.Letters := this.SetLetters()
  89.         this.Display := this.UpdateDisplay()
  90.         this.Round++       
  91.         Return
  92.     }
  93.    
  94.     battle(x, y) {                                  ;Meat and bones of the Words With Enemies Easy Challenge, Cut down to fit this challenge.
  95.     For each, Char in StrSplit(x,,"`n") {
  96.         if InStr(y, SubStr(Char, 1,1)) {
  97.             x := StrReplace(x, Char,,, 1)
  98.             y := StrReplace(y, Char,,, 1)
  99.         }
  100.     }  
  101.     this.Update_Score(StrLen(x))
  102.     this.Update_EnemyScore(StrLen(y))
  103.     Return
  104.     }  
  105.  
  106.     InLetters(x) {
  107.         y := this.Letters  
  108.         For Each, Char in StrSplit(x,,"`r") {
  109.             if InStr(y, SubStr(Char, 1,1)) {
  110.                 x := StrReplace(x, Char,,, 1)
  111.                 y := StrReplace(y, Char,,, 1)
  112.             }
  113.         }
  114.         If (StrLen(x) != 0) {
  115.             Return False
  116.         }
  117.         Return True
  118.     }
  119.    
  120.     SetLetters() {  ;Random selection of letters
  121.         vowels := "aeiouy"
  122.         consonants := "bcdfghjkllmnpqrstvwxz"
  123.         vowels := StrSplit(vowels,, "`r")
  124.         consonants := StrSplit(consonants,, "`r")
  125.  
  126.         Loop, 14 {
  127.             Random, out, 1, % consonants.MaxIndex()
  128.             letters .= consonants[out]
  129.         }
  130.  
  131.         Loop, 6 {
  132.         Random, out, 1, % vowels.MaxIndex()
  133.         letters .= vowels[out]
  134.         letters := Format("{:U}", letters)
  135.         }
  136.         For each, Char in StrSplit(letters,, "`r") {
  137.             y .= Char . " "
  138.         }
  139.        
  140.         this.DLetters := y
  141.         Return letters
  142.     }
  143.    
  144.     UpdateDisplay() {   ;Main game window
  145.         x := this.Display
  146.         If (this.Round = 5) {
  147.             x .= "`nYour Word:" . this.Word . " vs Enemy Word:" . this.EnemyWord
  148.             Return x
  149.         }
  150.         x := this.Display
  151.         x .= "`nYour Word:" . this.Word . " vs Enemy Word:" . this.EnemyWord . "`n`nYour Letters:`n" . this.DLetters
  152.         Return x
  153.     }
  154.            
  155.     IsWord(x) {     ;If UserInput is a Word in List Return True
  156.         For each, Line in StrSplit(this.wList, "`n", "`r") {           
  157.             If  (x = Line) {
  158.                 this.Word := Format("{:U}", x)
  159.                 Return True
  160.             }          
  161.         }
  162.         Return False
  163.     }
  164.    
  165.     setEnemyWord() {                             ;Returns selected word based on Alpha Sorted list and Mode selected
  166.         sortedletters := AlphaSort(this.Letters)
  167.         z := StrSplit(this.wList, "`n", "`r")
  168.         For each, Line in StrSplit(this.aList, "`n", "`r") {
  169.             Count++
  170.             If (InStr(sortedletters, Line) && StrLen(Line) > 1) {
  171.                 x .= z[Count] . "`n"
  172.             }  
  173.         }
  174.         x := StrSplit(x, "`n", "`r")
  175.         y := x.MaxIndex()                         ;Get the Number of words in list
  176.        
  177.         If (this.Mode == "Hard") {               
  178.             Out := (y - 1)         
  179.         }
  180.         If (this.Mode == "Intermediate") {
  181.             Out := (y / 2)     
  182.         }
  183.         If (this.Mode == "Easy") {
  184.             Out := (y / 4)
  185.         }
  186.         Out := Round(Out)
  187.         Return x[Out]
  188.     }
  189.    
  190.     Update_Score(x) {   ;Update Player Score
  191.         this.Score += x
  192.         Return
  193.     }
  194.  
  195.     Update_EnemyScore(x) {  ;Updates Enemy Score
  196.         this.EnemyScore += x
  197.         Return
  198.     }
  199.    
  200.     FinalScore() {                            ;Compares score declares winner
  201.         Return x ((this.Score > this.EnemyScore) ? "You Won with a score of: " . this.Score
  202.         : (this.Score < this.EnemyScore) ? "The Enemy Won with a score of: " . this.EnemyScore
  203.         : "The Game was a Tie!")
  204.     }
  205. }
  206.  
  207. AlphaSort(n) {                            ;Sort function used to sort var by alphabet
  208.     delimiter := Chr(1)
  209.     delimited := RegExReplace(n, "(.)", "$1" . delimiter)
  210.     option = D%delimiter%
  211.     Sort delimited, %option%
  212.     StringReplace result, delimited, %delimiter%, , All
  213.     return result
  214. }
  215.  
  216. SortFunc(lineA, lineB, offset) {         ;Sort function used to sort list smallest to largest
  217.     if (StrLen(lineA) != StrLen(lineB) && StrLen(lineA) > 1)
  218.         return StrLen(lineA)-StrLen(lineB)
  219.     return -offset
  220. }
  221.  
  222. GuiClose:
  223. ExitApp
  224. return
Advertisement
Add Comment
Please, Sign In to add comment