Add quiz subset question handling
authorCharles Clément <caratorn@gmail.com>
Wed, 17 Feb 2010 17:50:30 +0000 (09:50 -0800)
committerCharles Clément <caratorn@gmail.com>
Wed, 17 Feb 2010 17:50:30 +0000 (09:50 -0800)
README
data/qquiz.desktop
src/quiz.cpp
src/quiz.h

diff --git a/README b/README
index db6d6cf..c204b8e 100644 (file)
--- a/README
+++ b/README
@@ -10,6 +10,7 @@ Here's the syntax for the quiz files :
 Quiz title
 # This is a comment
 # Author : name email_address
 Quiz title
 # This is a comment
 # Author : name email_address
+# subset: number of questions to put in a game, example 30. Optional
 Default amount of time in minutes
 Question 1 ; Answer 1 | Another possible answer to question 1
 Question 2 ; Answer 1 | Answer 2
 Default amount of time in minutes
 Question 1 ; Answer 1 | Another possible answer to question 1
 Question 2 ; Answer 1 | Answer 2
index bbc66fb..970475d 100644 (file)
@@ -7,4 +7,3 @@ Exec=/usr/bin/qquiz
 Icon=qquiz
 X-HildonDesk-ShowInToolbar=true
 X-Osso-Type=application/x-executable
 Icon=qquiz
 X-HildonDesk-ShowInToolbar=true
 X-Osso-Type=application/x-executable
-
index 76d5ac8..e7d04b1 100644 (file)
@@ -29,7 +29,7 @@ question::~question() {
        delete(label);
 }
 
        delete(label);
 }
 
-quiz::quiz() : current(NULL), correct(0) {
+quiz::quiz() : current(NULL), subset(0), correct(0)  {
        QAction *choose;
 
        window = new QWidget();
        QAction *choose;
 
        window = new QWidget();
@@ -110,12 +110,17 @@ void quiz::choose_quiz() {
                        }
                        delete(timer);
                        questions.clear();
                        }
                        delete(timer);
                        questions.clear();
+                       subset = 0;
                        correct = 0;
                } else {
                        init_gui();
                }
                current = files.at(items.indexOf(item));
                if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
                        correct = 0;
                } else {
                        init_gui();
                }
                current = files.at(items.indexOf(item));
                if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
+                       if (subset) {
+                               trim_questions();
+                       }
+                       build_index();
                        menu->addAction(give_up);
                        display_score();
                        display_grid();
                        menu->addAction(give_up);
                        display_score();
                        display_grid();
@@ -152,6 +157,12 @@ int quiz::read_quiz(const char *filename) {
                do {
                        getline(ifs, buffer);
                } while (buffer[0] == '#');
                do {
                        getline(ifs, buffer);
                } while (buffer[0] == '#');
+               if (! buffer.compare(0, strlen(SUBSET_PATTERN), SUBSET_PATTERN)) {
+                       subset = atoi(buffer.substr(strlen(SUBSET_PATTERN)).c_str());
+                               do {
+                                       getline(ifs, buffer);
+                               } while (buffer[0] == '#');
+               }
                total_time = atoi(buffer.c_str());
                /* convert minutes to seconds */
                total_time *= 60;
                total_time = atoi(buffer.c_str());
                /* convert minutes to seconds */
                total_time *= 60;
@@ -162,7 +173,7 @@ int quiz::read_quiz(const char *filename) {
                        loc = parse_line.find(CHAR_DELIM);
                        if (loc == string::npos) {
                                cerr << "Wrong format in file " << filename << endl;
                        loc = parse_line.find(CHAR_DELIM);
                        if (loc == string::npos) {
                                cerr << "Wrong format in file " << filename << endl;
-                               return 0 ;
+                               return 0;
                        }
 
                        hint = parse_line.substr(0, loc);
                        }
 
                        hint = parse_line.substr(0, loc);
@@ -182,7 +193,6 @@ int quiz::read_quiz(const char *filename) {
                        qanswer = qanswer.trimmed();
                        q = new question(QString::fromStdString(hint), qanswer );
                        questions.push_back(q);
                        qanswer = qanswer.trimmed();
                        q = new question(QString::fromStdString(hint), qanswer );
                        questions.push_back(q);
-                       index[qanswer.toLower()] = i;
 
                        while (mloc != string::npos) {
                                parse_line = parse_line.substr(mloc + 1);
 
                        while (mloc != string::npos) {
                                parse_line = parse_line.substr(mloc + 1);
@@ -194,7 +204,7 @@ int quiz::read_quiz(const char *filename) {
                                }
                                qanswer = QString::fromStdString(answer);
                                qanswer = qanswer.trimmed();
                                }
                                qanswer = QString::fromStdString(answer);
                                qanswer = qanswer.trimmed();
-                               index[qanswer.toLower()] = i;
+                               q->alternate_answers.append(qanswer);
                                mloc = parse_line.find(ANSWER_DELIM);
                        }
                        i++;
                                mloc = parse_line.find(ANSWER_DELIM);
                        }
                        i++;
@@ -205,6 +215,41 @@ int quiz::read_quiz(const char *filename) {
        return 1;
 }
 
        return 1;
 }
 
+void quiz::trim_questions() {
+       vector<question *> new_questions;
+       int number;
+       int nr_questions;
+
+       qsrand(QDateTime::currentDateTime().toTime_t());
+
+       nr_questions = subset;
+       while (nr_questions) {
+               number = qrand() % questions.size();
+               new_questions.push_back(questions.at(number));
+               questions.erase(questions.begin() + number);
+               nr_questions--;
+       }
+       questions.clear();
+       questions = new_questions;
+       total = subset;
+}
+
+void quiz::build_index() {
+       vector<question *>::iterator itr;
+       int position = 0;
+
+       for (itr = questions.begin(); itr != questions.end() ; itr++, position++) {
+               index[(*itr)->answer.toLower()] = position;
+               if (!(*itr)->alternate_answers.isEmpty()) {
+                       QList<QString>::iterator list_itr;
+
+                       for (list_itr = (*itr)->alternate_answers.begin();
+                                       list_itr != (*itr)->alternate_answers.end(); list_itr++)
+                               index[(*list_itr).toLower()] = position;
+               }
+       }
+}
+
 void quiz::init_gui() {
        QHBoxLayout *menu_layout;
 
 void quiz::init_gui() {
        QHBoxLayout *menu_layout;
 
@@ -303,7 +348,8 @@ void quiz::display_score() {
 
 void quiz::display_grid() {
        vector<question *>::iterator itrq;
 
 void quiz::display_grid() {
        vector<question *>::iterator itrq;
-       int i,j, nr_columns, nr_col_padding;
+       int i,j, nr_col_padding;
+       int nr_columns = 0;
        int padding = 3;
        int pixelsWide ;
        QFont font;
        int padding = 3;
        int pixelsWide ;
        QFont font;
index fe5e9eb..b6cd245 100644 (file)
@@ -33,6 +33,7 @@
 
 #define APP_NAME                       "qquiz"
 
 
 #define APP_NAME                       "qquiz"
 
+#define SUBSET_PATTERN         "subset:"
 #define CHAR_DELIM                     ';'
 #define ANSWER_DELIM           '|'
 
 #define CHAR_DELIM                     ';'
 #define ANSWER_DELIM           '|'
 
@@ -47,6 +48,7 @@ class question {
                question(QString s, QString r);
                QString hint;
                QString answer;
                question(QString s, QString r);
                QString hint;
                QString answer;
+               QList<QString> alternate_answers;
                QLabel *label;
                int answered;
 };
                QLabel *label;
                int answered;
 };
@@ -65,6 +67,8 @@ class quiz : public QObject{
                ~quiz();
                void retrieve_quizzes();
                int read_quiz(const char *filename);
                ~quiz();
                void retrieve_quizzes();
                int read_quiz(const char *filename);
+               void trim_questions();
+               void build_index();
                void init_gui();
                void display_score();
                void display_grid();
                void init_gui();
                void display_score();
                void display_grid();
@@ -80,6 +84,7 @@ class quiz : public QObject{
                quiz_file * current;
                QString title;
                int total;
                quiz_file * current;
                QString title;
                int total;
+               int subset;
                int correct;
                unsigned int total_time;
                unsigned int current_time;
                int correct;
                unsigned int total_time;
                unsigned int current_time;