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

17 Responses to "Autoit Bejeweled bot tutorial part 1: bot framework & preparation"

Jason Palchak said... June 3, 2010 at 7:11 AM

Hello!

I'm using your bejewled tuorial in a reverse "engineering manner" to create bots for this: http://yppedia.puzzlepirates.com/Bilging

and

this: http://yppedia.puzzlepirates.com/Treasure_Haul

puzzle.

I'm starting with Treasure Haul since it's the simplest one. Clear pieces as fast as you can! Almost the same as Bejewled (except vertical xD)

I'm having issues on how I'd have the program target the window.

Currently I have this: windowPuzzlePirates -PuzzlePirates in the .ini file and

$windowPuzzlePirates=IniRead ($iniFile, "Common" , "windowPuzzlePirates", "" )
In the code. Would this be correct?

In addition I'm recieving this error: The ini file could not be found at following location: TH Bot.ini

What does this mean?

Here's a copy of my source code so far:





#cs ----------------------------------------------------------------------------
PLINKO!-Treasure Haul Bot v1.0
AutoIt Version: 3.3.0.0
Author: Jason Frankiewicz-Palchak : Sage Man

Script Function:
Template AutoIt script.

#ce ----------------------------------------------------------------------------


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

#include
#include

Global $paused ;sets up pause hotkey
HotKeySet("{ESC}", "_quit") ;set escape hotkey to exit
HotKeySet("{PAUSE}", "_pause") ;set pasue hotkey to pause

_log("Initializing PLINKO! Prepare to PWN!...")

$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

$windowPuzzlePirates=IniRead ( $iniFile, "Common" , "windowPuzzlePirates", "" )

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

if winactivate($windowPuzzlePirates) == 0 then
msgbox(0,"","Window " & $windowPuzzlePirates & " could not be found.")
exit
endif


;-------------------- 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

Func _Log($message)
_FileWriteLog(@scriptDir & "\output.txt", $message)
EndFunc

Steven Machtelinckx said... June 3, 2010 at 7:51 AM

Hi Jason,

The script looks for an ini file having the same name as your script file. What's the name of your ini file? It should be 'TH Bot.ini'.

Jason Palchak said... June 3, 2010 at 3:32 PM

Yes the ini file has the same name as the script, and is indeed titled 'TH Bot.ini'

Does it matter the location of the ini file?

Steven Machtelinckx said... June 3, 2010 at 3:41 PM

Yes, the code now checks for the ini file in the same directory as the script file.

Jason Palchak said... June 3, 2010 at 4:03 PM

So if both are on my desktop, theoretically there should be no issue?

Steven Machtelinckx said... June 3, 2010 at 4:13 PM

You are right. Is it working?

If not, then you can mail me a zip archive with your script and ini file. I'll have a look at it.

Jason Palchak said... June 3, 2010 at 7:24 PM

What's your e-mail?

Steven Machtelinckx said... June 3, 2010 at 7:38 PM

You can find my email address behind the "contact me" link.

Steven Machtelinckx said... June 4, 2010 at 7:55 AM

Jason,

Your ini file has the ".txt" extension: TH Bot.ini.txt
Just remove the ".txt" from the filename and you should be fine.
If you don't see the ".txt" extension then open the ini file and "save as" the file as "TH BOT.ini"

Anonymous said... September 9, 2010 at 10:11 AM

bot2.au3 (4) : ==> Missing separator character after keyword.:


</file^ ERROR
i am geting this error any help?

Anonymous said... September 15, 2010 at 8:24 PM

#include
#include
Global $paused ;sets up pause HotKeySet
HotKeySet("{ESC}", "quit") ;set escapse hotkey to Exit
HotKeySet("{PAUSE}", "_pause") ;set pause HotKeySet

;makes a quit function
Func _quit()
Exit
EndFunc

;makes a puase function
Func _pause()
$paused = Not $paused
While $paused
sleep(100)
ToolTip(' is "Paused, to upause press pause key"',0,0
WEnd
ToolTip("")
EndFunc

;log File
Func _log($message)
_FileWriteLog(@scriptDir &* "\output.txt", $message)
EndFunc

_log("Inittalizing bejeweled bot...")

$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

windowBejeweled=facebook.com - Bejeweled

$windowBejeweled=IniRead ($iniFile, "common" , "windoeBejeweled", "" )

if winactivate($windowBejeweled)== 0 Then
msgbox(0,"","window " & $windowBejeweled & " could not be found.")
exit
endIf

i'm getting this error down below

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\jake\Desktop\bejewled bot\bot.au3"
C:\Users\jake\Desktop\bejewled bot\bot.au3 (35) : ==> Missing separator character after keyword.:
windowBejeweled=facebook.com - Bejeweled:
windowBejeweled^ ERROR
>Exit code: 1 Time: 0.221

Anonymous said... September 15, 2010 at 8:35 PM

;-----Declaration------
#include
#include
Global $paused ;sets up pause HotKeySet
HotKeySet("{ESC}", "quit") ;set escapse hotkey to Exit
HotKeySet("{PAUSE}", "_pause") ;set pause HotKeySet

_log("Inittalizing bejeweled bot...")

$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

$windowBejeweled=IniRead ($iniFile, "common" , "windowBejeweled", "" )
windowBejeweled= SpelSpelen.com - Bejeweled
;------main

if winactivate($windowBejeweled)== 0 Then
msgbox(0,"","window " & $windowBejeweled & " could not be found.")
exit
endIf


;-----function-----
;makes a quit function
Func _quit()
Exit
EndFunc

;makes a puase function
Func _pause()
$paused = Not $paused
While $paused
sleep(100)
ToolTip(' is "Paused, to upause press pause key"',0,0
WEnd
ToolTip("")
EndFunc

;log File
Func _log($message)
_FileWriteLog(@scriptDir &* "\output.txt", $message)
EndFunc

i'm getting this error down below

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\jake\Desktop\bejewled bot\bot.au3"
C:\Users\jake\Desktop\bejewled bot\bot.au3 (17) : ==> Missing separator character after keyword.:
windowBejeweled= SpelSpelen.com - Bejeweled
windowBejeweled^ ERROR
>Exit code: 1 Time: 0.226

Anonymous said... September 15, 2010 at 8:53 PM

i got it working but it can't find the game any help it just says window spelspelen.com can't be found

TestWithUs said... May 7, 2013 at 6:05 AM

SWIFT Interview questions on

http://testwithus.blogspot.in/p/swift.htm

For selenium solution visit
http://testwithus.blogspot.in/p/blog-page.html


For QTP interview questions

http://testwithus.blogspot.in/p/qtp-questions.html


www.searchyourpolicy.com




zero said... March 3, 2015 at 4:31 AM

Is it possible to make a roulette bot using the methods in this tut?

Steven M said... March 3, 2015 at 2:25 PM

A Roulette bot should be feasible with autoit. The question is: what do you want to achieve? Even if you used a roulette bot, you'd still have to spend money to spin the wheel, right?

zero said... March 4, 2015 at 4:16 AM

Thanks for the reply. Well, I wanted the roulette bot to function like the one in this video but with slight differences. https://www.youtube.com/watch?v=PdEWjR8N_YU

Post a Comment

Recent Articles

Top Commenters

Recent Comments