0

I have an object called "Player" with properties such as "score", "turns", "name", etc. Based on how many players the user chooses to have, I create an array of players.

As the game is played, I update each player's score. What I need to do is compare the "score" from the current player against all the other players. If it is higher, said player wins. Currently the only way I've been able to do this is to create a temporary array with the score values of all players and reorder the array from highest to lowest. I am then checking to see if the current player's score is the same as the score in the 0 index of my temp array. This can cause potential problems though, as two players COULD have the same score, at which point I have no way of correlating the score in the temp array with the player to whom it belongs to.

Is what I'm currently doing the best possible choice? There has to be a way to optimize this. Thanks for any help!

2
  • gjunkie, can you post your code. That will help others to answer. Commented Nov 21, 2011 at 6:17
  • var winner = function(thisPlayer){ var playerScores=[]; for(i=0;i<7;i++){ for(i=0;i<players.length;i++){ playerScores.push(players[i].score); playerScores.sort(function(a,b){return b - a}) // Sort scores } if(thisPlayer.score == playerScores[0]){ winnerAlert(thisPlayer.name); } } } Commented Nov 21, 2011 at 6:46

2 Answers 2

1

You could just look through the array for the highest score and then see if that's your player or not.

var playerInfo = [
    {name: "Bob", score: 70},
    {name: "Jill", score: 98},
    {name: "Ted", score: 96}
];

function findHighestScore() {
    // assumes there is always at least one entry in the playerInfo array
    var highestIndex = 0;
    var highestScore = playerInfo[0].score;
    for (var i = 1; i < playerInfo.length; i++) {
        if (playerInfo[i].score > highestScore) {
            highestIndex = i;
            highestScore = playerInfo[i].score;
         }
     }
     return(highestIndex);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi jfriend00, this looks like it might work! quite simple when you see it written down. Of course now I'm having issues updating the score values to each player, so I can't fully test it...
I went back and figured out my other bugs. This solution works like a charm. Thank you!
0

Why can't you iterate over the array of players and simply check the score of each versus the current player?

Instead of keeping an Array of scores could you keep an array of players sorted by their score?

Does this help?

http://www.javascriptkit.com/javatutors/arraysort2.shtml

3 Comments

Hi Eric, my issue is that I have a Winner() function that runs to check if there is a winner. This function runs at the end of each player's turn, and I pass it the player that's running it: Winner(player2). So I can see player2's score, but I need to see if it is higher than the other players, regardless of how many players there are.
so loop over all the players and check to see if the score of the passed in player is higher then all the others. Once you find one that is higher you can stop.
Right, I guess that's exactly what I'm trying to do, but I don't know how. Do you have an example?

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.