Problem Description
Here
is a Delphi implementation of the classic electronic memory game,
Simon. The program produces increasingly long sequences of light and
tones which the player must then repeat. The first user error ends
the game.
Background & Techniques
This program was written and sent to me by Shane Holmes. I
made a couple of cosmetic changes, adding a track bar to replace numeric
input to set the speed at which the light/sound sequences are displayed
and inverting Shane's "High Scores" list so that the highest
scores appear at the top of the list.
Non-programmers are welcome to read on, but may
want to skip to the bottom of this page to
download an executable version of the program.
The code is pretty straightforward. TShape
components are used for the lights. A TObjectslist,
Lights, is used to hold the current sequence of lights by
filling it with TIntObj integer objects. The TIntObj
object is defined in the U_Common unit.
After the initial random entry is placed in Lights
by the start button click procedure, successive entries are generated each
time the user has correctly clicked the number of lights in the
list. When the user finally clicks an incorrect light, a loser
message is delivered and if the count is among the top 5 found so far, a
user name and score are added to the list of top scores.
The U_Common unit defines the TScores
object which maintains top scorers information. It is similar to my
TScores unit except that Shane uses file streaming to
save the high scores, while mine uses an "Ini" text
file. This makes the data in the list less prone to user
editing, which may or may not be an advantage. But is is
a good example of what is required to save an arbitrary set of integer and
string data to a file stream.
U_Common also contains a function, WinOpSys,
which returns the currently running version of Windows. I ended up
not using the function in this program but retained the code
because it might come in handy someday.
Addendum February 17, 2004: I
discovered that the Windows version of the Beep procedure used to generate
sounds for Simon didn't work at all well for Windows 95, 98, and ME.
The procedure reverts to a message beep for these systems which operates asynchronously,
meaning it does not wait for the beep to finish before continuing to
execute code. The for faster play speeds the result is that beeps are
lost making it very confusing to play. Today's version
uses a routine that generates tones of the proper length using the PC
speaker for Windows ME and lower. A big
improvement.
Running/Exploring the Program