使用12个元素的关联数组;
this.rolls = {1:[], 2:[], 3:[], 4:[], 5:[],
6:[], 7:[], 8:[], 9:[],10:[], 11:[], 12:[]};
获得前10个元素之和的最有效方法是什么。下面的代码目前汇总了所有元素;
var sum = 0;
for (var k in this.rolls) {
vals = this.rolls[k];
for (var i=0; i<vals.length; i++) {
sum += vals[i] || 0
};
};
this.score = sum
前10个元素是:
this.rolls = {1:[], 2:[], 3:[], 4:[], 5:[],
6:[], 7:[], 8:[], 9:[],10:[]};
以下是完整代码:
function Game() {
this.score = 0;
this.frameOver = false;
this.rolls = {1:[], 2:[], 3:[], 4:[], 5:[],
6:[], 7:[], 8:[], 9:[],10:[], 11:[], 12:[]};
this.currentFrame = 1;
// this.lastFrameStrike = false;
// this.lastFrameSpare = false;
// this.isStrike = false
};
Game.prototype.roll = function(pins) {
this.strikeOrSpare(pins);
this.bonusDistributor(pins);
this.rolls[this.currentFrame].push(pins);
this.scoreUpdater(pins);
this.frameHandler(pins);
this.nextFrameBonus(pins)
};
// --------------------------------------------------
Game.prototype.strikeOrSpare = function(pins) {
if (pins === 10) {
this.isStrike = true;
this.frameOver = true
}
else if (this.rolls[this.currentFrame][0] + pins === 10) {
this.isSpare = true;
this.frameOver = true
};
};
// --------------------------------------------------
Game.prototype.bonusDistributor = function(pins) {
if(this.wasSpare) { this.addToLastSpare(pins) };
if(this.wasStrike) { this.addToLast(pins) };
if(this.wasStrike2 && this.currentFrame > 1) { this.addToLastAgain(pins) };
};
// --------------------------------------------------
Game.prototype.addToLast = function(pins) {
this.rolls[this.currentFrame - 1][0] += pins
};
Game.prototype.addToLastAgain = function(pins) {
this.rolls[this.currentFrame - 2][0] += pins
};
Game.prototype.addToLastSpare = function(pins) {
this.rolls[this.currentFrame - 1][1] += pins;
this.wasSpare = false
};
// --------------------------------------------------
Game.prototype.scoreUpdater = function(pins) {
var sum = Object.keys(this.rolls).sort(function (a, b) {
return (+a) - (+b);
}).slice(0, 10).reduce(function (p, c) {
return p + this.rolls[c].reduce(function (p, c) {
return p + c;
}, 0);
}, 0);
};
Game.prototype.frameHandler = function(pins) {
if (this.frameOver) {
this.currentFrame++; this.frameOver = !this.frameOver
} else {
this.frameOver = !this.frameOver;
};
};
Game.prototype.nextFrameBonus = function(pins) {
if (this.isSpare) {
this.wasSpare = true;
this.isSpare = false
if (this.wasStrike) {
this.wasStrike = false;
this.wasStrike2 = true
}
} else if (this.isStrike && this.wasStrike) {
this.wasStrike2 = true
} else if (this.isStrike) {
this.isStrike = false;
this.wasStrike = true
} else if (this.wasStrike) {
this.wasStrike = false;
this.wasStrike2 = true
} else if (this.wasStrike2) {
this.wasStrike2 = false
};
};
// --------------------------------------------------
答案 0 :(得分:3)
您遇到问题的第一个问题是this.rolls
是Object
,而不是Array
,因此for (var k in this.rolls)
不是保证< / em>按顺序枚举键。所以要解决的第一个问题是获取前10个密钥,将它们转换为数字,然后对它们进行排序。在这里,我使用了所有原生的Array
和Object
方法:
var rolls = this.rolls;
var sum = Object
// Get all keys
.keys(rolls)
// Convert string keys to integers
.map(function (key) { return parseInt(key, 10); })
// Sort in ascending order
.sort()
// Take the first 10
.slice(0, 10)
// Get the arrays for each key
.map(function (key) { return rolls[key]; })
// Merge all arrays into one array
.reduce(function (allNumbers, array) { return allNumbers.concat(array); }, [])
// Sum all numbers
.reduce(function (sum, number) { return sum + (number || 0); }, 0);
答案 1 :(得分:1)
对对象键进行排序和过滤,然后使用它们来减少数组总和
var rolls = this.rolls;
var sum = Object.keys(rolls).sort(function (a, b) {
return (+a) - (+b);
}).slice(0, 10).reduce(function (p, c) {
return p + rolls[c].reduce(function (p, c) {
return p + c;
}, 0);
}, 0);
的 DEMO 强>
答案 2 :(得分:0)
已编辑:您的示例中有一个错误。您无法在forEach
内使用此。 roll 。声明它(var self = this;
或我的版本在var rolls = this.rolls;
之外)
我建议使用以下代码来计算总和:
var sum = 0;
var rolls = this.rolls;
Object.keys(rolls).forEach(function (key, i, array) {
if (i < 10) {
var item = rolls[key].slice();
while (item.length) {
sum += item ? Number(item.shift()) : 0 + item ? Number(item.pop()) : 0;
}
}
});
console.log(sum, this.rolls);
this.score = sum;
有完整的例子:
function Game() {
this.score = 0;
this.frameOver = false;
this.rolls = {1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [], 11: [], 12: []};
this.currentFrame = 1;
// this.lastFrameStrike = false;
// this.lastFrameSpare = false;
// this.isStrike = false
}
Game.prototype.roll = function (pins) {
this.strikeOrSpare(pins);
this.bonusDistributor(pins);
this.rolls[this.currentFrame].push(pins);
this.scoreUpdater(pins);
this.frameHandler(pins);
this.nextFrameBonus(pins);
};
// --------------------------------------------------
Game.prototype.strikeOrSpare = function (pins) {
if (pins === 10) {
this.isStrike = true;
this.frameOver = true;
}
else if (this.rolls[this.currentFrame][0] + pins === 10) {
this.isSpare = true;
this.frameOver = true;
}
};
// --------------------------------------------------
Game.prototype.bonusDistributor = function (pins) {
if (this.wasSpare) {
this.addToLastSpare(pins);
}
if (this.wasStrike) {
this.addToLast(pins);
}
if (this.wasStrike2 && this.currentFrame > 1) {
this.addToLastAgain(pins);
}
};
// --------------------------------------------------
Game.prototype.addToLast = function (pins) {
this.rolls[this.currentFrame - 1][0] += pins;
};
Game.prototype.addToLastAgain = function (pins) {
this.rolls[this.currentFrame - 2][0] += pins;
};
Game.prototype.addToLastSpare = function (pins) {
this.rolls[this.currentFrame - 1][1] += pins;
this.wasSpare = false;
};
// --------------------------------------------------
Game.prototype.scoreUpdater = function (pins) {
var sum = 0;
// var self = this;
var rolls = this.rolls;
Object.keys(rolls).forEach(function (key, i, array) {
if (i < 10) {
var item = rolls[key].slice();
while (item.length) {
sum += item ? Number(item.shift()) : 0 + item ? Number(item.pop()) : 0;
}
}
});
console.log(sum, this.rolls);
this.score = sum;
};
Game.prototype.frameHandler = function (pins) {
if (this.frameOver) {
this.currentFrame++;
this.frameOver = !this.frameOver;
} else {
this.frameOver = !this.frameOver;
}
};
Game.prototype.nextFrameBonus = function (pins) {
if (this.isSpare) {
this.wasSpare = true;
this.isSpare = false;
if (this.wasStrike) {
this.wasStrike = false;
this.wasStrike2 = true;
}
} else if (this.isStrike && this.wasStrike) {
this.wasStrike2 = true;
} else if (this.isStrike) {
this.isStrike = false;
this.wasStrike = true;
} else if (this.wasStrike) {
this.wasStrike = false;
this.wasStrike2 = true;
} else if (this.wasStrike2) {
this.wasStrike2 = false;
}
};
var game = new Game();
game.scoreUpdater();