Quiz in Processing

edited November 2015 in How To...

For a group project my group wants to create a quiz with Processing integrated in Eclipse. Now for the part with the questions I need a little help. We want to read out the questions and answers from a text, but how exactly do we tell Eclipse what are the answers to the questions, what is the correct answer etc... We created the text document with writing the question in line 1, the correct answer in line 2, the other two wrong answers in lines 3 and 4, the next question in line 6 etc.

Tagged:

Answers

  • are you in OOP ?

    then make a class Question with question, correct answer, false answers, points for score etc.

  • use loadStrings to load the entire textfile into your sketch

    see reference on loadStrings

    then for-loop over the array

    and store it in an ArrayList of your new class using add

    when you post the 1st 30 lines of your text-file?

  • edited May 2014
    Wann war die offizielle Campuseröffnung des Standortes Kamp-Lintfort?   
    04.04.14    
    12.08.12    
    08.12.13
    Wieviele Bachelor- und Masterstudiengänge gibt es am Campus Kamp-Lintfort? (Stand 4.4.2014) 
    12  
    9   
    36
    Wieviele englischsprachige Studiengänge gibt es am Campus Kamp-Lintfort? (Stand 4.4.2014)   
    9   
    7   
    3
    Wieviele Bachelor- und Masterstudiengänge gibt es an der Hochschule Rhein-Waal insgesamt? (Stand 4.4.2014)  
    33  
    43  
    15
    

    This is an example with 4 questions, the first answer always is the correct one. It's german though, I hope that's not a problem? If it is, I can translate them!

  • Weird, in my document it's correct, but here the third answer shares a line with the following question...

  • edited May 2014

    Use the C button to format code (or lines like this), not the I button...
    I formatted it for you. Also changed the category, since you don't show any code yet.

  • Oh ok, sorry for that, thanks a lot!

  • You missed PhiLho's point - YOU HAVEN'T SHOWN US ANY CODE YET. What have you got so far? What have you tried? It's possible for at least 6 people here to just write exactly what you want, with ease, in very little time. But we don't, because we value our time. So if you have a chunk of code that almost doing it already, post it, and that'll cut down the amount of effort we have to put in to help you!

  • here...

    ArrayList <Question> questions = new ArrayList();
    int indexQuestions = 0;
    
    // the status of the program 
    final int play=0;       // possible status 
    final int gameOver=1;   // possible status 
    final int feedback=2;   // possible status 
    int status = play;      // current status
    
    /*
    Wann war die offizielle Campuseröffnung des Standortes Kamp-Lintfort?  
     04.04.14   
     12.08.12   
     08.12.13
     Wieviele Bachelor- und Masterstudiengänge gibt es am Campus Kamp-Lintfort? (Stand 4.4.2014)
     12 
     9  
     36
     Wieviele englischsprachige Studiengänge gibt es am Campus Kamp-Lintfort? (Stand 4.4.2014)  
     9  
     7  
     3
     Wieviele Bachelor- und Masterstudiengänge gibt es an der Hochschule Rhein-Waal insgesamt? (Stand 4.4.2014) 
     33 
     43 
     15
     */
    
    void setup() {
      size(800, 600);
      // define all questions
      String[] a1 = loadStrings("test.txt");
      // println (a1);
      for (int i = 0; i<a1.length; i+=4) {
        Question currentquestion = new Question( a1[i], a1[i+1], a1[i+2], a1[i+3], 2, 30, 30 );
        questions.add(currentquestion);
      }
    }
    
    void draw() {
      background(0);
      switch (status) {
      case play:
        questions.get(indexQuestions).display(); 
        break;
      case feedback:
        // correct? 
        if (questions.get(indexQuestions).check(key)) {
          text ("Gut", 100, 200);
        }
        else {
          text ("Nein", 100, 200);
        } // else
        text ("Bitte eine Taste", 100, 300); 
        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') 
        { 
          status=feedback;
        }
        break;
      case feedback:
        // any key 
        // next status
        status = play; 
        // next question 
        indexQuestions++;
        // last question? 
        if (indexQuestions>questions.size()-1)
        {
          status = gameOver; // game over
        } // if
        break;
      case gameOver:
        if (key == 'r') {
          // restart 
          indexQuestions=0;
          status=play;
        } // if
        break;
      default:
        // error 
        break;
      } // switch
      //
    } // func 
    
    // ==================================================================
    
    class Question {
      String quest;
      String answer1, answer2, answer3;
      int correctAnswerNumber; 
      int posX, posY;
    
      // constr
      Question (String que, 
      String answer1temp, String answer2temp, String answer3temp, 
      int correctAnswerNumberTemp, 
      int x, int y) {
        quest = que;
        answer1=answer1temp;
        answer2=answer2temp;
        answer3=answer3temp;
        correctAnswerNumber=correctAnswerNumberTemp;
        posX=x;
        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 ================================================================
    
  • sorry, TFguy I hadn't seen your post, you must have posted while I was digging out an old code.... ;-)

Sign In or Register to comment.