Autoit Bejeweled bot tutorial part 1: bot framework & preparation

AutoIt Bejeweled bot tutorial part 1This article makes part of the Bejeweled bot tutorial series.

In this part we will set up a framework for our bot script. We will create the necessary files for our bot and add basic functionality to the script such as logging and configuration. Once we're set it's time to start with the real programming work.

Preparation

Bot files

The bot consists out of three main files:
The script file (.au3)
A configuration file (.ini)
A log file (.txt)
Create a new Autoit script file and give it a useful name. Next create a configuration file and give it the same name as your script. You can do this by creating a new text file and changing the file extension to .ini. You don't need to create the log file, it will be generated automatically.

Now add following configuration section and key to the configuration file:
[Common]
amountGems=8
windowBejeweled=SpelSpelen.com - Bejeweled

Code sections

I divided the script in three main code sections. Whenever I display new code, you need to add the code to the right section. I'll always indicate either the section, or the exact location to insert your code at. Now copy the section comments below into your script file. If I mention the section only, then add the code beneath the last added code of that section.
;----------------------- TESTINGMINDED -----------------------
;---------- bot tutorial from www.testingminded.com ----------

;-------------------- DECLARATION SECTION --------------------

;-------------------- MAIN SECTION ---------------------------

;-------------------- FUNCTION SECTION -----------------------

Basic functions

Before writing the bejeweled specific code we'll first add basic functionality to the script. We will be using Array and File functions so you need to link to those external libraries. Add following code to the declaration section:
#include 
#include 
Note1: the code boxes seem to include closing tags, which I didn't wrote, for the include statements. So please don't mind and don't use those include closing tags, for instance line 3 in above code box.

We need to have a possibility to pause or stop the bot. Therefore add following code to the declaration section
Global $paused ;sets up pause hotkey
HotKeySet("{ESC}", "_quit") ;set escape hotkey to exit
HotKeySet("{PAUSE}", "_pause") ;set pasue hotkey to pause
And add following code to the function section:
;makes a quit function
Func _quit()
Exit
EndFunc

;makes a pause function
Func _pause()
$paused = Not $paused
While $paused
Sleep(100)
ToolTip('Script is "Paused, to unpause press pause key"', 0, 0)
WEnd
ToolTip("")
EndFunc
For debugging purpose I recommend to have an application log, therefore add following code to the function section:
Func _Log($message)
_FileWriteLog(@scriptDir & "\output.txt", $message)
EndFunc
Note2: the code boxes seem to comment out some parts of the code :-) In order be sure you write the code correctly, click the "view plain" link to see the original and correct code.

We'll create a first log statement to indicate what the bot is doing. Add following code to the declaration section:
_log("Initializing bejeweled bot...")
The configuration file in which we store the bot settings must exist. To check that, we will add following code to the declaration section:
$iniFile=stringleft(@ScriptName,stringlen(@scriptname)-4) & ".ini"
if not FileExists($iniFile) Then
msgbox(0,"","The ini file could not be found at following location: " & $iniFile)
exit
EndIf

Finding the bejeweled window

Now we have everything in place to write our first bot specific code. Open a browser window and navigate to http://www.spelspelen.com/play/bejeweled. The window should display the bejeweled game. We will now write code to tell the script which browser window to use. The script will then activate the bejeweled browser window.

We'll specify the title of the page which is displaying the bejeweled game to the configuration file. The title is usually displayed in the title bar at the top of your browser window. Writing down a partial name will do. Add following key to the configuration file:
windowBejeweled=SpelSpelen.com - Bejeweled
In order to read the newly added configuration key, add following code to the declaration section:
$windowBejeweled=IniRead ( $iniFile, "Common" , "windowBejeweled", "" )
Now we'll add code to search and activate that browser window. Add following code to the main section
if winactivate($windowBejeweled) == 0 then
msgbox(0,"","Window " & $windowBejeweled & " could not be found.")
exit
endif

Proof running the code

At this stage, you should be able to run your script and actually see results. Save your script and configuration file, and run the bot script by pressing F5 in your ScITE editor. If you correctly followed all steps, the bejeweled browser window becomes active. If not, double check that your configuration file exists and contains the section and all keys mentioned in above steps.

If you encounter difficulties with the execution of these steps and you don't succeed in fixing the problem yourself, then leave a message in the comment section. Don't forget to include the error message shown in the ScITE output window. I will answer to your question as soon as possible.

In the next part of this tutorial we will be positioning the gem play field and calculating the gem dimensions.

Continue to the second part of the bejeweled bot tutorial: identifying the gem play field

Related Posts by Categories

Comments

Recent Articles

Top Commenters

Recent Comments