Reputation: 1808
I need help with some design issues on creating a trivia quiz game. Before moving on i could use some advice if following approach is OK/Improvable/prone to failure/.. . This is what I have come up with so far:
public class Question {
private String question;
private List<Answer> answers;
private int statisticsCorrectness;
}
public class Answer {
private String answer;
private boolean correct;
}
I than create questions in a holder class which I can use to get a set of questions (This is where im unsure, seems wrong to put it in a class, have never seen it before)
public class StaticStuff {
public Module getModuleOne(){
Module module;
//Question1
Answer a = new Answer("Ten", true);
Answer a1 = new Answer("Five", false);
Answer a2 = new Answer("Six", false);
Answer a3 = new Answer("Eight", false);
Answer a4 = new Answer("10", true);
Answer[] listOfAn = new Answer[]{a, a1, a2, a3, a4};
Question q1 = new Question("What is 5 plus 5", Arrays.asList(listOfAn));
//End Question1
//Question2
//....
//End Question2
//Add all questions
Question[] questionsForFirstModule = new Question[]{q1};
//Create the module that will be returned
module = new Module("Introduction", Arrays.asList(questionsForFirstLession));
return module;
}
}
The way I planned to use it would be to get each Module object saved in the SharedPreferences the first time the app runs (Using Json or parcelable). Than when I need the set of questions I just get the object from the SharedPreferences and send it back "updated" to SharedPreferences.
If a clear cut question need to be specified it would be: is there much drawback saving objects this fashion compared to using a database. Maybe I should go for a database approach (Something I just realized after writing all this) ?
I looked at the first three pages on google "Trivia game java tutorial" but didn't really get enough info from that.
Upvotes: 3
Views: 800
Reputation: 614
Using an external data file would be better here. I wrote a Millionaire type Quizz game for a coding challenge some time ago, and I used something like this:
Question - Question text - Answers (array[4] of string)
That was filled by reading a simple text file, today you could use JSON or XML for that, but a plain text file works well, it's just not as flexible.
You could use Room or something similar for this, but keep in mind that editing these question will be something you do do a lot, so make this as easy as possible for you.
In my version, the first answer in the data was always the correct one. I randomised the order when displaying them, mainly to make it more easy to write down the questions, because that gets a lot of work, and you want to make it easy for you.
Consider storing how often the questions have already been displayed in combination with a timestamp so you can avoid repeating them if possible.
In android, create an activity that displays the question with the possible answers, and on click displays the result of the choice, before changing the ViewModel data to display the next question.
Score and other variables can be persisted by using the usual ways to take care of state changes.
Upvotes: 1