Note that there are some explanatory texts on larger screens.

plurals
  1. POMake 3 levels of difficulties in a quiz app
    primarykey
    data
    text
    <p>I am developing a simple quiz app on Android. The questions in the quiz should become more difficult as advancing in the quiz. Now, I am trying to implement a static method that takes a list of <code>Question</code> objects and produces (pick) a sub-list of the appropriate questions depending on the difficulty and it should be ordered (First question in the list is the simplest one). The app has 3 levels (modes) of difficulties.</p> <p>Here is the method snippet:</p> <pre><code>public static List&lt;Question&gt; getQuestions(List&lt;Question&gt; availableQuestions, int quizDifficulty, int numberOfQuestion) { if(availableQuestions.size() &lt; numberOfQuestion) throw NotEnoughQuestionsException(); List&lt;Question&gt; questions = new ArrayList&lt;Question&gt;(numberOfQuestion); if(quizDifficulty == 0) // Easy { // ... return questions; } else if(quizDifficulty == 2) // Hard { // ... return questions; } else /*if(quizDifficulty == 1)*/ // Normal { // ... return questions; } } </code></pre> <p>Each <code>Question</code> object has a field <code>difficulty</code> which is in range <code>1 (most simple)</code> to <code>10 (most difficult)</code>, and this field can be access by <code>getDifficulty()</code> method.</p> <p>As I though of a way to implement the method, I decided to make the questions' difficulties should not exceed level <code>8</code> for <code>Easy</code> mode, and they should be more than level <code>3</code> in the <code>Hard</code> mode, and in between level <code>9</code> and level <code>2</code> in the <code>Normal</code> mode.</p> <p>The problem is that the provided list of questions <code>availableQuestions</code> is not guaranteed to contain all the required levels of difficulties, for example, all the questions are in level 1.</p> <p>So, my question is, what is the best idea to implement this method?</p> <hr> <p><strong>EDIT:</strong></p> <p>Here is my progress so far:</p> <pre><code>public static List&lt;Question&gt; getQuestions(List&lt;Question&gt; availableQuestions, int quizDifficulty, int numberOfQuestion) { if(availableQuestions.size() &lt; numberOfQuestion) throw NotEnoughQuestionsException(); List&lt;Question&gt; questions = new ArrayList&lt;Question&gt;(numberOfQuestion); Map&lt;Integer, List&lt;Question&gt;&gt; map = new HashMap&lt;Integer, List&lt;Question&gt;&gt;(); for(int i = 1; i &lt;= 10; i++) map.put(i, new ArrayList&lt;Question&gt;()); for(Question question : availableQuestions) map.get(question.getDifficulty()).add(question); int L1 = map.get(1).size(); // number of questions with level 1 int L2 = map.get(2).size(); int L3 = map.get(3).size(); int L4 = map.get(4).size(); int L5 = map.get(5).size(); int L6 = map.get(6).size(); int L7 = map.get(7).size(); int L8 = map.get(8).size(); int L9 = map.get(9).size(); int L10 = map.get(10).size(); final int L1_TO_L8 = 0; final int L1_TO_L9 = 1; final int L1_TO_L10 = 2; final int L2_TO_L9 = 3; final int L2_TO_L10 = 4; final int L3_TO_L10 = 5; int status; if(difficulty == 0) // Easy (level 1 to level 8) { int missing = questionsCount - (L1+L2+L3+L4+L5+L6+L7+L8); if(missing &gt; 0) // not enough questions in L1 through L8 { if(missing - L9 &gt; 0) // we must include all the level { status = L1_TO_L10; } else // enough questions in L1 through L9 { status = L1_TO_L9; } } else // enough questions in L1 through L8 { status = L1_TO_L8; } } else if(difficulty == 2) // Hard (level 3 to level 10) { int missing = questionsCount - (L3+L4+L5+L6+L7+L8+L9+L10); if(missing &gt; 0) // not enough questions in L3 through L10 { if(missing - L2 &gt; 0) // we must include all the level { status = L1_TO_L10; } else // enough questions in L2 through L10 { status = L2_TO_L10; } } else // enough questions in L3 through L10 { status = L3_TO_L10; } } else /*if(difficulty == 1)*/ // Normal (level 2 to level 9) { int missing = questionsCount - (L2+L3+L4+L5+L6+L7+L8+L9); if(missing &gt; 0) // not enough questions in L2 through L9 { if(missing - L1 &gt; 0) // we must include all the level { status = L1_TO_L10; } else // enough questions in L1 through L9 { status = L1_TO_L9; } } else // enough questions in L2 through L9 { status = L2_TO_L9; } } // ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload