ELEC2645 (2018/19) / Mbed 2 deprecated EL17MCD

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Scores.cpp Source File

Scores.cpp

00001 /* Scores.cpp
00002 Processes new scores and saves 
00003 the highest ones.
00004 23.4.19
00005 */
00006 #include "Scores.h"
00007 
00008 Scores::Scores()
00009 {
00010   _top_three[0] = 0;
00011   _top_three[1] = 0;
00012   _top_three[2] = 0;
00013 }
00014 
00015 Scores::~Scores()
00016 {
00017 
00018 }
00019 
00020 float Scores::score_calculator(int turns, int health) // Calculates score
00021 {
00022   float new_score = health * 100 / turns;
00023   _add_to_top_scores(new_score);
00024   return new_score;
00025 }
00026 
00027 void Scores::display_score(float current, N5110 &lcd)  // Display the current games end score
00028 {
00029   char buffer[14]; 
00030   sprintf(buffer, "%.0f", current); 
00031   lcd.printString(" Score :", 10, 4); 
00032   lcd.printString(buffer, 60, 4);
00033 }  
00034 
00035 void Scores::display_top_scores(N5110 &lcd) // Displays high scores 
00036 {
00037   char buffer[14]; 
00038   sprintf(buffer, "%.0f", _top_three[0]); 
00039   lcd.printString(" 1. ", 15, 2); 
00040   lcd.printString(buffer, 45, 2);
00041     
00042   sprintf(buffer, "%.0f", _top_three[1]); 
00043   lcd.printString(" 2. ", 15, 3); 
00044   lcd.printString(buffer, 45, 3);
00045     
00046   sprintf(buffer, "%.0f", _top_three[2]); 
00047   lcd.printString(" 3. ", 15, 4); 
00048   lcd.printString(buffer, 45, 4);
00049 }     
00050 
00051 void Scores::_add_to_top_scores(float new_score) // Ranks the top scores in descending 
00052 {                                                // order in an array
00053   if (new_score > _top_three[0]) { 
00054     _top_three[2] = _top_three[1];
00055     _top_three[1] = _top_three[0];
00056     _top_three[0] = new_score; 
00057   } else if (new_score > _top_three[1]) {
00058     _top_three[2] = _top_three[1];
00059     _top_three[1] = new_score; 
00060   } else if (new_score > _top_three[2]) { 
00061     _top_three[2] = new_score; 
00062   }    
00063 }