Advertisement
Guest User

Game - Words With Enemies

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