Make your own free website on
Fred JavaScript Nim

Hi, welcome to my Javascript Nim page. Nim is a simple little game (mainly of interest to mathematicians) and is played with heaps of matches (or coins, or anything really). You start with any number of heaps containing any number of matches, and two players take it in turn to remove any number of matches from any one heap. The winner is the player who removes the final match(es).

For example, if we have heaps of size 6,3,2 and 4, then I may choose to take 2 matches from the heap of size 6, or I may choose to take all 4 of the matches from the heap of size 4.

You can play Nim against the computer at the bottom of this page. Simply click on 'New Game' to begin. The computer will pick some random heaps with up to 15 matches in each. You take matches from the piles by changing the number in one of the piles and clicking on 'Move'. The computer will check if your move is legal, and then play it's reply.

Watch out though, if you make a mistake the computer will beat you. It's also kind enough to tell you when it is going to win, and when it thinks it is loosing.

Once you are beating the computer you should be able to keep on beating it until the end of the game. Once the computer starts winning, there's not a lot you can do I'm afraid.

Heaps :

[Note : You must have JavaScript enabled in you browser for this game to work]

If you are interesting in how this works, and understand JavaScript, just view the source for this page - the code is all there with comments. I haven't explained exactly how the computer wins though - you will have to work that out for yourself.