Autoit Bejeweled bot tutorial part 4: switching the gems

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

This final article on my bejeweled bot is about checking the possible moves, followed by switching the gems. We could say that this is one of the most crucial factors in the bot creation process.


Decision making

There are several ways to decide which gems to move first. You could look at the amount of points to earn for each move, and then move the gem which gives you the maximum of points. Also you could look at the cascade effect your move is possibly going to produce.

This bot is applying a fairly easy and straightforward approach though. We define the possible gem patterns for identical gems and scan the gem play field for gems matching these patterns. The order of the patterns we look for defines the importancy of the move. We start by checking the patterns which will give us more points. The eventual cascade effect is not calculated, but for each pattern we start checking the lowest gems first and continue towards the top.

Now roll up your sleeves and prepare yourself for the coding.

We start by creating the clickDelay definition. Add following key to the configuration file:

moveClickDelay=0
And add this code to the declaration section:

$moveClickDelay=IniRead ( $iniFile, "Common" , "moveClickDelay", 0 )
Next we will implement the first decision making code. The variables firstGem and secondGem will contain the x and y coordinates of both gems to be switched, once we find matching gems. The switched boolean is put to false by default, and will keep track of switched gems during this iteration. If no gems were switched yet, we can continue to check for different patterns, otherwhise start over by checking for the first pattern again.

We start by looping through the gem play field and fetching the gem colors of the gems we need to check against the pattern. Looping happens vertically from bottop to top, and horizontally from right to left. In this first case below we fetch the gem colors of 4 adjacent gems and check whether the first, second and fourth gems match. If they match, we store their x and y coordinates and switch the gems. If not, we check whether the first, third and fourth gems match, and so on.

Add following code to the main section, on the place of the placeholder for the movement of gems:

local $firstGem[2] = [0, 0]
local $secondGem[2] = [0, 0]

if $switched == false then
for $j = ($amountGems - 1) to 0 step -1
for $i = ($amountGems - 4) to 0 step -1

$ColorGem1=$field[$i][$j]
$ColorGem2=$field[$i+1][$j]
$ColorGem3=$field[$i+2][$j]
$ColorGem4=$field[$i+3][$j]

;XXOX
if _ColorsMatch($ColorGem1, $ColorGem2, $ColorGem4)then
$firstGem[0] = $i + 2
$firstGem[1] = $j
$secondGem[0] = $i + 3
$secondGem[1] = $j
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

;XOXX
if _ColorsMatch($ColorGem1, $ColorGem3, $ColorGem4)then
$firstGem[0] = $i
$firstGem[1] = $j
$secondGem[0] = $i + 1
$secondGem[1] = $j
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

next
next

endif
The other patterns are quite similar so I won't comment them. Add this code to the main section as well, right below the code you just added:

if $switched == false then
for $j = ($amountGems - 4) to 0 step -1
for $i = ($amountGems - 1) to 0 step -1

$ColorGem1=$field[$i][$j]
$ColorGem2=$field[$i][$j+1]
$ColorGem3=$field[$i][$j+2]
$ColorGem4=$field[$i][$j+3]

;X
;X
;O
;X
if _ColorsMatch($ColorGem1, $ColorGem2, $ColorGem4)then
$firstGem[0] = $i
$firstGem[1] = $j + 2
$secondGem[0] = $i
$secondGem[1] = $j + 3
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

;X
;O
;X
;X
if _ColorsMatch($ColorGem1, $ColorGem3, $ColorGem4)then
$firstGem[0] = $i
$firstGem[1] = $j
$secondGem[0] = $i
$secondGem[1] = $j + 1
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

next
next
endif

if $switched == false then
for $j = ($amountGems - 2) to 0 step -1
for $i = ($amountGems - 3) to 0 step -1

$ColorGem1=$field[$i][$j]
$ColorGem2=$field[$i+1][$j]
$ColorGem3=$field[$i+2][$j]
$ColorGem4=$field[$i][$j+1]
$ColorGem5=$field[$i+1][$j+1]
$ColorGem6=$field[$i+2][$j+1]

;XXO or ;OOX
;OOX ;XXO
if _ColorsMatch($ColorGem1, $ColorGem2, $ColorGem6) or _ColorsMatch($ColorGem4, $ColorGem5, $ColorGem3) then
$firstGem[0] = $i + 2
$firstGem[1] = $j
$secondGem[0] = $i + 2
$secondGem[1] = $j + 1
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

;XOX or ;OXO
;OXO ;XOX
if _ColorsMatch($ColorGem1, $ColorGem3, $ColorGem5) or _ColorsMatch($ColorGem4, $ColorGem6, $ColorGem2) then
$firstGem[0] = $i + 1
$firstGem[1] = $j
$secondGem[0] = $i + 1
$secondGem[1] = $j + 1
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

;OXX or ;XOO
;XOO ;OXX
if _ColorsMatch($ColorGem2, $ColorGem3, $ColorGem4) or _ColorsMatch($ColorGem1, $ColorGem5, $ColorGem6) then
$firstGem[0] = $i
$firstGem[1] = $j
$secondGem[0] = $i
$secondGem[1] = $j + 1
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

next
next
endif


if $switched == false then
for $j = ($amountGems - 3) to 0 step -1
for $i = ($amountGems - 2) to 0 step -1

$ColorGem1=$field[$i][$j]
$ColorGem2=$field[$i][$j+1]
$ColorGem3=$field[$i][$j+2]
$ColorGem4=$field[$i+1][$j]
$ColorGem5=$field[$i+1][$j+1]
$ColorGem6=$field[$i+1][$j+2]

;XO or ;OX
;XO ;OX
;OX ;XO
if _ColorsMatch($ColorGem1, $ColorGem2, $ColorGem6) or _ColorsMatch($ColorGem3, $ColorGem4, $ColorGem5) then
$firstGem[0] = $i
$firstGem[1] = $j + 2
$secondGem[0] = $i + 1
$secondGem[1] = $j + 2
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

;XO or ;OX
;OX ;XO
;XO ;OX
if _ColorsMatch($ColorGem1, $ColorGem3, $ColorGem5) or _ColorsMatch($ColorGem4, $ColorGem6, $ColorGem2) then
$firstGem[0] = $i
$firstGem[1] = $j + 1
$secondGem[0] = $i + 1
$secondGem[1] = $j + 1
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif

;OX or ;XO
;XO ;OX
;XO ;OX
if _ColorsMatch($ColorGem2, $ColorGem3, $ColorGem4) or _ColorsMatch($ColorGem1, $ColorGem5, $ColorGem6) then
$firstGem[0] = $i
$firstGem[1] = $j
$secondGem[0] = $i + 1
$secondGem[1] = $j
$switched = _Switch($firstGem, $secondGem)
exitLoop 2
endif


next
next

endif

Checking gem colors

Identical gems are recognized based on their color. This bot doesn't apply any safety margin. Only when the color dots have exactly the same code we consider them as matching.

Add following code to the function section:

Func _ColorsMatch($color1, $color2, $color3)
$colorsEqual = false

if $color1 = $color2 and $color1 = $color3 then
_Log("The colors matched exactly: " & hex($color1))
$colorsEqual = true
return $colorsEqual
endif

EndFunc
It's important to know that this approach is not error proof and only suitable for Bejeweled I. Therefore I have upgraded this approach in my Bejeweled Blitz bot by applying color ranges, which also works for Bejeweled 2. In the future I will discuss this so called "color range approach". Meanwhile I'll give you something to chew on by showing an extract of the configuration file of my Bejeweled Blitz bot.

[colors]
red=0xFE9191:0xFE4141
orange=0xF8B84E:0xEF7422
white=0xFEFEFE:0x909090

Switching the gems

Actually switching the gems is the last part of the chain and is not complicated at all. The bot clicks two times: once on the first gem and once on the second gem. Add following code to the function section:

Func _Switch($firstGem, $secondGem)
mouseclick("left",$Field1[0] + ($BlockWidth * $firstGem[0]) ,$Field1[1] + ($BlockWidth * $firstGem[1]),1,$MoveClickDelay)
mouseclick("left",$Field1[0] + ($BlockWidth * $secondGem[0]) ,$Field1[1] + ($BlockWidth * $secondGem[1]),1,$MoveClickDelay)
return True
EndFunc

Proof running the code

At this point you should again be able to run you script and see the bot working. 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 bot should be performing all steps from previous parts, and the bot should perform correct moves. If you find the bot clicking faster than the gem play field replenishes after switching gems, you can increase the searchDelay timeout in the configuration file.

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.

This was the last article in the Bejeweled bot tutorial series. Though, you can expect an article on the implementation of the color range approach for the Bejeweled Blitz Bot. Check out testingminded regularly for updates or subscribe to our RSS feed.

Related Posts by Categories

Comments

Recent Articles

Top Commenters

Recent Comments