You remember Simon, right? It spits of a random pattern of light/sound and you have to match that pattern to continue to the
next level. I think it starts with a one flash/tone pattern which increases by one everytime you complete the pattern
successfully. Anyway, for my first PhysComp assignment, I locked myself in the PhysComp lab and started plugging away to
make one from scratch.
The first thing I started with was connecting four LEDs to repeat a random pattern. The pattern was a string which
was randomly generated by me (e.g. 1443) and remained constant from game to game. After the LEDs were hooked up, I made
four makeshift switches out from red/blue wire. The switches acted like the buttons on Simon's exterior - touch the
switch after Simon has played it's code and your switch press is recorded. This is accomplished by setting two variables:
(a) one to count how many times you have pressed one of the four switches and (b) another that keeps track of the switch
the user pressed. Since the original random code was a string, the variable that was tracking what the user pressed was also a
string. The problem with this is that when the code entered by the user is the same length (four digits) as the code generated
by Simon, then the only way to see if the user is correct or not is to compare the two strings - something which the BasicX
does not support. After stressing over this for hours, Josh Nimoy posted a brilliant string conversion function to the PhysComp
listserv, which I eventaully used to solve my string comparison woes. Finally, the user could enter a code and have it compared
to random code. Once this worked, I hooked up a green to flash when the user got the puzzle right and a red LED to flash
when the user got the puzzle wrong.
Next was trying to dig up a way for Simon to generate the random number (remember: the number right now is the same every game...
it's just a string that I made up randomly and hardcoded into the app). Ideally, when Simon generates a puzzle for you, it
should be random. Since there is no random function available in BasicX, the trick is to come up with a way to generate a
random number, or in my case, four random numbers (for the four LEDs that light up to make the puzzle).
My first thought was to use some sort of timer to generate the random numbers. I was hoping I could use internal BasicX
functions to perhaps generate the time of day, extract a constantly changing number from this time (e.g. milliseconds), then
repeat this function four times to produce a pseudo-random four digit number. I didn't have much luck with this route as I
ended up running into roadblocks trying to compare the random number/code (integer) to the code that the user was entering by
pressing switches (a string).
... I'll write the rest of this later. Time to go to dinner.