Ranking Scores

In your fancy, new game, the program dutifully track players and their scores. When the game is over, or at the end of a round, the players not only want to know their scores, but where each sits in the overall ranking. That process takes a bit more coding than just spitting out the scores.

Recently, I coded a simulation of a Bunco game. Bunco is a dice game, designed more for social interaction than competition. At the simplest level, players roll three dice and track their totals based on various aspects of the game.

As I worked on the simulation, I recognized that my round results summary would work better if I ranked the scores, high-to-low. After all, it was simple to loop through the player[] array and show the round results like this:

Round Results:
Player 1, score 11
Player 2, score 47
Player 3, score 95
Player 4, score 63
Player 5, score 51
Player 6, score 84

Above, the code loops through each element in a player[] array, then coughs up the related score. That’s easier to do than to sort the players by score and display the results like this:

Round Results:
#1 Player 3, score 95
#2 Player 6, score 84
#3 Player 4, score 63
#4 Player 5, score 51
#5 Player 2, score 47
#6 Player 1, score 11

My original plan for this month’s Exercise was to code a Bunco simulation, but it’s a big job. A smaller job is to code the portion of the program that ranks the six players’ scores, high-to-low as shown above.

For this month’s Exercise, your task is to create an array of six players, assign each a random score (1 to 100), sort the scores high-to-low, then display the results.

Remember: You must display the player number and their score, ranked. Just spitting out the scores isn’t enough. After all, Player 3 wants to know where she’s ranked.

Click here to view my solution, but please try the Exercise on your own before you take a peek.

Leave a Reply