In the classic hand-held electronic game Simon, the device played a sequence of 'moves' and the players task was to remember and reproduce the sequence. Each of the four possible moves had a different colour (red, yellow, blue, green) associated with a different physical position and with a different audible tone. If the player reproduced the sequence correctly, the device played the sequence again with one more move added, and as the sequence lengthened, each move was shown for a shorter duration. If the player made a single incorrect move the game was over and the score reflected the longest length of sequence that the player reproduced correctly. The game was surprisingly engaging, and proved very popular.
We are going to build a simulation of the Simon game, with the four moves shown as large coloured circles in a row accompanied by audible tones. The player will use the four numeric keys 1 2 3 4 to input their memory of the sequence - the numbers corresponding to the four positions in the row.
We will make more than one use of a 'List' data structure in this program. A List is similar to an Array, but may be extended indefinitely (within in the limits of a computer's memory). A list may even start off empty and grow from there.
Although the game will display only one circle at a time, we want to position it
in one of four places in a row, with a different colour for each place.
We already know that we will be using the 1 2 3 4 keys so, for
the time being, we will simply define a name
Note that it doesn't matter which value (1 to 4) is specified for
Whenever you find yourself with multiple instructions that together perform something that you could put a single name on, it is often good practice to move them into a named procedure. This keeps your code easier to read and to maintain. It is even more useful if you subsequently find yourself wanting to perform that same thing elsewhere in the code.
Now we're ready to complete the role of Simon (played by the computer) in the game, which is to repeatedly play a sequence of moves, adding a new random move each time. (We will later add the Player's role, interleaving between these plays). To keep the growing program easy to read, we will build the Simon's role into a new procdure.
We now need a loop that requires the player to reproduce each move in the sequence.
We can determine the length of the sequence using
Three bits of polishing are urgent - but write down anything else you can think of: