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 3503 times)
Help with quiz-like chess program
May 10th, 2010, 5:59pm
 
Hello all! I'm working on a chess program, and am needing help. I'm going to have different tactics show up - the tactics are just going to be pictures of a chess board from online. Then I'm going to have 4 choices, labeled A, B, C and D. The user will press the key of the correct answer. If he or she presses the wrong answer, then a screen comes up explaining why that is so. So far, I've accomplished this with the first tactic and somewhat with the third.

What I'm have the most trouble with is having different letter answers for each tactic. The answer to my first tactic is C and the answer to my second is D. I cannot figure out how to get my "checkAnswer" function to work properly in this respect. Here's what I have so far:


Code:

Tactics tactic1;
Tactics tactic2;

PImage Tactic;
PFont Perpetua;

boolean T1Completed = false;
char a;
char b;
char c;
char d;

void setup() {
size(800,600);
smooth();

Perpetua = loadFont("Perpetua-48.vlw");

tactic1 = new Tactics(loadImage("TOne.jpg"), "One", "...Nxh4", "...d2", "...Rg7", "...Rxh7", "Black", "Easy", "Wrong. If ...Nxh4+, then Rxh4.",
"Wrong. If ...d2, pushing the pawn, the rook on d6 simply eats the pawn.", "Wrong. If ...Rxh7, picking up the pawn, white can mate by Rd8#.", c);
tactic2 = new Tactics(loadImage("TTwo.jpg"), "Two", "Bxh7", "g4", "Nxd8", "Bg5", "White", "Easy", "Wrong. There is a better move lurking.",
"Wrong. g4 threatens g5#, but the black king can now go to e7.", "Wrong. There is a better move lurking.", d);
}

void draw() {
background(125, 0, 0);

tactic1.display();
tactic1.checkAnswer();

if (T1Completed) {
tactic2.display();
tactic2.checkAnswer();
}
}
[b][/b]
void keyPressed() {
if (key=='c') {
if (!T1Completed) {
T1Completed = true;
}
}
}


Tactics class:

Code:

class Tactics {
PImage Tactic;
String TNumber, TChoiceA, TChoiceB, TChoiceC, TChoiceD, Color, TDifficulty, WrongChoice1, WrongChoice2, WrongChoice3;
char Correct;

Tactics(PImage tempTactic, String tempTNumber, String tempTChoiceA, String tempTChoiceB, String tempTChoiceC, String tempTChoiceD, String tempColor, String tempTDifficulty,
String tempWrongChoice1, String tempWrongChoice2, String tempWrongChoice3, char tempCorrect) {
Tactic = tempTactic;
TNumber = tempTNumber;
TChoiceA = tempTChoiceA;
TChoiceB = tempTChoiceB;
TChoiceC = tempTChoiceC;
TChoiceD = tempTChoiceD;
Color = tempColor;
TDifficulty = tempTDifficulty;
WrongChoice1 = tempWrongChoice1;
WrongChoice2 = tempWrongChoice2;
WrongChoice3 = tempWrongChoice3;
Correct = tempCorrect;
}

void display() {
image(Tactic, 0, 0);

fill(0);
textFont(Perpetua, 38);
text("Tactic " + TNumber, 520, 35); //Text displays tactic number

textFont(Perpetua, 32);
text("A: " + TChoiceA, 520, 150); //Text displays tactic choice A
text("B: " + TChoiceB, 520, 200); //Text displays tactic choice B
text("C: " + TChoiceC, 520, 250); //Text displays tactic choice C
text("D: " + TChoiceD, 520, 300); //Text displays tactic choice D

textFont(Perpetua, 30);
text(Color + " to move", 520, 400); //Text displays what color it is to move

textFont(Perpetua, 28);
text("Difficulty: " + TDifficulty, 520, 505); //Text displays the difficulty of the tactic
}

void checkAnswer() {
if (keyPressed) {
if (key=='a' && Correct==b || Correct==c || Correct==d) {
background(255);
text(WrongChoice1, 0, 25);
}
else if (key=='b' && Correct==a || Correct==c || Correct==d) {
background(255);
text(WrongChoice2, 0, 25);
}
else if (key=='c' && Correct==a || Correct==b || Correct==d) {
background(255);
text(WrongChoice3, 0, 25);
}
else if (key=='d' || Correct==a || Correct==b || Correct==c) {
background(255);
text(WrongChoice4, 0, 25);
}
}
}
}


It's like I have two different answer checkers in there, and I know that isn't right. I have my checkAnswer() function, along with a boolean variable set up that changes from false to true depending if a certain tactic is completed or not.

Any help is appreciated. Thanks!
Re: Help with quiz-like chess program
Reply #1 - May 11th, 2010, 2:08am
 
You haven't made the problem particularly clear...  However I note that you have a reference to 'WrongChoice4' in the checkAnswer class but this isn't defined in the class.  I'd be tempted to change these variable names to 'feedback#' (though I'd use a letter rather than a number to be consistent with the 'choice' variable names) and pass a string for all 4 answers - then for the correct answer you can display some confirmation text.

A few points on coding style:

1. Isn't the following:
Code:
if (key=='a' && Correct==b || Correct==c || Correct==d) { 


equivalent to:
Code:
if (key == 'a' && Correct != a) { 



2.  Whilst it's convenient to use shortcuts to declare your class variables of the same type on a single line IMO it's better and more readable to declare them individually on separate lines.

3.  If you find you need to pass a lot of values to a constructor it might be worth creating additional methods to populate the class properties.  Again this makes for more legible code.  In this case I might have taken the following approach:

Code:
class Tactics {
 PImage img;
 String number;
 String color;
 String difficulty;
 String correct;
 String choiceA;
 String choiceB;
 String choiceC;
 String choiceD;
 String feedbackA;
 String feedbackB;
 String feedbackC;
 String feedbackD;
 char correct;

 Tactics(PImage img, String number, String color, String difficulty, char correct) {
   this.img = img;
   this.number = number;
   this.color = color;
   this.difficulty = difficulty;
   this.correct = correct;
 }
 
 void setQuestions(String choiceA, String choiceB, String choiceC, String choiceD, String feedbackA, String feedbackB, String feedbackC, String feedbackD) {
   this.choiceA = choiceA;
   this.choiceB = choiceB;
   this.choiceC = choiceC;
   this.choiceD = choiceD;
   this.feedbackA = feedbackA;
   this.feedbackB = feedbackB;
   this.feedbackC = feedbackC;
   this.feedbackD = feedbackD;
 }


So in this case to invoke your class you need to make two calls - one to the constructor and one to the setQuestions method...

You'll see I dispensed with your prepending 'T' to everything - it's unnecessary and makes the variables difficult to read.  The fact I haven't capitalised the property names is personal coding style (though IIRC consistent with the style generally used in Java).  And again naming parameters the same as the target property and using 'this.propertyName' for assignment is a common convention, but again down to style...

Finally when you're calling methods with long string parameters it's often worth breaking them up onto separate lines to make things more legible...  Or better still store the text in an external file.
Re: Help with quiz-like chess program
Reply #2 - May 11th, 2010, 2:32am
 
Note on the key/Correct test: blindfish's version is better and more readable. But for the record, the original expression wasn't correct because && has a higher priority than ||
Correct expression is: if (key=='a' && (Correct==b || Correct==c || Correct==d)) { (additional parentheses). The test on 'd' was not consistent with the others...

You should use more arrays (for keys, answers, error messages, tactics...).

And more toward your problem, you should use a state machine, that would allow you to extend easily your program.

Again, blindfish is right, if you plan to have lot more screens/tactics, you should have a text file holding the data. With added advantage, once you made the mechanism of displaying/handling answers/changing state & tactics more generic, to allow to change and extend the list of tactics without touching your code.
Re: Help with quiz-like chess program
Reply #3 - May 11th, 2010, 3:01pm
 
Thanks for all your advice. I tried to incorporate some of it into my code, so I hope it's at least a little easier to read.

When I run my program now, I'm only able to get from tactic 1 to tactic 2 by pressing "c", which is the right answer. Unfortunately, I'd like my explanations of why a, b and d are wrong to show up when I press those letters. For some reason, they aren't doing that. If you look in the Tactics class, at the checkAnswer() function, it looks like the explanations should be showing up (I call them responses).

Also, I'm not able to get from tactic 2 to tactic 2, even when I press the right key (and again, explanations don't show up if I get tactic 2 wrong). Huh

Code:
Tactics tactic1;
Tactics tactic2;
Tactics tactic3;

PImage Tactic;
PFont Perpetua;

boolean T1Completed = false;
boolean T2Completed = false;
char a;
char b;
char c;
char d;

void setup() {
size(800,600);
smooth();

Perpetua = loadFont("Perpetua-48.vlw");

tactic1 = new Tactics(loadImage("TOne.jpg"), "One", "...Nxh4", "...d2", "...Rg7", "...Rxh7", "Black", "Easy", "Wrong. If ...Nxh4+, then Rxh4.",
"Wrong. If ...d2, pushing the pawn, the rook on d6 simply eats the pawn.", "Correct.", "Wrong. If ...Rxh7, picking up the pawn, white can mate by Rd8#.", c);
tactic2 = new Tactics(loadImage("TTwo.jpg"), "Two", "Bxh7", "g4", "Nxd8", "Bg5", "White", "Easy", "Wrong. There is a better move lurking.",
"Wrong. g4 threatens g5#, but the black king can now go to e7.", "Wrong. There is a better move lurking.", "Correct.", d);
tactic3 = new Tactics(loadImage("TThree.jpg"), "Three", "...Bxc3", "...a6", "...Qb6", "...Bd7", "Black", "Easy", "Correct.", "...a6 prepares ...b5, but the queen still isn't trapped.",
"...Qb6 doesn't create any threats.", "Even if attacked, the enemy queen can still go to b3 or c2.", a);
}

void draw() {
background(125, 0, 0);

tactic1.display();
tactic1.checkAnswer();

if (T1Completed) {
tactic2.display();
tactic2.checkAnswer();
if (T2Completed) {
tactic3.display();
tactic3.checkAnswer();
}
}
}




void keyPressed() {
if (key == 'c') {
if (!T1Completed) {
T1Completed = true;
if (key == 'd') {
if (!T2Completed) {
T2Completed = true;
}
}
}
}
}

Code:
class Tactics {
PImage Tactic;
String Number, ChoiceA, ChoiceB, ChoiceC, ChoiceD, Color, Difficulty, ResponseA, ResponseB, ResponseC, ResponseD;
char Correct;

Tactics(PImage tempTactic, String tempNumber, String tempChoiceA, String tempChoiceB, String tempChoiceC, String tempChoiceD, String tempColor, String tempDifficulty,
String tempResponseA, String tempResponseB, String tempResponseC, String tempResponseD, char tempCorrect) {
Tactic = tempTactic;
Number = tempNumber;
ChoiceA = tempChoiceA;
ChoiceB = tempChoiceB;
ChoiceC = tempChoiceC;
ChoiceD = tempChoiceD;
Color = tempColor;
Difficulty = tempDifficulty;
ResponseA = tempResponseA;
ResponseB = tempResponseB;
ResponseC = tempResponseC;
ResponseD = tempResponseD;
Correct = tempCorrect;
}

void display() {
image(Tactic, 0, 0);

fill(0);
textFont(Perpetua, 38);
text("Tactic " + Number, 520, 35); //Text displays tactic number

textFont(Perpetua, 32);
text("A: " + ChoiceA, 520, 150); //Text displays tactic choice A
text("B: " + ChoiceB, 520, 200); //Text displays tactic choice B
text("C: " + ChoiceC, 520, 250); //Text displays tactic choice C
text("D: " + ChoiceD, 520, 300); //Text displays tactic choice D

textFont(Perpetua, 30);
text(Color + " to move", 520, 400); //Text displays what color it is to move

textFont(Perpetua, 28);
text("Difficulty: " + Difficulty, 520, 505); //Text displays the difficulty of the tactic
}

void checkAnswer() {
if (keyPressed) {
if (key == 'a' && Correct != a) {
background(255);
text(ResponseA, 0, 25);
}
else if (key == 'b' && Correct != b) {
background(255);
text(ResponseB, 0, 25);
}
else if (key == 'c' && Correct != c) {
background(255);
text(ResponseC, 0, 25);
}
else if (key == 'd' && Correct != d) {
background(255);
text(ResponseD, 0, 25);
}
}
}
}

Re: Help with quiz-like chess program
Reply #4 - May 11th, 2010, 11:57pm
 
As I wrote, you need to have states in your program: displaying problem n, showing response or congrat message, going to next problem.
You probably display the responses, but only when the key is pressed, it is erased immediately after by the next draw() call.
And you should do all your key management in the keyPressed() function, ie. don't call checkAnswer() in draw().
When key is pressed, you check the answer, and then change the state accordingly.
The state can be a simple variable, a char or an int, indicating what is the current screen to display.
Re: Help with quiz-like chess program
Reply #5 - May 12th, 2010, 1:57pm
 
I've been going over this with my computer professor, and he recommends using arrays. So I did that and now everything looks nice and like it should work. The only problem is that I get an error saying "ChessOO.Tactics(int) is undefined". ChessOO is the name of my program.

I don't see how it is undefined. I followed the examples from Shiffman's book perfectly, and yet it still isn't defined. My goal is to have 20 tactics, but I'm only working with 3 now.
Code:
Tactics[] tactic = new Tactics(3);

PImage Tactic;
PFont Perpetua;

boolean T1Completed = false;
boolean T2Completed = false;
String a = "a";
String b = "b";
String c = "c";
String d = "d";

int tNumber = 1;

void setup() {
size(800,600);
smooth();

Perpetua = loadFont("Perpetua-48.vlw");

for (int i = 0; i < tactic.length; i++) {
tactic[1] = new Tactics(loadImage("TOne.jpg"), "One", "...Nxh4", "...d2", "...Rg7", "...Rxh7", "Black", "Easy", "Wrong. If ...Nxh4+, then Rxh4.",
"Wrong. If ...d2, pushing the pawn, the rook on d6 simply eats the pawn.", "Correct.", "Wrong. If ...Rxh7, picking up the pawn, white can mate by Rd8#.", c);
tactic[2] = new Tactics(loadImage("TTwo.jpg"), "Two", "Bxh7", "g4", "Nxd8", "Bg5", "White", "Easy", "Wrong. There is a better move lurking.",
"Wrong. g4 threatens g5#, but the black king can now go to e7.", "Wrong. There is a better move lurking.", "Correct.", d);
tactic[3] = new Tactics(loadImage("TThree.jpg"), "Three", "...Bxc3", "...a6", "...Qb6", "...Bd7", "Black", "Easy", "Correct.", " ...a6 prepares ...b5, but the queen still isn't trapped.",
"...Qb6 doesn't create any threats.", "Even if attacked, the enemy queen can still go to b3 or c2.", a);
}
}

void draw() {
background(125, 0, 0);

tactic[tNumber].display();
}




void keyPressed() {
String response;
if (key == 'a' || key == 'b' || key == 'c' || key =='d') {
response = tactic1.checkAnswer(key);
background(255);
println(response, 0, 25);
if (key == Correct) {
tNumber = tNumber + 1;
}
}
}

Code:
class Tactics {
PImage Tactic;
String Number, ChoiceA, ChoiceB, ChoiceC, ChoiceD, Color, Difficulty, ResponseA, ResponseB, ResponseC, ResponseD;
String Correct;

Tactics(PImage tempTactic, String tempNumber, String tempChoiceA, String tempChoiceB, String tempChoiceC, String tempChoiceD, String tempColor, String tempDifficulty,
String tempResponseA, String tempResponseB, String tempResponseC, String tempResponseD, String tempCorrect) {
Tactic = tempTactic;
Number = tempNumber;
ChoiceA = tempChoiceA;
ChoiceB = tempChoiceB;
ChoiceC = tempChoiceC;
ChoiceD = tempChoiceD;
Color = tempColor;
Difficulty = tempDifficulty;
ResponseA = tempResponseA;
ResponseB = tempResponseB;
ResponseC = tempResponseC;
ResponseD = tempResponseD;
Correct = tempCorrect;
}

void display() {
image(Tactic, 0, 0);

fill(0);
textFont(Perpetua, 38);
text("Tactic " + Number, 520, 35); //Text displays tactic number

textFont(Perpetua, 32);
text("A: " + ChoiceA, 520, 150); //Text displays tactic choice A
text("B: " + ChoiceB, 520, 200); //Text displays tactic choice B
text("C: " + ChoiceC, 520, 250); //Text displays tactic choice C
text("D: " + ChoiceD, 520, 300); //Text displays tactic choice D

textFont(Perpetua, 30);
text(Color + " to move", 520, 400); //Text displays what color it is to move

textFont(Perpetua, 28);
text("Difficulty: " + Difficulty, 520, 505); //Text displays the difficulty of the tactic
}

void checkAnswer() {
if (key == 'a') {
background(255);
println(ResponseA, 0, 25);
}
else if (key == 'b') {
background(255);
println(ResponseB, 0, 25);
}
else if (key == 'c') {
background(255);
println(ResponseC, 0, 25);
}
else if (key == 'd') {
background(255);
println(ResponseD, 0, 25);
}
}
}


Re: Help with quiz-like chess program
Reply #6 - May 12th, 2010, 2:33pm
 
Tactics[] tactic = new Tactics(3);
should be
Tactics[] tactic = new Tactics[3];
Re: Help with quiz-like chess program
Reply #7 - May 13th, 2010, 1:19pm
 
Thanks Koogy.  Smiley

Ok, so I've made significant progress I feel. I've just gone over my code with my computer professor and he said he didn't know why I was getting the following error. He recommend a way around it (not really solving the problem at hand, but ignoring it and trying a different route) by using booleans to test for correctness and such, but I feel this way will it be easier. Here's my new code:

Code:
Tactics[] tactic = new Tactics[3];

PImage Tactic;
PFont Perpetua;

boolean T1Completed = false;
boolean T2Completed = false;
String a = "a";
String b = "b";
String c = "c";
String d = "d";

int tNumber = 1;

void setup() {
size(800,600);
smooth();

Perpetua = loadFont("Perpetua-48.vlw");

tactic[1] = new Tactics(loadImage("TOne.jpg"), "One", "...Nxh4", "...d2", "...Rg7", "...Rxh7", "Black", "Easy", "Wrong. If ...Nxh4+, then Rxh4.",
"Wrong. If ...d2, pushing the pawn, the rook on d6 simply eats the pawn.", "Correct.", "Wrong. If ...Rxh7, picking up the pawn, white can mate by Rd8#.", c);
tactic[2] = new Tactics(loadImage("TTwo.jpg"), "Two", "Bxh7", "g4", "Nxd8", "Bg5", "White", "Easy", "Wrong. There is a better move lurking.",
"Wrong. g4 threatens g5#, but the black king can now go to e7.", "Wrong. There is a better move lurking.", "Correct.", d);
tactic[3] = new Tactics(loadImage("TThree.jpg"), "Three", "...Bxc3", "...a6", "...Qb6", "...Bd7", "Black", "Easy", "Correct.", " ...a6 prepares ...b5, but the queen still isn't trapped.",
"...Qb6 doesn't create any threats.", "Even if attacked, the enemy queen can still go to b3 or c2.", a);
}

void draw() {
background(125, 0, 0);

tactic[tNumber].display();
}

void keyPressed() {
String response;
if (key == 'a' || key == 'b' || key == 'c' || key =='d') {
response = tactic[tNumber].checkAnswer(key);
background(255);
text(response, 0, 25);
if (response == "Correct.") {
tNumber = tNumber + 1;
}
}
}

Code:
class Tactics {
PImage Tactic;
String Number, ChoiceA, ChoiceB, ChoiceC, ChoiceD, Color, Difficulty, ResponseA, ResponseB, ResponseC, ResponseD, Correct;

Tactics(PImage tempTactic, String tempNumber, String tempChoiceA, String tempChoiceB, String tempChoiceC, String tempChoiceD, String tempColor, String tempDifficulty,
String tempResponseA, String tempResponseB, String tempResponseC, String tempResponseD, String tempCorrect) {
Tactic = tempTactic;
Number = tempNumber;
ChoiceA = tempChoiceA;
ChoiceB = tempChoiceB;
ChoiceC = tempChoiceC;
ChoiceD = tempChoiceD;
Color = tempColor;
Difficulty = tempDifficulty;
ResponseA = tempResponseA;
ResponseB = tempResponseB;
ResponseC = tempResponseC;
ResponseD = tempResponseD;
Correct = tempCorrect;
}

void display() {
image(Tactic, 0, 0);

fill(0);
textFont(Perpetua, 38);
text("Tactic " + Number, 520, 35); //Text displays tactic number

textFont(Perpetua, 32);
text("A: " + ChoiceA, 520, 150); //Text displays tactic choice A
text("B: " + ChoiceB, 520, 200); //Text displays tactic choice B
text("C: " + ChoiceC, 520, 250); //Text displays tactic choice C
text("D: " + ChoiceD, 520, 300); //Text displays tactic choice D

textFont(Perpetua, 30);
text(Color + " to move", 520, 400); //Text displays what color it is to move

textFont(Perpetua, 28);
text("Difficulty: " + Difficulty, 520, 505); //Text displays the difficulty of the tactic
}

String checkAnswer(char key) { //highlighted
if (key == 'a') {
return ResponseA;
}
else if (key == 'b') {
return ResponseB;
}
else if (key == 'c') {
return ResponseC;
}
else if (key == 'd') {
return ResponseD;
}
}
}


I'm getting a "This method must return a result of type String". In the comments, I told you what line is being highlighted. The one that says: String checkAnswer(char key) {

But as you can see, it is returning a string. The error message says it isn't. I don't see what's going on.

I can feel I'm getting really close to getting this working, but I'm not quite there. Any help would be greatly appreciated.

Re: Help with quiz-like chess program
Reply #8 - May 13th, 2010, 1:42pm
 
What if key has a value different of the 4 you test?
Maybe it cannot happen, but the compiler doesn't know that for sure. So it complains that, in this case, your function doesn't return a string.
You can return a default value (eg. ResponseA), an empty string, a null value, but you must return something (related to string!).
Re: Help with quiz-like chess program
Reply #9 - May 13th, 2010, 1:57pm
 
Thank you. I fixed this error now, thanks to your help, by adding:
Code:
}
else {
return null;
}


and I no longer get an error there. The next error claimed my array index was out of bounds, so I changed
Code:
Tactics[] tactic = new Tactics[3]; 



to
Code:
Tactics[] tactic = new Tactics[4]; 



and that seemed to fix the problem (I believe it's because I forgot arrays have a "zero-ith" unit).

Now my program actually runs - only one (small) error. When I press the wrong key, it comes up with the proper error message, but it only flashes it. My goal is to have the proper error message stay on the screen as long as the wrong key is being pressed.

It's acting as if keyPressed() is in draw, but it isn't.


Re: Help with quiz-like chess program
Reply #10 - May 13th, 2010, 2:22pm
 
See the description of KeyPressed() in the Reference.  In particular this section:

Quote:
Because of how operating systems handle key repeats, holding down a key may cause multiple calls to keyPressed() (and keyReleased() as well). The rate of repeat is set by the operating system and how each computer is configured.


this might help Wink
Re: Help with quiz-like chess program
Reply #11 - May 13th, 2010, 2:34pm
 
Well, that's inconvenient. No worries though - I'm able to accomplish my goal using only one boolean right?


Code:

boolean isPressed = false;

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


I defined the boolean variable up to, so it's global. I'm just showing a portion of the code.

The program runs, but it isn't doing what I wanted, so I must have done something wrong.  Huh I thought I was using booleans correctly (going from Shiffman's book).
Re: Help with quiz-like chess program
Reply #12 - May 14th, 2010, 1:18am
 
Look at the code I posted in the linked thread: you need to use keyReleased() to change the state of the boolean to false; otherwise you run into the key repeat issue...
Re: Help with quiz-like chess program
Reply #13 - May 14th, 2010, 11:49am
 
I see. Thanks! I was able to incorporate that successfully. Now everything is looking pretty good. Only one thing yet to figure out. When the user presses the "h" key, I want a help screen to come up, and then when "h" is released, I want it to go back to the current tactic. Sounds easy, right?

I've set up a boolean as a global variable:

Code:
boolean help = false; 



I put this in the keyPressed() routine:

Code:
  if (key == 'h') {
if (help = false) {
help = true;
}
else if (help = true) {
help = false;
}
}


Then I tried this in my draw routine:

Code:
if (help) {
background(255);
}


That did nothing even when I pressed "h". I then revamped it a bit to:
Code:
if (!help) {
background(255);
}


This made the background white and kept it that way (even when I pressed "h").

What am I doing wrong? I want the whole screen to be white when "h" is pressed (kind of like how the responses show - the chess board and the other text disappear).






Re: Help with quiz-like chess program
Reply #14 - May 14th, 2010, 11:45pm
 
You need to take the same approach as with the other keyboard interaction - i.e. set help to false on keyRelease.  You also have some syntax errors:

Quote:
 if (key == 'h') {
   if (help = false) {
     help = true;
   }
   else if (help = true) {
     help = false;
   }
 }


A single equal sign is the assignment operator so you shouldn't be using it in conditions...
Pages: 1 2