Autoit Bejeweled bot tutorial part 2: identifying the gem play field

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

Last time we have set up a framework for our Bejeweled bot and written the first code to locate the Bejeweled window. At this stage we know which window to work with, so we can locate the gem play field and calculate the gem positions.


Identifying the gem play field

The possible solutions

Several methods to position the gem play field exist; they all have their advantages and disadvantages. Possible methods to determine the gem play field borders are:

  1. Fixed start coordinates and fixed end coordinates: start with the x,y coordinates of both the upper left and lower right side of the field.
  2. Fixed start coordinates and fixed dimensions: start with the x,y coordinates of the upper left side of the field together with the length/height of the field.
  3. Searching both start and end coordinates: start with searching for the color which marks both the start and the end of the field on the screen.
I opted for the third method, as that is the most flexible one. Each site can have its bejeweled game positioned on a different location. By searching both start and end each time, we can be sure to have the correct position every time.

Searching the play field corners

In order to search the gem play field, we will search both the upper left and lower right corner of the field. Once we have their coordinates, we can calculate the other corners ourself.

bejeweled gem play fieldbejeweled bot first cornerbejeweled bot last corner








Add following key to the configuration file:

cornerColor1=0x313129
cornerColor4=0x313129
In order to read the newly added configuration keys, add following code to the declaration section:

$cornerColor1=IniRead ( $iniFile, "Common" , "cornerColor1", "0x313029" )
$cornerColor4=IniRead ( $iniFile, "Common" , "cornerColor4", "0x313029" )
Next we will prepare the variables in which to store the coordinates of the four corners. We will also create variables representing three gems: the upper left, upper right, and lower left gem. With these corner and gem locations we can easily process the play field. Add following code to the declaration section

$corner1=_arrayCreate("","")
$corner2=_arrayCreate("","")
$corner3=_arrayCreate("","")
$corner4=_arrayCreate("","")

$Field1=_arrayCreate("","")
$Field2=_arrayCreate("","")
$Field3=_arrayCreate("","")

$BlockWidth = 0
Now it's time to search the screen for the color specified in the ini file. Function 'pixelSearch' returns the x,y coordinates of the first found pixel matching that color, from left to right and top to bottom. The coordinates are stored in the &Corner1 variable. If the color cannot be found, the bot stops, else the mouse pointer is moved to the found pixel. Add following code to the main section:

_log("Searching top left position of playfield")
ToolTip('Searching top left position pixel with color ' & hex($CornerColor1, 6) , 0, 0)

$Corner1 = pixelSearch(0,0,@DesktopWidth,@DesktopHeight, $CornerColor1,0)
If @error Then
msgbox(0,"","Top Left pixel with color: " & hex($CornerColor1, 6) & " not found")
exit
Else
MouseMove($corner1[0],$corner1[1])
endif
Once we have found the first corner we can look for the fourth corner, which is located at the lower bottom of the play field. Again, if the color cannot be found, the bot stops, else the mouse pointer is moved to the found pixel. Add following code to the main section:

_log("Searching bottom right position of playfield")
ToolTip('Searching bottom right position pixel with color ' & hex($CornerColor4, 6) , 0, 0)

$Corner4=_pixelSearchReverse(0,0,@DesktopWidth,@DesktopHeight, $CornerColor4)
If @error Then
msgbox(0,"","Bottom Right pixel with color: " & hex($CornerColor4, 6) & " not found")
exit
Else
MouseMove($corner4[0],$corner4[1])
endif
Function _pixelSearchReverse doesn't come with Autoit by default. Add this function to the function section:

Func _pixelSearchReverse($left,$top,$right,$bottom,$color)
$coordFoundColor=_ArrayCreate("-1","-1")
$i = 0
$j = 0

for $i = @DesktopWidth to 0 Step -1
for $j = @DesktopHeight to 0 Step -1

$currentcolor = PixelGetColor($i,$j)
if $currentcolor = $color then
$coordFoundColor[0] = $i
$coordFoundColor[1] = $j
return $coordFoundColor
endif

next
next
SetError(-1)
endFunc
With help of corner1 and corner4, we can calculate the x,y coordinates of the other corners, corner2 and corner4. Add this code to the main section:

;Calculate position of remaining corners
$Corner2[0]=$Corner4[0]
$Corner2[1]=$Corner1[1]

$Corner3[0]=$Corner1[0]
$Corner3[1]=$Corner4[1]

Calculating the gem positions

With help of the corner positions we can now calculate the position of three main gems: the upper left, upper right, and lower left gem. Soon we will be needing this exact position of these gems in order to process all gems on the play field.

Add following code to the declaration section:

$AmountGems=IniRead ( $iniFile, "Common" , "AmountGems", 0 )
First we will calculate the distance between each gem. Add following code to the main section:

;Calculate distance between center of each gem
$BlockWidth = ($Corner2[0] - $Corner1[0]) / $AmountGems
Next we will calculate the position of the three main gems. Add following code to the main section:

$Field1[0]=$Corner1[0] + ($BlockWidth / 2)
$Field1[1]=$Corner1[1] + ($BlockWidth / 2)

$Field2[0]=$Corner2[0] - ($BlockWidth / 2)
$Field2[1]=$Corner1[1] + ($BlockWidth / 2)

$Field3[0]=$Corner3[0] + ($BlockWidth / 2)
$Field3[1]=$Corner3[1] - ($BlockWidth / 2)

Proof running the code

At this point you should be able to run you script and see more 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. Next the mouse pointer is moved to the upper left corner of the play field, followed by the lower right corner of the play field. Don't worry; searching for the right bottom pixel might take a while. Feel free to improve the _pixelSearchReverse function, which will speed up the initialization process a lot!

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 creating the main loop for the bot and implement the function to read the gem colors.

Continue to the third part of the bejeweled bot tutorial: reading the gem colors

Related Posts by Categories

Comments

Recent Articles

Top Commenters

Recent Comments