in AI / ML

AI: Bejeweled

After WordGame I took a few week break and sat down to automate a Bejeweled clone. Again I took a HTML/JS version of the game because interjection JS is pretty easy. I liked the version by alkaruno.

Making AI possible

I actually didn’t change much. I only changed a bit of the cosmetic because I was writing it and starting at the screen for a while I changed the graphics to just simple colors and the background to white – so that it was less heavy. Then I injected my ai.js and it ran.

The Algorithm

Here’s the general idea: We try out all the moves and find the move which maximizes our additional points.

I just adopted the scoring function used by the game. Therefore, we have perfect knowledge about the score of our action. Next I found all possible moves. In case you never played a game like this. You can switch bricks. But a switch is only legal if it causes a line (horizontal or vertical) of 3 bricks to connect.

So, we see what happens if we swap positions. I recorded the swaps in a map / object. Then I calculated the resulting points and compared it to the current best move, so far.

You can see that the moves is optimal for each round, however it doesn’t have to be optimal in the long run. New bricks will be added randomly and these could change the outcome.

And that’s the algorithm basically.

Then I injected it into the game and added buttons for starting and stopping the AI. Also I created a slider which adjusted the time the AI stops before making a move. You can see in the video that if I make the delay between moves too low the game bugs out and square won’t get filled up.


I recorded this video like usually with recordit.

Write a Comment