Liquid Digital Lab

Simple AI in Director

Posted 3 years, 2 months ago by Simon

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.

Leave a Comment

Three D Clock

Classic clock built in processing

Posted by Lloyd - Comments (0)

This Happened #5

Lloyd gets chance to go along to the 5th gathering of This Happened

Posted by Lloyd - Comments (0)

Tom survives in one piece!

Tom makes it back to the office through 19 peaks over 149 miles, 60 flood warnings, 60mph gusts and 48hours of torrential rain, all in the name of MS Society, Wheels4Life and a weekend to remember!

Posted by Tom - 3 Comments (3)

Liquid goes Coast to Coast

Inspired by his managers previous fundraising wetsuit exploits, our newest designer Tom will be tackling the coast to coast bike ride this September.

Posted by Tom - Comments (1)

Liquid Artwork

Have a look at the artwork we created for the front office

Posted by Lloyd - 3 Comments (3)

Decisions, decisions… A Bit More Game AI

Game AI goes a few steps forwards, and 2000 years into the past…

Posted by Simon - 3 Comments (3)

Ever Won The Lottery?

Winning The Big One!
I recently read somewhere that winning the UK national lottery is easy. All you have to do is …

Posted by Simon - Comments (1)

3DS Max - Basics and Blueprints

The start of a series of tutorials in 3ds max from Liquids latest addition to the creative team.

Posted by Lloyd - 9 Comments (9)

Sound Reactive Sketch

We created this experimental Music Visualiser in processing & minim sound library.

Posted by Lloyd - 2 Comments (2)

Pathfinding in Games - Using A* in Director

Discover the joys of the A* pathfinding algorithm in our retro Director maze

Posted by Simon - Comments (1)

Colour Clock Screensaver

We’ve made an animated clock screensaver. You can have it too.

Posted by Ben - 2 Comments (2)

Using the Flash Tween Class

Trying to create realistic bouncing balls or elastic objects in Flash? Ben Stevens shows us the easy way to code motion using the Flash Tween Class.

Posted by Ben - 6 Comments (6)

A Jungle Jumble Using Lists in Director

What do you get if you cross a monkey, an elephant and a giant chicken-thing? Simon Edwards tries to explain using lists in Director.

Posted by Simon - 3 Comments (3)

Lloyd Survives the 2007 London Triathlon

Liquid’s very own budding triathlete successfully completes the Michelob ULTRA London Triathlon in a time of 2 hours and 42 minutes. Find out how he got on.

Posted by Lloyd - Comments (1)

Simple AI in Director

Simon Edwards discusses 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.

Posted by Simon - Comments (0)

Ruby on Rails Tutorial - An Online DVD Catalogue

John Cove takes us through the process of making an online catalogue using RoR.

Posted by John - Comments (0)

Why write an ActiveX control?

Howard Peters discusses the advantages of using ActiveX, and explains how to create your own simple ActiveX control.

Posted by Howard - Comments (0)

Nichola’s Placement Year

Nichola, our resident student placement, has now been with us for a whole year and will be leaving us soon. So we asked her what it’s been like to work with the Liquid team.

Posted by Nichola - Comments (0)

Kinetica Soundwaves Exhibition

Exhibition visit

Posted by Ben - Comments (0)

PHP Tutorial - Creating a Quiz

John Cove explains how to use HTML and PHP to create an online quiz.

Posted by John - Comments (0)

Director Tutorial - Vector Font Outlines

Simon Edwards demonstrates an undocumented Director feature enabling you to map out a string of text in a vector cast member.

Posted by Simon - Comments (0)

HDR Photography

HDR (High Dynamic Range), is a software based, photographic technique that extracts tonal information from multiple exposures of the same shot. If you exposure bracket the same shot, you can merge all 3 images into one, then, using the data from all 3, create one output file which preserves accurate details from all exposures, bringing out highlights from the shadows.

Posted by Darren - Comments (0)

Happy Birthday To Us!

Liquid Digital Ltd has reached the end of its first financial year. There’s no time for cake and a candle though - there are some big things on the horizon for the first quarter of Year 2. Watch this space for news soon.

Posted by Simon - Comments (0)

Director Tutorial - Counting Down To A Launch Date

Tutorial written by our resident Director Guru, Simon Edwards.

Posted by Simon - Comments (0)

Lloyd in a wetsuit

Our creative leader will be braving the elements by competing in the Michelbob ULTRA London Triathlon on 4th August 2007.

Posted by Ben - Comments (0)