Jump to content
Sign in to follow this  
Topher

The Word Game v1.6.07

Recommended Posts

Topher

The Word Game is a mastermind like game in which the player tries to determine a hidden word. The word is entered by another person or chosen randomly by the game. The player then guesses using a valid word and is told how many letters in their word is in the game's word. The player has twenty chances to guess the word. Try to improve upon your average number of guesses per round.

A valid word is an uncapitalized five letter word with no repeating letters. For example: READY, CHOMP and UNZIP are valid words. SALLY, RESET and FGHYU are not.

The Dictionary

Ta Da

Instructions

Code

#NoTrayIcon
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=backup\TWG.ico
#AutoIt3Wrapper_UseX64=n
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
;
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author:      Garth Bigelow
Script Function:
The Word Game
A Mastermind like game using five letter words
#ce ----------------------------------------------------------------------------
FileInstall("Dictionary.dat", "Dictionary.dat")
FileInstall("TaDa.wav", "TaDa.wav")
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
Global $Name = "The Word Game"
Global $NameVersion = $Name & " ver 1.6.07" ; Always placed here so I can quickly change the version number.
Global Const $GameFile = "game.txt"
Global Const $DictionaryFile = "Dictionary.dat"
Global Const $StatFile = "Stat.txt"
Global $GameID
Global $TheWord
Global $WordArray[21]
Global $Number
Global $ShowLetters = False
;Menu Variables
Global $hWord, $hStats, $hHelp, $hMain
Global Enum $idSetWord = 1000, $idRandomWord, $idGiveUp, $idResetAlphabet, $idSaveExit, $idViewStats, $idViewStatHistory, $idResetStats, $idAbout, $idHelp
Global $MenuActionFlag = 0
;Alphabet Variables
Dim $Alphabet[27]
Dim $AlphaState[27]
Dim $LetterButtonArray[21][6]
; Mouse Variables
Global $SingleClick = False, $DoubleClick = False
Global Const $DoubleClickTime = _GetDoubleClickTime()

; Only allow one instance of the program
If _Singleton($NameVersion, 1) = 0 Then
WinActivate($NameVersion)
MsgBox(262160, "Notice", $Name & " is already running.")
Exit
EndIf
; for double clicking on letters and menu commands
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
; create .ini file if needed
If FileExists("Config.ini") = 0 Then
; 0 - Select via Menu
; 1 - Enter Word Always
; 2 - Random Word Always
iniWrite("Config.ini", "Word", "Always", 0)
iniWrite("Config.ini", "Color", "OutColor", 0x666666)
iniWrite("Config.ini", "Color", "OutBKColor", 0xA5D2F2)
Endif
$iniWordFlag = IniRead("Config.ini", "Word", "Always", 0)
$LetterOutColor = IniRead("Config.ini", "Color", "OutColor", 0x666666)
$LetterOutBKColor = IniRead("Config.ini", "Color", "OutBKColor", 0xA5D2F2)
; loop for games played
GUISetHelp("The Word Game.pdf")
While True
Game()
If MsgBox(262180, $Name, "Fancy another game?") = 7 Then ExitLoop
WEnd
Func Game()
$Prep = True
$Number = 1
; Create Game Window
$GameID = GUICreate($NameVersion, 800, 565, -1, -1, BitOR($WS_CAPTION, $WS_SYSMENU))
GUISetBkColor(0xA5D2F2, $GameID)
If $GameID = 0 Then ErrorBox(@ScriptLineNumber, "Platform GUI can not be created")
GUISetState()
GUISetFont(14, 600, -1, "Courier New")
SystemMenu($GameID)
GUICtrlSetState($idResetAlphabet, $GUI_DISABLE)
GetSecretWord()
; Display Alphabet Buttons
Const $Square = 790 / 26
LoadAlphaStates()
For $Letter = 1 To 26
  $Alphabet[$Letter] = GUICtrlCreateButton(Chr(64 + $Letter), $Letter * $Square - $Square + 5, 10, $Square, $Square, BitOR($BS_NOTIFY, $BS_OWNERDRAW))
  SetAlphaColor($Letter, $Alphabet[$Letter])
Next
; Create Letters Array
For $a = 1 To 20
  For $b = 1 To 5
   $LetterButtonArray[$a][$b] = GUICtrlCreateButton("", ($b - .5) * 20, 25 + ($a * 24), 20, 20, BitOR($BS_NOTIFY, $BS_OWNERDRAW))
   GUICtrlSetState($LetterButtonArray[$a][$b], $GUI_HIDE)
  Next
Next
$Number = DisplayWords()
If $Number > 1 Then _GUICtrlMenu_EnableMenuItem($hWord, 6, 0)
;Main Loop
_GUICtrlMenu_EnableMenuItem($hWord, 0, 2)
_GUICtrlMenu_EnableMenuItem($hWord, 1, 2)
_GUICtrlMenu_EnableMenuItem($hWord, 3, 0)
_GUICtrlMenu_EnableMenuItem($hWord, 5, 0)
While True
  ; End game if the 20 guesses are exhausted
  If $Number = 21 Then
   MsgBox(262192, "You Lose", "Your 20 guesses are over." & @CRLF & "My word was " & $TheWord)
   UpdateStats(-1)
   DisplayStats()
   ExitLoop
  EndIf
  $msg = GUIGetMsg()
  DetectClicks()
  If $ShowLetters = True Then
   $ShowLetters = False
   For $Letter = 1 To 26
    SetAlphaColor($Letter, $Alphabet[$Letter])
   Next
   For $Row = 1 To $Number - 1
    DisplayWord($Row)
   Next
   SaveWordFile()
   GUICtrlSetState($WordArray[$Number], $GUI_FOCUS)
  EndIf
  ; allow entry of the guess word
  If $Prep = True Then
   $WordArray[$Number] = GUICtrlCreateInput("", 20, 25 + ($Number * 24), 80, 27)
   $Guess = $WordArray[$Number]
   GUICtrlSetBkColor(-1, 0xFFFFFF)
   GUICtrlSetColor(-1, 0x000000)
   $GuessButton = GUICtrlCreateButton("Guess", 110, 25 + ($Number * 24), 80, 27)
   $Prep = False
   GUICtrlSetState($WordArray[$Number], $GUI_FOCUS)
  EndIf
  ; compare guess word to secret word
  If $msg = $GuessButton Then
   _GUICtrlMenu_EnableMenuItem($hWord, 6, 0)
   GUICtrlSetData($Guess, StringUpper(GUICtrlRead($Guess)))
   ;other stuff
   If TestWord(GUICtrlRead($Guess)) = True Then
    $Prep = True
    GUICtrlSetState($GuessButton, $GUI_HIDE)
    GUICtrlSetState($Guess, $GUI_DISABLE)
    DisplayWord($Number)
    If GUICtrlRead($Guess) = $TheWord Then
     Tada()
     MsgBox(262192, "Congratulations", "That is my word" & @CRLF & "You guessed it in " & $Number & " Guesses")
     UpdateStats($Number)
     DisplayStats()
     ExitLoop
    EndIf
    SaveWordFile()
    $Number += 1
   Else
    MsgBox(262160, "Mistake", GUICtrlRead($Guess) & " is not a valid word." & @CRLF & "Press OK to try another word")
    GUICtrlSetData($Guess, "")
    GUICtrlSetState($WordArray[$Number], $GUI_FOCUS)
   EndIf
  EndIf
  MenuCommands()
  ; ends the game prematurely and displays the secret word
  ; unless the game is at the beginning, then just exit game
  If $MenuActionFlag = $idGiveUp Then
   $MenuActionFlag = 0
   If $TheWord <> "" Then
    If MsgBox(262180, "Confirmation", "Are you sure you want to give up?") = 7 Then ContinueLoop
    MsgBox(262160, "You Lose.", "My word was " & $TheWord)
    If $Number > 1 Then
     UpdateStats(-1)
     DisplayStats()
    EndIf
   EndIf
   ExitLoop
  EndIf
  ; reset the alphabet to alpha state 0
  ; used if you discover your work is wrong
  If $MenuActionFlag = $idResetAlphabet Then
   $MenuActionFlag = 0
   If MsgBox(262180, "Confirmation", "Are you sure you want to reset the alphabet") = 7 Then ContinueLoop
   For $a = 1 To 26
    $AlphaState[$a] = 0
    ;$Alphabet[$a] = GUICtrlCreateButton(Chr(64 + $a), $a*$Square-$Square+5, 45, $Square, $Square)
    SetAlphaColor($a, $Alphabet[$a])
   Next
   For $Row = 1 To $Number - 1
    DisplayWord($Row)
   Next
   SaveWordFile()
   GUICtrlSetState($WordArray[$Number], $GUI_FOCUS)
  EndIf
  If $msg = $GUI_EVENT_CLOSE Or $MenuActionFlag = $idSaveExit Then
   _GUICtrlMenu_SetMenu($GameID, 0)
   GUIDelete($GameID)
   Exit
  EndIf
  If $MenuActionFlag = $idHelp Then
   $MenuActionFlag = 0
   ShellExecute("The Word Game.pdf")
  Endif
WEnd
FileDelete($GameFile)
_GUICtrlMenu_SetMenu($GameID, 0)
GUIDelete($GameID)
EndFunc   ;==>Game
;
; Determines whether system has detected a single or double click but not both
;
Func DetectClicks()
If $SingleClick Then
  $DoubleClickPause = TimerInit()
  Do
   Sleep(10)
   If $DoubleClick Then
    ActOnClick($DoubleClick, 2)
    $SingleClick = False
    ExitLoop
   EndIf
  Until TimerDiff($DoubleClickPause) > 100;$DoubleClickTime
  $DoubleClick = False
  If $SingleClick Then
   If Not $DoubleClick Then ActOnClick($SingleClick, 1)
  EndIf
  $SingleClick = False
EndIf
EndFunc
;
; Increase In or Out or Unknown state of alphabet letters
;  by one of two states depending on single or double clicks
;
Func ActOnClick($nID, $Clicks)
For $a = 1 To 26
  If $nID = $Alphabet[$a] Then
   $AlphaState[$a] += $Clicks
   If $AlphaState[$a] > 2 Then $AlphaState[$a] -= 3
   $ShowLetters = True
   Return $GUI_RUNDEFMSG
  EndIf
Next
For $a = 1 To 20
  For $b = 1 To 5
   If $nID = $LetterButtonArray[$a][$b] Then
    $Letter = Asc(GUICtrlRead($LetterButtonArray[$a][$b])) - 64
    $AlphaState[$Letter] += $Clicks
    If $AlphaState[$Letter] > 2 Then $AlphaState[$Letter] -= 3
    $ShowLetters = True
    Return $GUI_RUNDEFMSG
   EndIf
  Next
Next
EndFunc
; Determine Maximum Speed of clicks to still be a double click
;
Func _GetDoubleClickTime()
    Local $aResult = DllCall("user32.dll", "uint", "GetDoubleClickTime")
    If @error Then Return SetError(1,0,100)
    Return $aResult[0]
EndFunc
;
; detects clicks on letter buttons
; directs menu commands
;
Func _WM_COMMAND($hWnd, $msg, $wParam, $lParam)
Local $nNotifyCode = BitShift($wParam, 16)
Local $nID = BitAND($wParam, 0x0000FFFF)
; Local $hCtrl = $lParam
; alphebet single and double click detection
Switch ($nNotifyCode)
  Case $BN_DBLCLK
   $DoubleClick = $nID
  Case $BN_CLICKED
   $SingleClick = $nID
EndSwitch
; menu commands
Switch _WinAPI_LoWord($wParam)
  Case $idSetWord
   $MenuActionFlag = $idSetWord
  Case $idRandomWord
   $MenuActionFlag = $idRandomWord
  Case $idResetAlphabet
   $MenuActionFlag = $idResetAlphabet
  Case $idSaveExit
   $MenuActionFlag = $idSaveExit
  Case $idGiveUp
   $MenuActionFlag = $idGiveUp
  Case $idViewStats
   $MenuActionFlag = $idViewStats
  Case $idViewStatHistory
   $MenuActionFlag = $idViewStatHistory
  Case $idResetStats
   $MenuActionFlag = $idResetStats
  Case $idAbout
   $MenuActionFlag = $idAbout
  Case $idHelp
   $MenuActionFlag = $idHelp
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND
;
; Respond to Menu Commands
;
Func MenuCommands()
Switch ($MenuActionFlag)
  Case $idViewStats
   DisplayStats()
  Case $idViewStatHistory
   DisplayStatHistory()
  Case $idResetStats
   If MsgBox(262180, "Confirmation", "Delete your previous statistics?") = 6 Then
    FileDelete($StatFile)
    MsgBox(262144, "Comment", "Previousd Statistics Deleted")
   EndIf
  Case $idAbout
   About()
  Case Else
   Return
EndSwitch
$MenuActionFlag = 0
EndFunc   ;==>MenuCommands
;
; Setup Main Menu Structure
;
Func SystemMenu($hGUI)
; Create File menu
$hWord = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_InsertMenuItem($hWord, 0, "Enter &Word", $idSetWord)
_GUICtrlMenu_InsertMenuItem($hWord, 1, "Set &Random Word", $idRandomWord)
_GUICtrlMenu_InsertMenuItem($hWord, 2, "", 0)
_GUICtrlMenu_InsertMenuItem($hWord, 3, "Reset &Alphabet", $idResetAlphabet)
_GUICtrlMenu_InsertMenuItem($hWord, 4, "", 0)
_GUICtrlMenu_InsertMenuItem($hWord, 5, "Save and E&xit", $idSaveExit)
_GUICtrlMenu_InsertMenuItem($hWord, 6, "&Give Up", $idGiveUp)
_GUICtrlMenu_EnableMenuItem($hWord, 3, 2)
_GUICtrlMenu_EnableMenuItem($hWord, 5, 2)
_GUICtrlMenu_EnableMenuItem($hWord, 6, 2)
_GUICtrlMenu_SetMenuStyle($hWord, $MNS_NOCHECK)
; Create Edit menu
$hStats = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_InsertMenuItem($hStats, 0, "&View Statistics", $idViewStats)
_GUICtrlMenu_InsertMenuItem($hStats, 1, "View Stat &History", $idViewStatHistory)
_GUICtrlMenu_InsertMenuItem($hStats, 2, "", 0)
_GUICtrlMenu_InsertMenuItem($hStats, 3, "Reset Statistics", $idResetStats)
_GUICtrlMenu_SetMenuStyle($hStats, $MNS_NOCHECK)
; Create Help menu
$hHelp = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_InsertMenuItem($hHelp, 0, "&Help", $idHelp)
_GUICtrlMenu_InsertMenuItem($hHelp, 1, "&About", $idAbout)
_GUICtrlMenu_SetMenuStyle($hHelp, $MNS_NOCHECK)
; Create Main menu
$hMain = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_InsertMenuItem($hMain, 0, "&Word", 0, $hWord)
_GUICtrlMenu_InsertMenuItem($hMain, 1, "&Stat", 0, $hStats)
_GUICtrlMenu_InsertMenuItem($hMain, 2, "&Help", 0, $hHelp)
; Set window menu
_GUICtrlMenu_SetMenu($hGUI, $hMain)
EndFunc   ;==>SystemMenu
;
; Credits
;
Func About()
Local $Display
$Display = $NameVersion & @CRLF & @CRLF
$Display &= "   - Written by Garth Bigelow" & @CRLF
$Display &= "   - with much patient help from the AutoIt help forums" & @CRLF & @CRLF
$Display &= "   - Copyright (c) 2012" & @CRLF
$Display &= "   - Program may be freely disseminated as long as no charge is levied." & @CRLF
MsgBox(262144, "About", $Display)
EndFunc   ;==>About
; SaveWordFile creates the game file which is used to continue a game if the game is closed while still in play
; $GameFile contains
;   The Secret Word
;   The Letter States (0,1 or 2) for Each Letter of the Alphabet
;   followed by each word guessed
; each entry on its own line
;
Func SaveWordFile()
$file = FileOpen($GameFile, 2) ; write fresh mode
; Check if file opened for reading OK
If $file = -1 Then
  MsgBox(0, @ScriptLineNumber, "Unable to open file.")
  Exit
EndIf
FileWriteLine($file, $TheWord)
For $Num = 1 To 26
  FileWriteLine($file, $AlphaState[$Num])
Next
For $Num = 1 To $Number
  If GUICtrlRead($WordArray[$Num]) >= "A" Then
   FileWriteLine($file, GUICtrlRead($WordArray[$Num]))
  EndIf
Next
FileClose($file)
EndFunc   ;==>SaveWordFile
;
; Tests whether $TestWord is in the dictionary
;   returns true if it is, false if it isn't
Func TestWord($TestWord)
$file = FileOpen($DictionaryFile, 0) ; read only mode
; Check if file opened for reading OK
If $file = -1 Then
  MsgBox(0, @ScriptLineNumber, "Unable to open file.")
  Exit
EndIf
; Read in lines of text until the EOF is reached
While True
  $word = FileReadLine($file)
  If @error = -1 Then ExitLoop
  $word = StringLeft($word, 5)
  If $word <> $TestWord Then ContinueLoop
  ; tidy up
  FileClose($file)
  ; valid word
  Return True
WEnd
; Tidy up
FileClose($file)
; Not a valid word
Return False
EndFunc   ;==>TestWord
;
; Returns the number of letters in the guess that are also in the secret word
;
Func GetCount($Char)
$Array1 = StringSplit($TheWord, "")
$Array2 = StringSplit(GUICtrlRead($WordArray[$Char]), "")
$Count = 0
For $a = 1 To 5
  For $b = 1 To 5
   If $Array1[$a] = $Array2[$b] Then $Count += 1
  Next
Next
Return $Count
EndFunc   ;==>GetCount
;
; Returns one random word from the dictionary
;
Func RandomWord()
$wordcount = 0
$file = FileOpen($DictionaryFile, 0) ; read only mode
; Check if file opened for reading OK
If $file = -1 Then
  MsgBox(0, @ScriptLineNumber, "Unable to open file.")
  Exit
EndIf
; count number of words
While True
  FileReadLine($file)
  If @error = -1 Then ExitLoop
  $wordcount += 1
WEnd
; record structure is a five letter word followed by a flag
;   if flag = 0 the word is common (and therefore eligible for being a secret word)
;          = 1 the word is rare
; keep picking random words until a common word is found
While True
  $ranword = FileReadLine($file, (Random(1, $wordcount - 1, 1)))
  If StringRight($ranword, 1) = 0 Then
   $ReturnWord = StringLeft($ranword, 5)
   ExitLoop
  EndIf
WEnd
FileClose($file)
; return the valid word
Return $ReturnWord
EndFunc   ;==>RandomWord
;
; Sets the secret word, either by manual entry or by random word from the dictionary
;
Func GetSecretWord()
$InitWord = ""
$file = ""
If FileExists($GameFile) Then
  $file = FileOpen($GameFile, 0) ; write read only
  ; Check if file opened for reading OK
  If $file = -1 Then
   MsgBox(0, @ScriptLineNumber, "Unable to open file.")
   Exit
  EndIf
  $TheWord = FileReadLine($file)
  FileClose($file)
  Return
EndIf
If Not FileExists($GameFile) Then
  While True
   $msg = GUIGetMsg()
   If $MenuActionFlag = $idSetWord Or $iniWordFlag = 1 Then
    $MenuActionFlag = 0
    $tempX = @DesktopWidth / 2 - 100
    $tempY = @DesktopHeight / 2 - 70
    $TheWord = InputBox("Select Word", "Enter The Game Word:", "", " M", -1, 140, $tempX, $tempY, 0, $GameID)
    If @error = 0 Then
     $TheWord = StringUpper($TheWord)
     If TestWord($TheWord) = True Then ExitLoop
     MsgBox(262160, "Word Problem", $TheWord & " is not a valid game word." & @CRLF & "Press OK to try another word")
    EndIf
   EndIf
   If $MenuActionFlag = $idRandomWord Or $iniWordFlag = 2 Then
    $MenuActionFlag = 0
    $TheWord = RandomWord()
    ExitLoop
   EndIf
   MenuCommands()
   If $msg = $GUI_EVENT_CLOSE Then Exit
  WEnd
EndIf
EndFunc   ;==>GetSecretWord
;
; Each letter of the alphabet has an associated state
;   0 - state is unknown
;   1 - letter is out
; 2 - letter is in
; This routine sets the states if a game is resumed
;
Func LoadAlphaStates()
If FileExists($GameFile) Then
  $file = FileOpen($GameFile, 0) ; write read only
  ; Check if file opened for reading OK
  If $file = -1 Then
   MsgBox(0, @ScriptLineNumber, "Unable to open file.")
   Exit
  EndIf
  For $Num = 1 To 26
   $AlphaState[$Num] = FileReadLine($file, $Num + 1)
  Next
  FileClose($file)
Else
  For $Num = 1 To 26
   $AlphaState[$Num] = 0
  Next
EndIf
EndFunc   ;==>LoadAlphaStates
;
; The states of the letters of the alphabet are displayed by a color
; This routine sets that color
;
Func SetAlphaColor($Char, $Object)
; debugging remove later
If $Char < 1 Or $Char > 26 Then Return
Switch $AlphaState[$Char]
  Case 0 ; Grey on Darker Gray
   GUICtrlSetBkColor($Object, 0xFFFFFF)
   GUICtrlSetColor($Object, 0x030203)
  Case 1 ; Disabled Mimic Colors
   GUICtrlSetBkColor($Object, $LetterOutBKColor)
   GUICtrlSetColor($Object, $LetterOutColor)
  ; GUICtrlSetBkColor($Object, 0xECF27E)
  ; GUICtrlSetColor($Object, 0x636162)
  Case 2 ; White on Red
   GUICtrlSetBkColor($Object, 0xFC0F03)
   GUICtrlSetColor($Object, 0xFFFFFF);  0xF4FC03)
EndSwitch
EndFunc   ;==>SetAlphaColor
;
; Display the word as a button for each letter and then
;   the number of letters between word and secret word
; Letters are displayed in their Alpha State colors
;
Func DisplayWord($Number)
; Remove Input Word Field
GUICtrlSetState($WordArray[$Number], $GUI_HIDE)
; Separate Word in Letters
$temp = GUICtrlRead($WordArray[$Number])
$Character = StringSplit($temp, "")
; Display Word
For $Num = 1 To 5
  GUICtrlSetData($LetterButtonArray[$Number][$Num], $Character[$Num])
  SetAlphaColor(Asc($Character[$Num]) - 64, $LetterButtonArray[$Number][$Num])
  GUICtrlSetState($LetterButtonArray[$Number][$Num], $GUI_SHOW)
Next
GUICtrlCreateLabel(" = " & GetCount($Number), 120, 23 + ($Number * 24), 90)
GUICtrlSetFont(-1, 17, 800)
; GUICtrlSetBkColor(-1, 0xE8E1E4)
GUICtrlSetColor(-1, 0x030203)
EndFunc   ;==>DisplayWord
;
; upon resuming the game the previous guesses are displayed
;
Func DisplayWords()
If Not FileExists($GameFile) Then Return 1
$file = FileOpen($GameFile, 0) ; read only
; Check if file opened for reading OK
If $file = -1 Then
  MsgBox(0, @ScriptLineNumber, "Unable to open file.")
  Exit
EndIf
$Count = 1
While True
  $word = FileReadLine($file, $Count + 27)
  If @error = -1 Then ExitLoop
  $WordArray[$Count] = GUICtrlCreateInput($word, 20, 25 + ($Count * 24), 80)
  DisplayWord($Count)
  $Count += 1
WEnd
FileClose($file)
Return $Count
EndFunc   ;==>DisplayWords
Func UpdateStats($guesses)
$file = FileOpen($StatFile, 1) ; write append
; Check if file opened for reading OK
If $file = -1 Then
  MsgBox(0, @ScriptLineNumber, "Unable to open file.")
  Exit
EndIf
FileWrite($file, $TheWord)
FileWrite($file, " ")
FileWriteLine($file, $guesses)
FileClose($file)
EndFunc   ;==>UpdateStats
Func DisplayStats()
Local $StatID, $file, $Wins, $Total, $Lost, $score, $msg
$file = FileOpen($StatFile, 0) ; read only
; Check if file opened for reading OK
If $file = -1 Then
  Return
EndIf
$Wins = 0
$Total = 0
$Lost = 0
While True
  $score = FileReadLine($file)
  If @error = -1 Then ExitLoop
  $score = StringRight($score, 2)
  If $score > 0 Then
   $Total += $score
   $Wins += 1
  Else
   If $score = -1 Then
    $Lost += 1
   EndIf
  EndIf
WEnd
$StatID = GUICreate("Statistics", 190, 110, -1, -1, BitOR($WS_CAPTION, $WS_SYSMENU, $WS_EX_TOPMOST), -1, $GameID)
GUISetFont(12, 800)
GUICtrlCreateLabel("Games Won:   " & $Wins, 10, 20)
If $Wins = 0 Then
  GUICtrlCreateLabel("Average Score: N/A", 10, 45)
Else
  GUICtrlCreateLabel("Average Score: " & Round($Total / ($Wins + $Lost), 2), 10, 45)
EndIf
GUICtrlCreateLabel("Games Lost:  " & $Lost, 10, 70)
GUISetState()
Do
  $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
GUIDelete($StatID)
EndFunc   ;==>DisplayStats
Func DisplayStatHistory()
Local $StatID, $StatEdit, $temp, $file, $line, $word, $score
$StatID = GUICreate("Statistics History", 230, 540, -1, -1, BitOR($WS_CAPTION, $WS_SYSMENU, $WS_EX_TOPMOST), -1, $GameID)
GUISetFont(14, 600, -1, "Courier New")
$temp = "  Word   Tries" & @CRLF & "  =====  ====="
$StatEdit = GUICtrlCreateEdit($temp, 10, 10, 210, 560)
GUISetState()
$file = FileOpen($StatFile, 0) ; read only
; Check if file opened for reading OK
If $file = -1 Then
  GUIDelete($StatID)
  Return
EndIf
; Determine Number of Words
; So that List can be lastest word first
$Count = 0
While True
  $line = FileReadLine($file)
  If @error = -1 Then ExitLoop
  $Count += 1
WEnd
; Exit if there are no words
If $Count = 0 Then
  GUIDelete($StatID)
  Return
EndIf
; Cycle thru the statistics file
While True
  $line = FileReadLine($file, $Count)
  $Count -= 1
  If $Count = 0 then ExitLoop
  ; ignore old style stat file entries
  If StringLen($line) < 5 Then ContinueLoop
  ; decode file line
  $word = StringLeft($line, 5)
  $score = StringRight($line, 2)
  ; display word
  $temp = GUICtrlRead($StatEdit)
  $temp &= @CRLF & "  " & $word & "  "
  ; display number of tries
  If $score = -1 Then
   $temp &= "Lost"
  Else
   ;   If $score < 10 Then $temp &= " "
   $temp &= "  " & $score
  EndIf
  GUICtrlSetData($StatEdit, $temp)
WEnd
Do
  $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
GUIDelete($StatID)
EndFunc   ;==>DisplayStatHistory
;
; Winning Hu-ra
;
Func Tada()
SoundPlay("TaDa.wav")
EndFunc   ;==>Tada
;
; Displays Error and Line of Error
;
Func ErrorBox($line, $content)
MsgBox(0, "Internal Error", $content & @CR & @CR & "Line: " & $line)
Exit
EndFunc   ;==>ErrorBox
Edited by Topher

[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • spikespaz
      By spikespaz
      The rule against game automation is misguided and unhelpful for the following reasons.
       
      I am a developer, using AutoIt to automate testing for a game that I am developing. Singleplayer games can be mundane and sometimes cheating can be overlooked. Multiplayer games usually have anticheat against repetitive robotic actions anyways. People want to override the control schemes of games with bad customization. Eg. Axiom Verge, Fortnite (Crouching). Game automation is not always bad. If the user feels the need to automate a singleplayer game for boring or mundane tasks, that is their choice. Similarly, if I wish to use scripts to automate testing, that is my choice.
      Game automation can be a problem for online multiplayer games, giving players a competitive advantage. This could be countered by common sense; ask OP what game they are automating, and is it an online game? What is this script for, and what does it seem to do?

      Please revise the rule as it seems very unnecessary and harmful to people seeking help with innocent attempts at game automation.
    • Barney
      By Barney
      Hi guys, I just finished a Connect 4 game by using MiniMax with Alpha Beta Pruning. I haven't written a program for a long time, but writing an
      AI program is always funny! I have to learn how the algorithm works and try to optimize the code to run faster. Let's play and have fun!

      Oops, I lost the game ...

      Thanks guys!
      Download: Connect 4.zip
    • Mannyfresh31
      By Mannyfresh31
      This is a game that I'm developing for Slot machines I just finished translating it to English my original version is in Spanish so if I miss spelled or translated something wrongly please accept my apologies I just releasing the Compile version this time just the .exe file Because I'm saving the code for myself since the software was originally written for my business or if someone wants it they have to pay but not you you getting it for free here at Autoit .
      once you run the software for first time is going to ask you for a license and it wont work without it you can get an Access code (license) by request just PM me and provide the code that the software will give when you press the cancel button three times.
      anyways once you're in the game the following hotkeys are set for interacting with it
      "q" for slot one or one credit
      "d" for slot two or five credits
      "1" or "2" or "3" or "4" or "5" to play simple, double, threeple, etc.  once you have credits of course
      "p" to stop the numbers once you're playing
      "c" for configurations or settings
      "i" for printing the receipt
      "s" for exit
      "t" for trading points for credits
       
      Link to download the game https://drive.google.com/open?id=1x22AM80fjrDjTwwAp_TqbdWyTabawoQX
       
      and here are some videos so you can see what the game is about
       
       
       
       
       
       
       
       
    • Slaventii_M
      By Slaventii_M
      Hi everybody!
      I've been studying Autoit not so long ago and today I want to share my game with you.
      Please check it out and tell me your opinion.
      Files in the archive:
      BG.jpg
      Mole.png
      Mole_Dead.png
      Shoot.wav
      Cursor.cur
      Game_Icon.iso
      Mole Shooter.au3
      Mole Shooter.exe
      Screenshots:
      Screenshot 1
      Screanshot 2
      Screanshot 3
      Files:
      Mole Shooter.rar
      Created and tested:
      Windows XP SP3 Game Edition (x86)
      Autoit Version: 3.3.10.2
    • seangriffin
      By seangriffin
      This is my first release of Box2D for AutoIT.  A very fun and exciting UDF to be bringing to you all.  For those who don't know, Box2D is a physics engine (see http://box2d.org/).  It simulates physical systems using rigid bodies and collision detection.  It is arguably the most prevalent physics engine in existence, within popular physics-based games like Angry Birds using it, and is also at the core of other advanced physics engines such as Unity.
      For a quick demonstration of this UDF in action see the following YouTube video ->  https://youtu.be/h5QH1O63Wik
      Or play "Angry Nerds", the demo game, for yourself.  Visit the EXAMPLES section below.
      Box2D is purely a mathematical engine.  It can model physics numerically.  This in itself is quite interesting, however it really shines when applied to a good graphics engine.  Recently I posted my SFML UDF over here ...
      After benchmarking several popular graphics engines (i.e. GDI+, Direct2D, Irrlicht and SFML) with this Box2D UDF I've selected SFML as my favourite, and the engine that I believe performs the best (fastest) with a rich set of functions appropriate for any physics engine.  With Box2D married with SFML, and running on AutoIT, the results are stunning.
      A HUGE THANK-YOU to jRowe and linus (for the Irrlicht UDF) and UEZ for his post on GDI+ loading screens and trancexx and Eukalyptus for their work on Direct2D.  Without their talents I would not have been able to reach this point on my own.
      The Box2D library is available for C++.  Way back in 2010 it was ported to Pure C by "Paril" and made available on Google Code and known as "Box2C".  Google Code has since been shut down but the but the archive of Box2C still exists to this day here -> https://code.google.com/archive/p/box2c.  This is the library which I have ported to AutoIT in this UDF.
      SFML I am also porting to AutoIT under a separate UDF, as linked above.
      Building this UDF has been a dream come true.  I've been fascinated by physics-based games as far back as the golden age of gaming in the 80's, with thrust style games like Asteroids and platformers like Donkey Kong.  I admired game developers like Jeremy Smith who created what may have been the first true game physics engines for the home computers.  I was astonished by their talents in games like Thrust and Exile for the C64.  Over the years I've attempted to mimic at least some of their success in my own games but alas I can not match their skills.  Now much older automation tools have become my game.  I use them almost every day, AutoIT included.  I've dabbled in other languages for physics game development, like Scratch and Unity, but sadly I had wondered why AutoIT, with all it's glorious capabilities and rapid scripting features, didn't have such a feature.  Hence this UDF.
      This UDF demands a big time investment, and I am time poor, but I have a keen interest in the topic and will do my best to continue it's development.  I am only a hobbyist game developer and welcome comments and suggestions from those far smarter than I on this topic.
      REQUIREMENTS:
      AutoIt3 3.2 or higher LIST OF FUNCTIONS (in progress):
      I've split this UDF into two halves.  "Box2C.au3" is a UDF specifically for Box2C, the C API for Box2D.  It provides the mathematics behind the engine.  Additionally I'm providing "Box2CEx.au3" as an Extension that provides the graphics and gaming functions for Box2D.
      Within the core "Box2C.au3" UDF:
      _Box2C_Startup _Box2C_Shutdown _Box2C_b2Vec2_Constructor _Box2C_b2Vec2_Length _Box2C_b2Vec2_Distance _Box2C_b2World_Constructor _Box2C_b2World_CreateBody _Box2C_b2World_DestroyBody _Box2C_b2World_CreateFixture _Box2C_b2World_CreateFixtureFromShape _Box2C_b2World_Step _Box2C_b2BoxShape_Constructor _Box2C_b2CircleShape_Constructor _Box2C_b2PolygonShape_Constructor _Box2C_b2PolygonShape_Set _Box2C_b2PolygonShape_CrossProductVectorScalar _Box2C_b2PolygonShape_CrossProductVectorVector _Box2C_b2PolygonShape_Normalize _Box2C_b2PolygonShape_ComputeCentroid _Box2C_b2BodyDef_Constructor _Box2C_b2Body_DestroyFixture _Box2C_b2Body_GetPosition _Box2C_b2Body_SetPosition _Box2C_b2Body_GetAngle _Box2C_b2Body_SetAngle _Box2C_b2Body_SetAwake _Box2C_b2Body_SetTransform _Box2C_b2Body_GetLinearVelocity _Box2C_b2Body_SetLinearVelocity _Box2C_b2Body_GetAngularVelocity _Box2C_b2Body_SetAngularVelocity _Box2C_b2Body_ApplyForce _Box2C_b2Body_ApplyForceAtBody _Box2C_b2Body_ApplyDirectionalForceAtBody _Box2C_b2Body_ApplyTorque _Box2C_b2Fixture_GetShape _Box2C_b2Fixture_GetDensity _Box2C_b2Fixture_SetDensity _Box2C_b2Fixture_GetRestitution _Box2C_b2Fixture_SetRestitution _Box2C_b2Fixture_GetFriction _Box2C_b2Fixture_SetFriction _Box2C_b2Fixture_SetSensor Within y "Box2CEx.au3" extension:
      x_metres_to_gui_x y_metres_to_gui_y metres_to_pixels atan2 radians_to_degrees degrees_to_radians _Box2C_Setup_SFML _Box2C_b2Vec2_GetGUIPosition _Box2C_b2World_Setup _Box2C_b2World_GDIPlusSetup _Box2C_b2World_SetPixelsPerMetre _Box2C_b2World_SetGUIArea _Box2C_b2World_GetGUIArea _Box2C_b2World_GetGUIAreaCenter _Box2C_b2World_Create _Box2C_b2World_Step_Ex _Box2C_b2World_StartAnimation _Box2C_b2World_Animate_SFML _Box2C_b2World_Animate_GDIPlus _Box2C_b2World_WaitForAnimateEnd _Box2C_b2ShapeArray_AddItem_SFML _Box2C_b2ShapeArray_SetItem_SFML _Box2C_b2ShapeArray_GetItemImagePath_SFML _Box2C_b2PolygonShape_ArrayAdd_Irrlicht _Box2C_b2PolygonShape_ArrayAdd_GDIPlus _Box2C_b2BodyDefArray_AddItem _Box2C_b2FixtureArray_SetItemSensor _Box2C_b2FixtureArray_GetItemDensity _Box2C_b2FixtureArray_SetItemDensity _Box2C_b2FixtureArray_GetItemRestitution _Box2C_b2FixtureArray_SetItemRestitution _Box2C_b2FixtureArray_GetItemFriction _Box2C_b2FixtureArray_SetItemFriction _Box2C_b2BodyArray_AddItem_SFML _Box2C_b2Body_ArrayAdd_Irrlicht _Box2C_b2Body_ArrayAdd_GDIPlus _Box2C_b2BodyArray_GetItemCount _Box2C_b2BodyArray_GetItemPosition _Box2C_b2BodyArray_SetItemPosition _Box2C_b2BodyArray_GetItemAngle _Box2C_b2BodyArray_SetItemAngle _Box2C_b2BodyArray_GetItemLinearVelocity _Box2C_b2BodyArray_SetItemActive _Box2C_b2BodyArray_SetItemAwake _Box2C_b2BodyArray_SetItemImage_SFML _Box2C_b2BodyArray_SetItemDraw _Box2C_b2BodyArray_ApplyItemForceAtBody _Box2C_b2BodyArray_ApplyItemDirectionalForceAtBody _Box2C_b2BodyArray_Transform_SFML _Box2C_b2Body_Transform_GDIPlus _Box2C_b2BodyArray_Draw_SFML _Box2C_b2Body_ArrayDrawDisplay_SFML _Box2C_b2Body_Destroy _Box2C_b2Body_Destroy_SFML _Box2C_b2Body_Rotate_GDIPlus The SFML functions used in the tests and demos will be available in the SFML UDF post (see reference above).
      EXAMPLES:
      For such a powerful physics engine coupled to such a powerful graphics engine (SFML) it's sad that I've only had time to build one functional game, thus far.  But it's a start.
      My self-titled "Angry Nerds" is merely a demo of the same game concept as the ever-so-popular Angry Birds game.  Angry Birds itself is built on top of Box2D.  Likewise Angry Nerds.  AutoIT + Box2D + SFML to be exact.  I've compiled Angry Nerds and provided an installer also, so you can quickly run the demo for yourself.  From the Github site below (in the DOWNLOAD section) run "Box2C_Angry_Nerds_Game_SFML_installer.exe" to install the demo to a location on your computer (desktop by default).  Go into this folder and run "Box2C_Angry_Nerds_Game_SFML.exe".  All instructions are displayed in-game.  Should be quite easy to work out.
      Aside from Angry Nerds there are also two test scripts:
      Box2C_linear_forces_test_SFML.au3 Box2C_angular_forces_test_SFML.au3 Feel free to run these for a basic demonstration of rigid bodies, forces and collisions.  The heart of Box2D and any physics engine.
      Lastly I also have four speed tests as follows:
      Box2C_speed_test_SFML.au3 Box2C_speed_test_Irrlicht.au3 Box2C_speed_test_D2D.au3 Box2C_speed_test_GDIPlus.au3 These were my initial evaluations into a suitable graphics engine for Box2D.  I've since settled on SFML, but feel free to execute these.  Note they may become quick defective over time as the SFML functions slowly take over.
      DOWNLOADS:
      You can download this UDF, including the examples above and associated files, from the following GitHub page:
      https://github.com/seanhaydongriffin/Box2C-UDF
      Hope you all enjoy!  I certainly am :-)
      Cheers,
      Sean.
       
×