Advertisement
B1KMusic

[Highscore test] highscoretable.cpp

Jan 25th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include "highscoretable.h"
  5.  
  6. HighscoreTable::HighscoreTable()
  7. {/*{{{*/
  8.     // this is the initialization of the class. Here, you could,
  9.     // for example, read from a text file, and use that to populate
  10.     // the scores array. For demo purposes, though, I'm gonna generate a silly default table
  11.     int i;
  12.  
  13.     for(i = 0; i < 5; i++){
  14.         scores[i] = new HighscoreEntry();
  15.         scores[i]->score = (i+1) * 100;
  16.     }
  17.  
  18.     strcpy(scores[0]->name, "AAA");
  19.     strcpy(scores[1]->name, "BBB");
  20.     strcpy(scores[2]->name, "CCC");
  21.     strcpy(scores[3]->name, "DDD");
  22.     strcpy(scores[4]->name, "EEE");
  23. }/*}}}*/
  24.  
  25. HighscoreTable::~HighscoreTable()
  26. {/*{{{*/
  27.     int i;
  28.  
  29.     for(i = 0; i < 5; i++){
  30.         delete scores[i];
  31.     }
  32. }/*}}}*/
  33.  
  34. void
  35. HighscoreTable::insert(int score, const char *name)
  36. {/*{{{*/
  37.     int lowestID = getLowest();
  38.     HighscoreEntry *lowest = scores[lowestID];
  39.  
  40.     if(score > lowest->score){
  41.         lowest->score = score;
  42.         strcpy(lowest->name, name);
  43.     }
  44. }/*}}}*/
  45.  
  46. int
  47. HighscoreTable::getLowest()
  48. {/*{{{*/
  49.     int i,
  50.         lowest = scores[0]->score,
  51.         lowestID = 0,
  52.         temp;
  53.  
  54.     for(i = 1; i < 5; i++){
  55.         temp = scores[i]->score;
  56.         if(temp < lowest){
  57.             lowest = temp;
  58.             lowestID = i;
  59.         }
  60.     }
  61.  
  62.     return lowestID;
  63. }/*}}}*/
  64.  
  65. void
  66. HighscoreTable::swap(int a, int b)
  67. {/*{{{*/
  68.     HighscoreEntry *temp = scores[a];
  69.  
  70.     scores[a] = scores[b];
  71.     scores[b] = temp;
  72. }/*}}}*/
  73.  
  74. void
  75. HighscoreTable::sort()
  76. {/*{{{*/
  77.     // This is a neat sorting algorithm called bubble sort. Look it up
  78.     int i,
  79.         j,
  80.         needsSort;
  81.  
  82.     for(i = 0; i < 5; i++){
  83.         needsSort = false;
  84.  
  85.         for(j = 4; j > i; j--){
  86.             if(scores[j]->score > scores[j-1]->score){
  87.                 swap(j, j-1);
  88.                 needsSort = true;
  89.             }
  90.         }
  91.  
  92.         if(!needsSort){
  93.             break;
  94.         }
  95.     }
  96. }/*}}}*/
  97.  
  98. const char *
  99. HighscoreTable::getRankStr(int rank)
  100. {/*{{{*/
  101.     switch(rank){
  102.         case 1:  return "st";
  103.         case 2:  return "nd";
  104.         case 3:  return "rd";
  105.         default: return "th";
  106.     }
  107. }/*}}}*/
  108.  
  109. void
  110. HighscoreTable::reportRow(int rank)
  111. {/*{{{*/
  112.     HighscoreEntry *row = scores[rank];
  113.  
  114.     printf("%i%s \t%i\t %s\n", rank + 1, getRankStr(rank + 1), row->score, row->name);
  115. }/*}}}*/
  116.  
  117. void
  118. HighscoreTable::report()
  119. {/*{{{*/
  120.     int i;
  121.  
  122.     sort();
  123.     printf("Rank\tScore\tName\n");
  124.  
  125.     for(i = 0; i < 5; i++){
  126.         reportRow(i);
  127.     }
  128. }/*}}}*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement