我正在开发基于JS的游戏,我正在跟踪几个用户的分数。在比赛结束时,我需要申报胜利者。可能有联系。
所以,有两个例子:
player1 = 3
player2 = 5
player3 = 4
(玩家2是赢家)
player1 = 4
player2 = 2
player3 = 4
(球员1和3平局)
只有3名玩家,我可能只是通过一堆嵌套的if / elses比较每个值来做到这一点,但是想知道是否有更优雅的方式来解决这个问题...特别是在你可能有10个玩家作为例子,if / else比较会很快变得笨拙。
一个选项似乎是将值粘贴在一个数组中,然后对其进行排序,但这并没有给出变量名称 - 只是值。然后,我可以做一个多维数组,但那时事情似乎再次变得混乱。
答案 0 :(得分:7)
没有简单的方法来迭代变量名称,如player1
,player2
等...而不使用eval()
或要求这些变量是全局变量,这通常是一个坏的要走的路。
如果它们是全局变量,你可以使用这个hack,但我会建议我的第二个方法,它将玩家信息存储为一个对象数组。这是一个使用全局变量的黑客(不推荐,但更直接地回答你的问题):
// these must be global variables
var score1 = 100;
var score2= 99;
var score3= 80;
function findHighScore() {
var highestScore = score1;
var highestScorePlayerNum = 1;
var tempScore;
for var (i = 2; i <= 3; i++) {
// this works only because the score variables are global variables which
// can be accessed as properties off the window object
tempScore = window["score" + i];
if (tempScore > highestScore) {
highestScore = tempScore;
highestScorePlayerNum = i;
}
}
return {score: highestScore, playerNum: highestScorePlayerNum};
}
更好的方法是不将每个玩家的得分保留在自己的javascript变量中,而是将其放入像这样的对象数组中:
var playerData = [
{name: "John", highScore: 145, lastPlayed: 23089234},
[name: "Bill", highScore: 99, lastPlayed: 59383945}
];
然后,您可以循环播放玩家数据,找到最高分并获得该玩家的名字。
function findHighScore() {
var highScoreSoFar = 0;
var result;
for (var i = 0; i < playerData.length; i++) {
if (playerData[i].highScore > highScoreSoFar) {
result = playerDatra[i];
highScoreSoFar = playerData[i].highScore;
}
}
return result;
}
var highPlayer = findHighScore();
alert("Highest scoring player is " + highPlayer.name + " with a score of " + highPlayer.highScore);
如果你想归还所有并列最高分的球员,你可以归还一组得分最高的球员:
function findHighScore() {
var highScoreSoFar = 0;
var result = [];
for (var i = 0; i < playerData.length; i++) {
if (playerData[i].highScore > highScoreSoFar) {
// new high score so start a new array with this player in it
result = [playerData[i]];
highScoreSoFar = playerData[i].highScore;
} else if (playerData[i].highScore === highScoreSoFar) {
// more than one player with this highScore so add this one to the array
result.push(playerData[i]);
}
}
return result;
}
答案 1 :(得分:2)
var players = []; // Array of player scores.
// Assumes that all player scores are higher than -1. You could change this.
var winnerScore = -1, winner = 0;
for (var i = 0; i < players.length; i++) {
if (players[i] > winnerScore) {
winner = i;
winnerScore = players[i];
}
}
alert("The winner was player " + winner + "!");
这应该可以解决问题:)如果你想处理关系,你可以这样做:
var players = []; // Array of player scores.
// Assumes that all player scores are higher than -1. You could change this.
var winnerScore = -1, winners = [];
for (var i = 0; i < players.length; i++) {
if (players[i] > winnerScore) {
winners = [i];
winnerScore = players[i];
} else if (players[i] == winnerScore) {
winners.push(i);
}
}
if (winners.length == 1) {
alert("The winner was player " + winners[0] + "!");
} else {
alert("The winners were players " + winners.join(", ") + "!");
}
答案 2 :(得分:2)
var players = {
player1: 3,
player2: 5,
player3: 4
};
var sorted = Object.keys(players)
.sort(function(keya, keyb) {
return players[keyb] - players[keya];
})
.forEach(function(key) {
console.log(key, players[key]);
})
DEMO: http://jsfiddle.net/WUAhS/
player2 5
player3 4
player1 3
如果您想要的只是高分,请使用.reduce()
。
var high_key = Object.keys(players)
.reduce(function(keya, keyb) {
return players[keya] > players[keyb] ? keya : keyb;
});