Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Scores/Scores.cpp@22:9e9685856ce1, 2019-05-09 (annotated)
- Committer:
- el17mcd
- Date:
- Thu May 09 13:45:05 2019 +0000
- Revision:
- 22:9e9685856ce1
- Parent:
- 21:44e87d88afe2
I have read and understood the university's guidelines on plagiarism.; I here by declare that all work is my own and no one else's.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17mcd | 16:a2c945279b79 | 1 | /* Scores.cpp |
el17mcd | 16:a2c945279b79 | 2 | Processes new scores and saves |
el17mcd | 16:a2c945279b79 | 3 | the highest ones. |
el17mcd | 16:a2c945279b79 | 4 | 23.4.19 |
el17mcd | 16:a2c945279b79 | 5 | */ |
el17mcd | 16:a2c945279b79 | 6 | #include "Scores.h" |
el17mcd | 16:a2c945279b79 | 7 | |
el17mcd | 16:a2c945279b79 | 8 | Scores::Scores() |
el17mcd | 16:a2c945279b79 | 9 | { |
el17mcd | 17:cb39d9fa08dc | 10 | _top_three[0] = 0; |
el17mcd | 17:cb39d9fa08dc | 11 | _top_three[1] = 0; |
el17mcd | 17:cb39d9fa08dc | 12 | _top_three[2] = 0; |
el17mcd | 16:a2c945279b79 | 13 | } |
el17mcd | 16:a2c945279b79 | 14 | |
el17mcd | 16:a2c945279b79 | 15 | Scores::~Scores() |
el17mcd | 16:a2c945279b79 | 16 | { |
el17mcd | 16:a2c945279b79 | 17 | |
el17mcd | 16:a2c945279b79 | 18 | } |
el17mcd | 16:a2c945279b79 | 19 | |
el17mcd | 21:44e87d88afe2 | 20 | float Scores::score_calculator(int turns, int health) // Calculates score |
el17mcd | 16:a2c945279b79 | 21 | { |
el17mcd | 17:cb39d9fa08dc | 22 | float new_score = health * 100 / turns; |
el17mcd | 17:cb39d9fa08dc | 23 | _add_to_top_scores(new_score); |
el17mcd | 17:cb39d9fa08dc | 24 | return new_score; |
el17mcd | 16:a2c945279b79 | 25 | } |
el17mcd | 16:a2c945279b79 | 26 | |
el17mcd | 22:9e9685856ce1 | 27 | void Scores::display_score(float current, N5110 &lcd) // Display the current games end score |
el17mcd | 16:a2c945279b79 | 28 | { |
el17mcd | 17:cb39d9fa08dc | 29 | char buffer[14]; |
el17mcd | 17:cb39d9fa08dc | 30 | sprintf(buffer, "%.0f", current); |
el17mcd | 17:cb39d9fa08dc | 31 | lcd.printString(" Score :", 10, 4); |
el17mcd | 17:cb39d9fa08dc | 32 | lcd.printString(buffer, 60, 4); |
el17mcd | 16:a2c945279b79 | 33 | } |
el17mcd | 16:a2c945279b79 | 34 | |
el17mcd | 22:9e9685856ce1 | 35 | void Scores::display_top_scores(N5110 &lcd) // Displays high scores |
el17mcd | 16:a2c945279b79 | 36 | { |
el17mcd | 17:cb39d9fa08dc | 37 | char buffer[14]; |
el17mcd | 17:cb39d9fa08dc | 38 | sprintf(buffer, "%.0f", _top_three[0]); |
el17mcd | 17:cb39d9fa08dc | 39 | lcd.printString(" 1. ", 15, 2); |
el17mcd | 17:cb39d9fa08dc | 40 | lcd.printString(buffer, 45, 2); |
el17mcd | 16:a2c945279b79 | 41 | |
el17mcd | 17:cb39d9fa08dc | 42 | sprintf(buffer, "%.0f", _top_three[1]); |
el17mcd | 17:cb39d9fa08dc | 43 | lcd.printString(" 2. ", 15, 3); |
el17mcd | 17:cb39d9fa08dc | 44 | lcd.printString(buffer, 45, 3); |
el17mcd | 16:a2c945279b79 | 45 | |
el17mcd | 17:cb39d9fa08dc | 46 | sprintf(buffer, "%.0f", _top_three[2]); |
el17mcd | 17:cb39d9fa08dc | 47 | lcd.printString(" 3. ", 15, 4); |
el17mcd | 17:cb39d9fa08dc | 48 | lcd.printString(buffer, 45, 4); |
el17mcd | 16:a2c945279b79 | 49 | } |
el17mcd | 16:a2c945279b79 | 50 | |
el17mcd | 21:44e87d88afe2 | 51 | void Scores::_add_to_top_scores(float new_score) // Ranks the top scores in descending |
el17mcd | 21:44e87d88afe2 | 52 | { // order in an array |
el17mcd | 17:cb39d9fa08dc | 53 | if (new_score > _top_three[0]) { |
el17mcd | 17:cb39d9fa08dc | 54 | _top_three[2] = _top_three[1]; |
el17mcd | 17:cb39d9fa08dc | 55 | _top_three[1] = _top_three[0]; |
el17mcd | 17:cb39d9fa08dc | 56 | _top_three[0] = new_score; |
el17mcd | 17:cb39d9fa08dc | 57 | } else if (new_score > _top_three[1]) { |
el17mcd | 17:cb39d9fa08dc | 58 | _top_three[2] = _top_three[1]; |
el17mcd | 17:cb39d9fa08dc | 59 | _top_three[1] = new_score; |
el17mcd | 17:cb39d9fa08dc | 60 | } else if (new_score > _top_three[2]) { |
el17mcd | 17:cb39d9fa08dc | 61 | _top_three[2] = new_score; |
el17mcd | 17:cb39d9fa08dc | 62 | } |
el17mcd | 16:a2c945279b79 | 63 | } |