Questions game

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.

Tagged:

Answers

  • edited January 2014

    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.).

  • Answer ✓

    this is the idea:

    // ==================================================================
    
    class Question {
      String quest;
      String answer1, answer2, answer3;
      int correctAnswerNumber; 
      int posX, posY;
      //   boolean que1, que2, que3;
      //   Question() {}
    
      Question (String que, 
      String answer1temp, String answer2temp, String answer3temp, 
      int correctAnswerNumberTemp, 
      int x, int y) {
        this.quest = que;
        answer1=answer1temp;
        answer2=answer2temp;
        answer3=answer3temp;
        // this.qu1 = set;
        correctAnswerNumber=correctAnswerNumberTemp;
        posX=x;
        posY=y;
      }
    
      void position(int x, int y) {
        this.posX = x;
        this.posY = y;
      }
    
      void display() {
        text(quest, posX, posY);
        text("  1 * "+answer1, posX, posY+22);
        text("  2 * "+answer2, posX, posY+44);
        text("  3 * "+answer3, posX, posY+66);
        text("Please enter a number", posX, posY+94);
      } // method 
    
      boolean check( char keyToTest ) {
        if (keyToTest=='1' && correctAnswerNumber==1) return true;
        if (keyToTest=='2' && correctAnswerNumber==2) return true; 
        if (keyToTest=='3' && correctAnswerNumber==3) return true; 
        return false;
      } // method 
      //
    } // class =================================================================
    
    Question [] questions = new Question [ 3 ];
    int indexQuestions = 0;
    
    // the status of the program 
    final int play=0;       // possible status 
    final int gameOver=1;   // possible status 
    int status = play;      // actual / current status
    
    void setup() {
      size(600, 600);
      // define all questions 
      questions[0] = new Question(  "What is blue like? ", "like white", "like blue", "like Red", 2, 30, 30 );
      questions[1] = new Question(  "What is L like? ", "like P", "like M", "like L", 3, 130, 130 );
      questions[2] = new Question(  "What is Lion like? ", "like Lion", "like M", "like L", 1, 330, 130 );
    }
    
    void draw() {
      background(0);
      switch (status) {
      case play:
        questions[indexQuestions].display(); 
        break;
      case  gameOver:
        text ( "All questions done.", 200, 200 );
        text ( "Press r to restart.", 200, 400 );
        break;
      }  // switch
    } // func 
    
    void keyPressed() {
      switch (status) {
      case play:
        if (key>='1' && key <= '3') 
        { 
          // correct? 
          if (questions[indexQuestions].check(key)) {
            println ("Good");
          }
          else {
            println ("No");
          }
          // next question 
          indexQuestions++;
          // last question? 
          if (indexQuestions>questions.length-1)
          {
            status = gameOver;
          } // if
        } // if
        break;
      case gameOver:
        if (key == 'r') {
          // restart 
          indexQuestions=0;
          status=play;
        } // if
        break;
      } // switch
      //
    } // func 
    
    //
    
  • edited January 2014

    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 :).

Sign In or Register to comment.