Example Program 5 (Part 2)

Before we look at the JavaScript code for the game you should perhaps play to the game to try it out and then we will examine the code in detail.

Pyramid of matches

Click here to play the Nim Game (JavaScript)

This program shows an example of modular programming with the extensive use of functions and examples of parameter passing.

In the main program there are two key functions:

  • GetStartingValue () ;
  • PlayGame ( ) ;

Within the PlayGame ( ) function there are calls to the other functions that make up the program. These are:

  • ComputersTurn () ;
  • PlayersTurn () ;
  • CheckForWin (PlayerID) ;

One final function is called within PlayersTurn, that is named ValidateData (Minimum, Maximum).

We will examine each function separately before looking at how they work together.

Next: Example Program 5 (Part 3)