Noughts and Crosses

This simple game has been around for a long time and has many different names. Lewis Caroll called it "oughts and crosses" and complained when some people added an N to the front. Others might call it "Tic Tac Toe". Whatever name you call it by, it is one of the simplest games to get a computer to play.

While my JavaScript version dates only from 2007, this was in fact the first game that I taught a computer to play long before I ever got access to an actual computer. Back in 1975 I "taught" a computer consisting of eleven matchboxes how to play this game in a way that it would always win or draw. To play the matchbox computer you selected the matchbox that had the position including your latest move shown on it in black with the various responses shown in different colours. By selecting a coloured match from the matchbox the matchbox computer made its move.

I also remember seeing a computer from the early 1960s set up in a museum where the only thing that the computer did was to play this game - and it always won because if the game was headed for a draw it would cheat.

To copy this game to use on your own web page involves just three simple steps. First simply copy the following CSS and attach it into the head of your page:

#ox {width:156px;overflow:hidden;}
#ox button {width:50px;height:50px;float:left;margin:1px;}
#ox .brox {clear:left;}

Next place the following HTML where you want the game to appear in your page:

<div id="ox"></div>

Finally, copy the following JavaScript and attach it to the bottom of your page immediately prior to the </body> tag:


That's all you need to do. The game should now work on your page for anyone with JavaScript enabled.

Note that this latest version of the game is not only unobtrusive, it also uses the new strict version of JavaScript. I have also rewritten parts of the code to make the entire script significantly shorter than it was previously. You will also notice that the code to test if the person playing the computer has won is commented out. You can uncomment this code if you want but the only impact will be to slow the game down by a very small amount as there will never be a situation where that if statement returns true.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow