Page 1 of 2
keeping score
Posted: Wed Feb 24, 2016 2:42 am
by ethanCodes
I am trying to make this simple program keep score. The user guesses a number between 0-15, and if the guess is correct, then add 2 points, incorrect, take 1 point away. The GUI has a submit button, which all the code is written through, an instruction label, a text box for user input, a "score" label and a score text box starting at 0. I'm not sure why, but if I guess incorrect, it goes down to -1, but won't go further into the negative? I'm not sure why. Here is the code I have so far.
Code: Select all
on mouseUp
put the text of fld "fld_guess" into theGuess
put 0 into score --adds the fun of keeping score to the game!
set the randomSeed to the long seconds
put random(15) into theAnswer --changed the range to make it easier, and somewhat less frustrating
if theGuess is theAnswer then
answer "Correct"
add 2 to score --adds 1 point to score if answer is correct
else
answer "Wrong! The correct answer was " & theAnswer & "."
add -1 to score --subtracts 1 point from score if answer is incorrect
end if
put empty into fld "fld_guess"
put score into field "scoreField" --displays the current score
end mouseUp
Re: keeping score
Posted: Wed Feb 24, 2016 3:34 am
by Simon
ooops
Code: Select all
put 0 into score --adds the fun of keeping score to the game!
Take a look at where that is again
Simon
Re: keeping score
Posted: Wed Feb 24, 2016 3:45 am
by dunbarx
Hi.
What Simon said.
But did you step through the handler when you were scratching your head? This would have given you the reason.
Craig Newman
Re: keeping score
Posted: Wed Feb 24, 2016 3:52 am
by ethanCodes
Thank you both, how do I step through a handler?
Re: keeping score
Posted: Wed Feb 24, 2016 3:54 am
by Simon
I guess we should add, look-up "breakpoint" in the dictionary.
And where is a good description of the little red dots?
Simon
Re: keeping score
Posted: Wed Feb 24, 2016 3:57 am
by ethanCodes
I know how the dots work and breakpoints. I will look into that. It still is only going to -1.
Re: keeping score
Posted: Wed Feb 24, 2016 4:01 am
by Simon
add "breakpoint" before putting 0 into score.
Oh... and I bet it doesn't go above 2
Simon
Re: keeping score
Posted: Thu Feb 25, 2016 3:28 am
by ethanCodes
So I guess I'm not getting this. I put breakpoints in but it doesn't show me anything? Clearly the program is replacing the 0 with 2 or -1, which makes me think that 'add' is not the right keyword to use, but I don't know what else you would use? This language is so screwy. If i'm adding 2 to 2, why is score not 4? If I'm adding -1 to 2, why is score not 1? This doesn't make sense.
Re: keeping score
Posted: Thu Feb 25, 2016 3:38 am
by Simon
Ok I'll just give it to you.
Code: Select all
put 0 into score --adds the fun of keeping score to the game!
Do you see every time you go through that bit of code it ALWAYS puts 0 into score. Even if you had -1 in it before you just cleared it out.
Do you see it now?
Simon
Re: keeping score
Posted: Thu Feb 25, 2016 4:20 am
by ethanCodes
right but that is in the on openCard section. I thought that would mean it would only get set when the card opens?
Re: keeping score
Posted: Thu Feb 25, 2016 4:35 am
by Simon
It's in an on mouseUp not openCard.
Simon
Re: keeping score
Posted: Thu Feb 25, 2016 4:41 am
by ethanCodes
Oh I'm sorry, I already changed that. Here is my updated code:
Code: Select all
on mouseUp
put the text of fld "fld_guess" into theGuess
set the randomSeed to the long seconds
put random(15) into theAnswer --changed the range to make it easier, and somewhat less frustrating
if theGuess is theAnswer then
answer "Correct"
add 2 to score --adds 1 point to score if answer is correct
else
answer "Wrong! The correct answer was " & theAnswer & "."
add -1 to score --subtracts 1 point from score if answer is incorrect
end if
put empty into fld "fld_guess"
put score into field "scoreField" --displays the current score
end mouseUp
and here is the on cardOpen code attached to the card:
Code: Select all
on openCard
put 0 into score --adds the fun of keeping score!
put 0 into field "scoreField"
end openCard
Re: keeping score
Posted: Thu Feb 25, 2016 4:51 am
by Simon
Well that changes a lot of things!
You just need a "local score" above "on mouseUp"
Simon
Re: keeping score
Posted: Thu Feb 25, 2016 5:00 am
by ethanCodes
I see...does that link the local score with the score initialized to 0 in the on openCard? or do I even need to initialize that score on on openCard?
Re: keeping score
Posted: Thu Feb 25, 2016 5:10 am
by Simon
Don't need it because they are unconnected.
Now if you make it a global variable then you could use it in both places but you have to make sure they are declared in both places.
Simon