0

I'm making an application for a game that has many questions per round. Each player can answer the question (multiple players can answer the same question) and receive a varying amount of points. The app has a list of players and a list of questions at the start.

I'm not sure how to model this - I was considering that each Question could have a dictionary , key of Player object and value of how many points they got. I also considered having a dictionary for each player that has a key of Question object and value of points (if they didn't answer then that Question isn't a key).

I'm not sure which is the best option or if there's a better way to do this. Is it a good idea to have many Question object copies floating around for many players (or vice-versa, for the other option)?

In my AngularJS factory I made a Player class:

function Player(name, heard) {
    this.name = name;
    this.heard = heard;
}

and a QuestionList and Question classes

function Question(number) {
    this.number = number;
}

function QuestionList() {
    this.questions;
}

QuestionList.prototype.createQuestions(n) {
    for (var i = 0; i < n; i++)
        this.questions.push(new Question(i + 1));
}

How do I relate them? Any help would be appreciated, thanks.

2
  • 1
    I assume players may come and go, but questions stay the same regardless of who's playing. This sounds like the info is stored in the player, not in the question. Have you considered an answers array as a property of the player? Each item in the array could contain the question (either question object reference or id number), the answer chosen, the points earned, the time answered, etc. Commented Sep 25, 2014 at 2:59
  • The players are the same for each round. Can you expand on the object reference/id number thing? I think that's what I need to avoid duplication. Also, I need to iterate over all the questions to show a scoreboard. In your method would I just go through each player searching for a question number/point value for each question number? Commented Sep 25, 2014 at 3:03

1 Answer 1

1

Each round has many Questions, and each Question has many choices (and point values), and each Player has many Choices where you add up the score of each.

function Round(questions){
    this.questions=questions;//array of Question instances
}

function Question(question){
    this.question=question;//the question "What's a green animal?"
    this.choices=choices;//array of choice instances
}
function Choice(question,choice,pointsWorth){
    this.question=question;//the question it belongs to---the parent class
    this.choice=choice;//"Alligator"
    this.pointsWorth=pointsWorth;//the correct answer is worth 5, wrong answers 0?
}
function Player(){
    this.choices=[];
}
Player.prototype.chooseChoice=function(choice){
    this.choices.push(choice);
}
Player.prototype.score=function(){
    return sum(this.choices);//You gotta write this function. this.choices[0].pointsWorth+this.choices[1].pointsWorth etc
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nah choices is just a question choice. So for the question "what is 1+1", a choice could be new Choice(question,"Two",5) and another could be new Choice(question,"Three",0)
Also, the choices and question text are answered out loud; the only thing I want to record is the point value - two values for correct depending on how fast they got the answer, some other value for incorrect, so I don't need a separate Choice for every question. I made a ScoreValue class that has the different score values though, and Round has a list of them. Does that change anything, or is the structure you wrote still fine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.