Newsreel |
|
|
|
Processing begins with a block of inline JScript code in the
<HEAD> section, executed once when the
page loads. This creates the game's custom data objects and history
session arrays, and sets system
values such as the points awarded for the four possible card combinations.
When this code finishes executing, the game does nothing, while waiting
for the <body> tag's onload event to fire.
The <body> tag's onload event handler is the function waitToStart()., which monitors these three asynchronous start-of-run processes:
waitToStart() doesn't let the game start until all three processes are complete. Each time it's called, it checks to see if any of them are still unfinished, and if so, creates a setTimeout() event to call itself again one second later. When all three processes are done, waitToStart() calls the function initpage() instead.
initpage() performs further start-of-run tasks, which require the
waitToStart() processes to be completed first. When it ends, the page
enters a dormant state, with no functions running. However the game control
panel is visible, which means that the user can select game options ( left-hand player, high-speed play
etc) then press the Start Game
button. This calls the functions initGame().
initGame() does the following:
It then calls the function initRound().

initRound() increments the current round number and displays 'no card' images in bother player columns. It then fires off asynchronous player processes for the left and right hand players (click here for details).
The game is now in a dormant state again, which will be punctuated by two calls to the function processCard(), as the two players play their cards. processCard() doesn't do much; it simply displays a 'back-of-card' image in the appropriate column, and stores the player's option (cooperate or defect) in a variable. However it also checks to see if both players have now played their cards for this round. If they have, it calls the end-of-round function processRound().

processRound() does the following:
It then checks to see if this was the last round of the game. If not, it calls initRound() for he next round of the game. If this was the last round, it calls endGame() instead.

endGame() updates the statistics session data arrays, and displays an 'End of game' message. If the game is in self-play (computer vs computer) mode, it checks to see if this was the last game of the set (in this mode the user can tell the page to play a number of games between the chosen strategies). If this was the non-last game of a self-play set, it increments the game counter and calls initGame(). If not, it makes the game control panel visible, and the page enters a dormant state, ready for the user's options.
|
DNJ Dilemma |
Overview of the game's processes
Indirectly-called strategy functions
Asynchronous left/right players