{"id":2223,"date":"2016-12-01T00:01:48","date_gmt":"2016-12-01T08:01:48","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2223"},"modified":"2016-12-08T08:25:19","modified_gmt":"2016-12-08T16:25:19","slug":"ranking-scores","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2223","title":{"rendered":"Ranking Scores"},"content":{"rendered":"<p>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.<br \/>\n<!--more--><br \/>\nRecently, I coded a simulation of a Bunco game. <a href=\"http:\/\/www.dicegamedepot.com\/bunco-rules\/\" target=\"_blank\">Bunco<\/a> 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.<\/p>\n<p>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 <code>player[]<\/code> array and show the round results like this:<\/p>\n<pre><code>Round Results:\r\nPlayer 1, score 11\r\nPlayer 2, score 47\r\nPlayer 3, score 95\r\nPlayer 4, score 63\r\nPlayer 5, score 51\r\nPlayer 6, score 84<\/code><\/pre>\n<p>Above, the code loops through each element in a <code>player[]<\/code> array, then coughs up the related score. That&#8217;s easier to do than to sort the players by score and display the results like this:<\/p>\n<pre><code>Round Results:\r\n#1 Player 3, score 95\r\n#2 Player 6, score 84\r\n#3 Player 4, score 63\r\n#4 Player 5, score 51\r\n#5 Player 2, score 47\r\n#6 Player 1, score 11<\/code><\/pre>\n<p>My original plan for this month&#8217;s Exercise was to code a Bunco simulation, but it&#8217;s a big job. A smaller job is to code the portion of the program that ranks the six players&#8217; scores, high-to-low as shown above.<\/p>\n<p>For this month&#8217;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.<\/p>\n<p>Remember: You must display the player number <em>and<\/em> their score, ranked. Just spitting out the scores isn&#8217;t enough. After all, Player 3 wants to know where she&#8217;s ranked.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2238\">Click here<\/a> to view my solution, but please try the Exercise on your own before you take a peek.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Humans want to see scores ranked, not displayed in any other order. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2223\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2223","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2223"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2223\/revisions"}],"predecessor-version":[{"id":2256,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2223\/revisions\/2256"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}