Shop OBEX P1 Docs P2 Docs Learn Events
Coding help — Parallax Forums

Coding help

ktdid07ktdid07 Posts: 8
edited 2008-04-25 17:35 in Learn with BlocklyProp
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.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-25 15:36
    Look in the Stamp Basic Manual (which you can download ... go to the Resources tab on Parallax's website, then choose Downloads, then Basic Stamp Documentation). You'll see all of the comparison operators. There's = for equal, <> for not equal. You can say "NOT (a = b)".

    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-04-25 15:57
    1. If you want to check the opposite of "IF IN9 = 1" then you do "IF IN9 = 0".

    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.
  • ktdid07ktdid07 Posts: 8
    edited 2008-04-25 17:29
    Thanks, Allan and Mike.

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-25 17:35
    You've got the right idea for the use of RANDOM. It produces a sequence of pseudo-random numbers by transforming one 16-bit number into another in a random-like way. You can then take one of these 16-bit numbers and transform it into something more useful. For example, it should be equally likely that the number is in the range 0-32767 as it is in the range 32768-65535. If you test for 0-32767, you have a 50:50 chance of that being true at any given time. RANDOM is like throwing dice except that you have 65536 possibilities instead of 6, roughly like throwing 6 dice at a time (6 x 6 x 6 x 6 x 6 x 6).

    Post Edited (Mike Green) : 4/25/2008 5:40:54 PM GMT
Sign In or Register to comment.