Coding help
I am building a little Simon Says memory game for a class of mine. It has three lights that flash a pattern, then a user parrots back the pattern with three buttons. The game evaluates the button presses and indicates whether you advance to the next level or you have to repeat the level. I have posted my code below, and then I have a question or two underneath. Any and all advice would be appreciated.
'light wiring
' PIN1 - Amber
' PIN2 - Red
' PIN3 - Green
'button wiring
' PIN9· - amber button
' PIN10 - red button
' PIN11 - green
ROUND VAR Nib
press1 VAR Nib
press2 VAR Nib
press3 VAR Nib
press4 VAR Nib
press5 VAR Nib
press6 VAR Nib
answer1 VAR Nib
answer2 VAR Nib
answer3 VAR Nib
answer4 VAR Nib
answer5 VAR Nib
answer6 VAR Nib
INIT:
answer1 = 0
answer2 = 0
answer3 = 0
answer4 = 0
answer5 = 0
answer6 = 0
ROUND = 1
' NEEDS TO BE RANDOM
GENERATE_SEQUENCE6:
· press1 = 2
· press2 = 3
· press3 = 1
· press4 = 2
· press5 = 3
· press6 = 1
GOTO BLINK
GENERATE_SEQUENCE1:
· press1 = 1
· press2 = 0
· press3 = 0
· press4 = 0
· press5 = 0
· press6 = 0
GOTO BLINK
GENERATE_SEQUENCE2:
· press1 = 1
· press2 = 3
· press3 = 0
· press4 = 0
· press5 = 0
· press6 = 0
GOTO BLINK
BLINK:
· HIGH· press1
· PAUSE 250
· LOW press1
IF ROUND > 1 THEN
· HIGH· press2
· PAUSE 250
· LOW press2
ENDIF
IF ROUND > 2 THEN
· HIGH· press3
· PAUSE 250
· LOW press3
ENDIF
· HIGH· press4
· PAUSE 250
· LOW press4
· HIGH· press5
· PAUSE 250
· LOW press5
· HIGH· press6
· PAUSE 250
· LOW press6
READ_PRESSES:
·FIRSTPRESS:
· IF IN9 = 1 THEN
··· answer1 = 1
· ELSEIF IN10 = 1 THEN
··· answer1 = 2
· ELSEIF IN11 = 1 THEN
··· answer1 = 3
· ENDIF
· IF answer1 <> 0· THEN
·· IF ROUND > 1 THEN
···· GOTO SECONDPRESS
· ELSEIF
·· ' MEANS WE GOT OUR ASWER AND THERE IS NO NEXT ROUND AND WE NEED TO EVALUATE THE ANSWER
·· 'EVALUATE -
··· IF answer1 = press1 THEN
···· 'we got it right and the game is over
··· ELSEIF answer1 = NOT press1 THEN
····· GOTO firstpress
···· ' wrong answer - do whatever
··· ENDIF
·· 'PLAYSOUND
· ENDIF
· GOTO· FIRSTPRESS
·SECONDPRESS:
· IF IN9 = 1 THEN
··· answer2 = 1
· ELSEIF IN10 = 1 THEN
··· answer2 = 2
· ELSEIF IN11 = 1 THEN
··· answer2 = 3
· ENDIF
· IF answer1 <> 0 THEN
·· IF ROUND > 2 THEN
···· GOTO THIRDPRESS
· ELSEIF answer1 = press1 AND answer2 = press2 THEN·· 'EVALUATE
··· DEBUG "Correct!"································ 'we got it right and the game is over
· ELSEIF NOT answer1 = press1 AND NOT answer2 = press2 THEN
··· DEBUG "Wrong!"··································· ' WRONG
· ENDIF
·· GOTO SECONDPRESS
·· 'PLAYSOUND
· ENDIF
· GOTO· SECONDPRESS
IF ROUND < 6 THEN
·ROUND = ROUND + 1
ELSE
·ROUND = 1
·GOTO RESETGAME
ENDIF
Okay, questions. Basically, I understand the gist of this program, but I get bogged down in the little things. And unfortunately, those little things make the program work (haha).
1. How do I make the program evaluate something that isn't, rather than something that is? For example, instead of saying IF IN9=1 THEN do something, I want to say something like ELSEIF IN9 does not equal 1, then do something else. I'm tempted to use the word "NOT" but I'm pretty sure that's not a command.
2. I obviously have some dangling commands here. I'm interested specifically in the section where I am evaluating the user inputed values from the buttons.
Anyway, this project is for a class that is kind of beyond our scope with a very challenging instructor, so I'm feeling pretty discouraged. Anything you guys could do to help me straighten all this out would make me eternally grateful.
'light wiring
' PIN1 - Amber
' PIN2 - Red
' PIN3 - Green
'button wiring
' PIN9· - amber button
' PIN10 - red button
' PIN11 - green
ROUND VAR Nib
press1 VAR Nib
press2 VAR Nib
press3 VAR Nib
press4 VAR Nib
press5 VAR Nib
press6 VAR Nib
answer1 VAR Nib
answer2 VAR Nib
answer3 VAR Nib
answer4 VAR Nib
answer5 VAR Nib
answer6 VAR Nib
INIT:
answer1 = 0
answer2 = 0
answer3 = 0
answer4 = 0
answer5 = 0
answer6 = 0
ROUND = 1
' NEEDS TO BE RANDOM
GENERATE_SEQUENCE6:
· press1 = 2
· press2 = 3
· press3 = 1
· press4 = 2
· press5 = 3
· press6 = 1
GOTO BLINK
GENERATE_SEQUENCE1:
· press1 = 1
· press2 = 0
· press3 = 0
· press4 = 0
· press5 = 0
· press6 = 0
GOTO BLINK
GENERATE_SEQUENCE2:
· press1 = 1
· press2 = 3
· press3 = 0
· press4 = 0
· press5 = 0
· press6 = 0
GOTO BLINK
BLINK:
· HIGH· press1
· PAUSE 250
· LOW press1
IF ROUND > 1 THEN
· HIGH· press2
· PAUSE 250
· LOW press2
ENDIF
IF ROUND > 2 THEN
· HIGH· press3
· PAUSE 250
· LOW press3
ENDIF
· HIGH· press4
· PAUSE 250
· LOW press4
· HIGH· press5
· PAUSE 250
· LOW press5
· HIGH· press6
· PAUSE 250
· LOW press6
READ_PRESSES:
·FIRSTPRESS:
· IF IN9 = 1 THEN
··· answer1 = 1
· ELSEIF IN10 = 1 THEN
··· answer1 = 2
· ELSEIF IN11 = 1 THEN
··· answer1 = 3
· ENDIF
· IF answer1 <> 0· THEN
·· IF ROUND > 1 THEN
···· GOTO SECONDPRESS
· ELSEIF
·· ' MEANS WE GOT OUR ASWER AND THERE IS NO NEXT ROUND AND WE NEED TO EVALUATE THE ANSWER
·· 'EVALUATE -
··· IF answer1 = press1 THEN
···· 'we got it right and the game is over
··· ELSEIF answer1 = NOT press1 THEN
····· GOTO firstpress
···· ' wrong answer - do whatever
··· ENDIF
·· 'PLAYSOUND
· ENDIF
· GOTO· FIRSTPRESS
·SECONDPRESS:
· IF IN9 = 1 THEN
··· answer2 = 1
· ELSEIF IN10 = 1 THEN
··· answer2 = 2
· ELSEIF IN11 = 1 THEN
··· answer2 = 3
· ENDIF
· IF answer1 <> 0 THEN
·· IF ROUND > 2 THEN
···· GOTO THIRDPRESS
· ELSEIF answer1 = press1 AND answer2 = press2 THEN·· 'EVALUATE
··· DEBUG "Correct!"································ 'we got it right and the game is over
· ELSEIF NOT answer1 = press1 AND NOT answer2 = press2 THEN
··· DEBUG "Wrong!"··································· ' WRONG
· ENDIF
·· GOTO SECONDPRESS
·· 'PLAYSOUND
· ENDIF
· GOTO· SECONDPRESS
IF ROUND < 6 THEN
·ROUND = ROUND + 1
ELSE
·ROUND = 1
·GOTO RESETGAME
ENDIF
Okay, questions. Basically, I understand the gist of this program, but I get bogged down in the little things. And unfortunately, those little things make the program work (haha).
1. How do I make the program evaluate something that isn't, rather than something that is? For example, instead of saying IF IN9=1 THEN do something, I want to say something like ELSEIF IN9 does not equal 1, then do something else. I'm tempted to use the word "NOT" but I'm pretty sure that's not a command.
2. I obviously have some dangling commands here. I'm interested specifically in the section where I am evaluating the user inputed values from the buttons.
Anyway, this project is for a class that is kind of beyond our scope with a very challenging instructor, so I'm feeling pretty discouraged. Anything you guys could do to help me straighten all this out would make me eternally grateful.
Comments
I'm not sure what you mean by "dangling commands". Some general suggestions:
1) Use indenting more to clarify the flow of control. Try using more spaces between indenting levels.
2) When you have labels, put a blank line or some comments before the label to explain what that section does.
3) Try naming the I/O pins. It makes things clearer. Look in the manual for the PIN declaration statement and examples of its use.
4) Where it makes sense, try putting more than one statement on a line using ":". The manual has examples of this. It makes things clearer.
2. Parentheses make sure the order of evaluation is what you want it to be. Unlike many "Basic" language implementations, PBasic goes left to right, UNLESS you put in parentheses.
3. It looks like what is going on is that "Press1", "Press2", etc are being used to 're-map' what LED's turn on in the sequence. This isn't a problem, just a key to how the program works.
I appreciate your input. You indirectly answered another one of my questions, which was what exactly "<>" means. I have gotten my code to tolkenize correctly, so we'll see what happens when we plug that bad boy into the stamp! I'll probably be back with more questions, but until then, thanks so much for your help.
As an added question, can either of you explain the RANDOM command to me? Ultimately, instead of manually programming a really pseudo-random command like I'm doing in the above program code, I'd like to use the RANDOM command to automatically program a pseudo-random sequence of blinking lights. The one thing that I can't figure out is how to make that command do something, rather than just generate a sequence of random numbers. Does that make sense? I'm looking to use those randomly generated numbers to flash lights on and off.
I was thinking about having the RANDOM command generate a sequence of random numbers, and then having those numbers feed into some sort of algorithm, like "IF number > 0 AND number < 12, THEN Flash1 or something like that.
Post Edited (Mike Green) : 4/25/2008 5:40:54 PM GMT