Saturday, January 26, 2013

Still Moving

Another day, another opportunity for hacking!  Actually, we had a little ice event here on Friday, so this particular Saturday was a great day to stay inside for some retro-hacking.  Luckily, we had already stocked-up on bread and milk... :-)

Random Numbers

Simon is built around a randomly generated sequence of tones/colors that the player has to match to keep playing.  However, the truth is that computers aren't really imaginative enough to generate truly random numbers.  To compensate for that, there are a variety of common tricks that are used to generate seemingly random numbers through mathematical processes.  The technique I used for generating random numbers in Fahrfall  was a software implementation of an LFSR.

Perhaps surprisingly, Simon did something even simpler.  Simon used a free running counter which was incremented every time the game entered its main loop.  When it was time to extend the tone/color sequence, Simon simply used the two lowest-order bits of that counter in order to pick one of the four tone/color pairs (numbered 0-3).  The inherent inconsistency of human timing makes it impossible to predict which number is in the machine's counter when these choices are made, resulting in something random enough to be useful for the game.  This technique has worked well enough for Simon so far, so this is the technique I used in my implementation as well.

Getting Started

One thing I added to my code was a "start" screen.  This gives the user more control over exactly when a game starts, and offers an opportunity for displaying copyright notices or perhaps some basic instructions.  In this case, however, it provides another added benefit -- it gives the free running counter a chance to run for a while before the game starts.  Without this chance for the counter to run, the game would always start with the same tone/color pair every time.

Follow The Leader

With all that said, the point of the random numbers is to generate a sequence of tone/color pairs.  But having that sequence isn't worth much if you can't play it back for the game!  I already had some code to play tones as the player selected each color.  On top of that, I implemented some code to track the length of a sequence and to walk each member of it.  For each member of the sequence, the corresponding tone is played and its color is highlighted.  The tone playback is timed, and a short delay is inserted between each tone.


With that in place, I can move onto playing a generated sequence and adding the rest of the game logic.  I expect to get that done over the next day or so.  As always, stay tuned... :-)

No comments:

Post a Comment