javascript - 如何在 JavaScript 中建模这个数据结构? (AngularJS)

标签 javascript angularjs oop object data-structures

我正在为一款每轮有很多问题的游戏制作应用程序。每个玩家都可以回答问题(多个玩家可以回答同一问题)并获得不同数量的分数。该应用程序在开始时有一个玩家列表和一个问题列表。

我不知道如何对此进行建模 - 我正在考虑每个问题都可以有一个字典、玩家对象的键以及他们获得的分数的值。我还考虑为每个玩家建立一个字典,其中包含问题对象的键和分数的值(如果他们没有回答,那么该问题就不是键)。

我不确定哪个是最好的选择,或者是否有更好的方法来做到这一点。让许多问题对象副本为许多玩家 float (或者反之亦然,对于其他选项)是一个好主意吗?

在我的 AngularJS 工厂中,我创建了一个 Player 类:

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

以及 QuestionList 和 Question 类

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));
}

我如何将它们联系起来?任何帮助将不胜感激,谢谢。

最佳答案

每一轮都有很多问题,每个问题都有很多选择(和分值),每个玩家都有很多选择,您可以将每个选项的分数相加。

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
}

关于javascript - 如何在 JavaScript 中建模这个数据结构? (AngularJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26029717/

相关文章:

javascript - 如何使固定的背景图像不固定在滚动条上?

javascript - Chrome最新版本(版本47.0.2526.80 m): navigator. webkitGetUserMedia返回一个没有停止功能的流

javascript - Ajax 提交功能不起作用

javascript - 在滚动 Angular 调整标题大小

java - 有什么办法可以减少这些线路吗?

c++ - c++基础题,动态内存分配

javascript - 使用 SPapp 添加母版页以托管 Web 母版页厨房

javascript - 将模板与 Controller 结合起来

javascript - Angular 指令访问范围内的值

python - 在 Python 中嵌套类 3 级 - 这是不好的做法吗?