We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Help with quiz-like chess program
Pages: 1 2 
Help with quiz-like chess program (Read 3502 times)
Re: Help with quiz-like chess program
Reply #15 - May 15th, 2010, 1:16am
 
blindfish wrote on May 14th, 2010, 11:45pm:
You also have some syntax errors: [...] A single equal sign is the assignment operator so you shouldn't be using it in conditions...

It can happens only with booleans, in Java.
That's one of the reason why I recommend to use a more logical (in both sense of word!), more readable () and terser syntax:
Code:
if (key == 'h') {
if (!help) {
help = true;
}
else if (help) {
help = false;
}
}

Ie. use the booleans in tests for what they are.
Beside, this can be simplified:
Code:
if (key == 'h') {
if (help) {
help = false;
}
else {
help = true;
}
}

If it is not true, it is necessarily false...
Actually, I prefer to write:
Code:
if (key == 'h') {
help = !help;
}

fully leveraging the boolean arithmetic...
Re: Help with quiz-like chess program
Reply #16 - May 15th, 2010, 3:45pm
 
Sorry - confused: what can only happen with booleans in Java?

Ah - I see: you mean assignment within a condition...  If you try it, for example, with an int you get a compile error.  Is there a good reason why the boolean doesn't throw a compile error but instead assigns the value?  That's the sort of thing that produces evil bugs that can be hard to track down - especially for beginners...

Agree fully with everything in your post about using booleans; though in this context - where you want something displayed for as long as the key is pressed and then hidden on release - you do need to change the boolean state on keyReleased() rather than toggling on keyPressed(); otherwise you encounter the key repeat issue already mentioned.

The boolean assignment for toggling can be incredibly useful though Wink
Re: Help with quiz-like chess program
Reply #17 - May 15th, 2010, 4:25pm
 
Thanks guys. I'm gonna use the same boolean if-statement format that I used for my boolean variable "isPressed" (to determine if a, b, c or d is pressed).

So I have:

Code:

if (key == 'h') {
   if (help == true) {
help = false;
   }
   else {
help = true;
   }
   background(125, 0, 0);
   textFont(Perpetua, 30);
   text("Test", 0, 25);
 }


I keep my keyReleased() method the same:

Code:

void keyReleased() {
  response = null;
}


Yet when I press "h", the screen appears white, and stops being white when I release "h". I set the background to be red, so I don't know why it would be white (as default?).

The word "Test" is only flashed when I press/release "h". I think the problem has to do with it not being in draw(), but it shouldn't be, because it's its own function.

My other boolean variable, isPressed, works just fine:

Code:

if (key == 'a' || key == 'b' || key == 'c' || key =='d') {
   if (isPressed == true) {
isPressed = false;
   }
   else {
isPressed = true;
   }
   response = tactic[tNumber].checkAnswer(key);
   background(125, 0, 0);
   fill(255);
   text(response, 0, 25);
   if (response == "Correct.") {
tNumber = tNumber + 1;
   }
 }


Any ideas?
Re: Help with quiz-like chess program
Reply #18 - May 16th, 2010, 12:46am
 
check2010 wrote on May 15th, 2010, 4:25pm:
The word "Test" is only flashed when I press/release "h". I think the problem has to do with it not being in draw(), but it shouldn't be, because it's its own function.


It's too early for me to explain why one works and the other doesn't, but the solution is to move the following into draw() and inside an appropriate condition:

Code:
if (help) {
background(125, 0, 0);
textFont(Perpetua, 30);
text("Test", 0, 25);
}
Re: Help with quiz-like chess program
Reply #19 - May 16th, 2010, 3:37am
 
blindfish wrote on May 15th, 2010, 3:45pm:
you mean assignment within a condition...  If you try it, for example, with an int you get a compile error.  Is there a good reason why the boolean doesn't throw a compile error but instead assigns the value

I think it goes back to the C roots/inheritance of the Java language: assignment has a value which can be used in conditions.
Except than in C, there are no booleans, 0 is seen as false, any other value is seen as true. So you can write if (a = b) print("b is true"); for example. It is more used in code like: while (c = getchar()) use(c); where you assign a value and test its value at the same time. Seductive because it is terse, but prone to errors...
In Java, you can only use booleans in conditions, there is no rule like 0 is false and 1 is true. Hence the "restriction" you wonder above.
Re: Help with quiz-like chess program
Reply #20 - May 16th, 2010, 2:33pm
 
Thanks PhiLho - that makes some sense - i.e. sounds like it's something of an anachronism...  I must admit I favour writing long-hand code to aid readability rather than terse code; though of course this has its place (e.g. jquery.min which reduces the size of the download - but I sure as hell wouldn't want to work from the source code).

@check2010
I was half-asleep when I answered this morning.  The reason the text in the other condition is persistent is because it's within the checkAnswer() method; which is being called every frame in draw()...
Pages: 1 2