We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I need to construct a quiz game that has 5 sets of 3 questions and only one of the questions is true. Already i made a class where you can set the questions with a question(String que, boolean set) method. The boolean set sets the possible answer where it should be true. Now i made three of those for each question. I also made three display() methods where you get the question in a text form. In the main class there are 5 void methods. I need the questions to change if the user pressed the correct answer between the 5 void methods. It needs to be in a OOP style as well. I also made a button class. Two things bother me. How can i know if the user pressed the correct answer from the multiple booleans? And how to pass that value so it can change between the questions?
class Question {
String quest;
int posX, posY;
boolean que1, que2, que3;
Question() {}
public Question question1(String que, boolean set) {
this.question = question;
this.qu1 = set;
return this;
}
public Question question2(String que, boolean set) {
this.question = question;
this.qu1 = set;
return this;
}
public Question question3(String que, boolean set) {
this.question = question;
this.qu1 = set;
return this;
}
public Question position(int x, int y) {
this.posX = x;
this.posY = y;
}
void display() {
button();
text(question, posX, posY);
}
}
Question question;
void setup() {
size(600, 600);
question = new Question();
}
void draw() {
check();
}
void question1() {
quiz.question1("One and two", true).position(100, 200).display1();
quiz.question2("Five and three", false).position(100, 300).display2();
quiz.question3("Six and two", false).position(100, 400).display3();
}
void question2() {
quiz.question1("One and two", true).position(100, 200).display1();
quiz.question2("Five and three", false).position(100, 300).display2();
quiz.question3("Six and two", false).position(100, 400).display3();
}
void question3() {
quiz.question1("One and two", true).position(100, 200).display1();
quiz.question2("Five and three", false).position(100, 300).display2();
quiz.question3("Six and two", false).position(100, 400).display3();
}
// How to check if the correct question is pressed and how to return the correct answer to get the next question
void check() {
if(correct) {
question1();
}
question2();
question3();
}
I also have a class for the button with the mouseX > xPos && mouseX < xPos && mouseY > yPos 7&& mouseY < yPos and this returns true when it is pressed.
Answers
hm....
public Question question1 and public Question question2 and public Question question3
don't make sense to me. They all look the same.
The idea of OOP is that the class Question represents the abstract idea of a question and not the question itself.
the class Question represents one question and you have an array for the questions you need (the objects). The array is of type Question. Here is the content of the question (the real question like "what is blue like").
Is that approach ok for you?
Then the array is a list of the questions and after one question is answered the index for the array gets increased.
Remember the Class is only the cookie maker, not the cookie. So the class is slim and shows only what one question needs (the abstract form for any question/answer set). That you have different questions is not handled inside the class but outside of it in the array.
For one question you need pos (x,y) and question and 3 answers and the number of the correct answer (of the three) and a bool "it has been asked" (e.g.).
this is the idea:
the check if the answer is correct is given only as println and not on the screen yet.
the button class should be called by the question class, I think.
because the question class has the question and answers and can fill this in into the button class.
BTW
I had to work with status of the program so that we know if the game is over.
Status is used in draw() and in keyPressed(). Thus the program can switch to different modes if you like.
You can use this approach also to display whether the answer was correct by implementing a new status.
Hey Chrisir,
Sorry for the delay, that is one elaborate explanation there :). You are right about the arrays and how the answers are stored. Yet i still used the boolean values and took a bit of a different approach because it needs to stay on the same answer until the user guesses the right one. Thank you a lot :).
Great!!