In this article I’d like to start discussing some of the techniques that developers call upon when programming games, particularly for defining logic that the computer will use for taking a turn in a 2-player game scenario. A recent search of my hard drive threw up a project from many years ago which uses a method of decision-making often referred to as weighting.
Put simply, it’s a technique that looks at every possible action and awards/deducts points based on certain criteria, eventually settling for the move with the highest score.
For instance, if you’re simulating chess, you would have the computer quickly rattle through every possible move - any moves that result in capturing an opponents piece would score points, probably weighting important pieces to score higher than others, with maximum points for check or check-mate. If the move placed the computer’s piece in jeopardy you’d deduct points, possibly with the same hierarchical weighting according to the importance of the piece as before. It’s also good practice to ‘look ahead’ a few moves and see if you might be placing a piece in possible jeopardy.
Simple AI like this allows you to set a personality for the computer - by changing the ‘rules’ or weighting criteria you can make the computer seems more aggressive or defensive, which is a great way for the computer to seem to learn from experience and change habits depending on whether it’s winning or losing with a particular opponent.
I’ve kept code out of this tutorial since it’s more about the technique than the syntax you’d use. As a hint, though, I’d tend to use an array with an item representing each possible move, or maybe each piece on the board, and then loop through the list applying each rule at a time. After the repeat loop, the highest scoring item wins and that particular move is made. Other code would ‘teach’ the computer whether to use the defensive or aggressive rules, although often I’d have several states in between and just use variables to adjust the points awarded for capturing opponents and such like.
Although I’ve used this technique in some fairly complex games, I thought I’d share an old game produced for WHSmith a few years back on a touchscreen playtable, installed in several airport shops to entertain the kids while Mum & Dad chose their holiday reading material. I know from the software logs that this was a particularly popular game and received a great deal of playtime. It’s a take on traditional noughts & crosses, affectionately titled ‘Nautical Crosses’ and themed as an underwater battle between various sea-themed tokens (long before the days of Sponge Bob). Rather than edit the original software to simplify the relevance to this tutorial, I’ve left it as is, so there’s a choice of 1 or 2 player games. Obviously it’s the 1 player game against the computer that we’re interested in at the moment.
When the computer takes a turn. It ‘thinks’ through the various options awarding each square a point value. This is weighting in its simplest form - an array with 9 items is used, and a repeat loop steps through each item. If placing a token in a square isn’t possible (i.e. it’s already been used) then no points are awarded, if the square is empty then 1 point is given. If using that square places the opposition in danger of losing then an additional point is added, but if using the square stops the opposition from winning on the next turn then two points are given, making the computer defensively-minded. After the loop is done, the highest scoring square receives the computer’s token. In the event of a draw then one is picked randomly.
It would have been possible to develop the AI to allow the opponent to win at first and then appear to improve the computer’s decision making over time, however this may have made the game appear too easy to start with and the player may have gone on to play something else before the learning nature kicked in enough to make the game interesting. The computer could have altered it’s focus on attack/defence during the game but I think it’s important to remember that this is only noughts and crosses, and the target audience were young children! It wasn’t our aim to make the games build in this way due to the environment and the expected dwell time. Besides, there were 5 other games on the unit for them to try and we were trying to encourage them to play them all in the time they had before catching their flights.
Incidentally, although not necessarily part of the AI, I should mention that in a game like this, arrays (or lists as Director calls them) are important for many things, especially checking if a winning line has been completed.
I hope soon to write a tutorial about another game I have in the archives that uses weighting in a slightly more complex way, and also to talk about some of the other aspects of game programming, some of which are evident in nautical crosses, such as animating assets in a game with code, adding extras such as sound effects and high score tables, and perhaps non-code related matters such as taking simple ideas and adding appeal with the use of metaphor and game play ideas.
For now though, enjoy the game! You’ll need shockwave installed since it was made in Director MX. The source code isn’t included since the game was a commercial project, but the discussion of the techniques should prove useful for anyone with programming experience in any language starting out with game programming.







