Online Game AI Coding Techniques

Published: 27th January 2012
Views: N/A

Online games are becoming increasingly easy to program. Adobe Flash and, more recently, the emergence of HTML5, have made easier some of the technical aspects of producing a game for the web. Sites with thousands of relatively polished Flash games are becoming increasingly common. However, creating the artificial intelligence (AI) that provides a fun and challenging experience is not easy and many games with an otherwise excellent game experience fall short when it comes to providing a sufficiently challenging opponent for a human player to challenge. I enjoy playing puzzle and board games online Scrabble, Checkers or Chess online than I am to play an action or arcade style game. As many games as there are out there it’s surprisingly hard to find new games with an AI that is sufficiently challenging and fun. This is probably because it is a lot easier to build a game like Tetris, where a player works to overcome a static challenge like aligning rows of tiles, than to build a dynamic AI like the one required for a 1-player Chess game. By employing two important coding techniques, building an intelligent puzzle game opponent can be much more straight-forward.


Recursion is a coding technique that can be used to conceptually simplify a complex problem, such as identifying the next best move for a computer player. “Recursion” is the process of repeating items in a self-similar way. For instance, imagine that you are organizing a treasure hunt for your child’s birthday. The first clue, which you give to them directly, is a hint to the location of the second clue. The second clue has a hint to the location of the third clue, the third clue has a hint to the location of the fourth clue, the fourth clue has a hint to the location of the fifth clue, which, finally, will have a hint to the location of the treasure. The way I’ve just described the hunt is redundant, overly complicated and confusing. The description can be simplified by simply saying “Each clue will lead you to the location of another clue, perpetually, until a final clue leads you to the treasure’s location”. This comes naturally when giving instructions to people but not so naturally when giving instructions to the computer.

In coding the AI for an online board game such as checkers, recursion can be particularly helpful. Once the human player has made the game’s first move, the computer opponent must choose an optimal second move. In order to do this, it can “think” much like a the human player does, examining possible next moves, imagining the opponent’s counter-moves to those moves, imagining their own counter-move to the opponent’s counter-moves and so on for several moves. In this way, recursion can be used to construct a list of possible move sequences in a conceptually simple way that doesn’t require too much coding complexity. For each sequence of possible future moves the hypothetical board can be scored and the first move of the outcome that most favors the computer player can be chosen.


Always choosing the optimal move for a computer player can often make game play too difficult. Calibrating the difficulty of the AI can be achieved via the computer’s ability to choose random numbers. Using the recursive technique above, the logic for each computer move results in ranking of possible future outcomes of the next few moves, from those that favor the computer player the most to those that favor it the least. Choosing the first move of the most optimal path will make the computer player most likely to win but we can make the game less difficult by choosing the first move of some other path. We could always choose the 5th or 10th best move but that could result in play that was overly predictable between games. By randomly choosing between the top, say, 10 most optimal paths, the AI can assuage some of the difficulty without becoming too predictable.

Writing a one-player puzzle game with an artificial intelligence that is fun to face off against is not easy but is made much easier by using recursion and calibrating the difficulty using randomization.

An example of a game that utilizes these techniques is called the 3 Elements Game, an online board game that can be played for free at

Report this article Ask About This Article

More to Explore