Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HighScore.cpp Source File

HighScore.cpp

00001 #include "HighScore.h"
00002 
00003 HighScore::HighScore() {
00004     
00005 }
00006 
00007 HighScore::~HighScore() {
00008     
00009 }
00010 
00011 bool HighScore::save_test(int score,SDFileSystem &sd, N5110 &lcd) {
00012     printf("Score = %d  : ",score);
00013     
00014     save_new_high_score(sd, lcd, score); 
00015     
00016     int output_score = read_high_score(sd); 
00017     
00018     // Checks if the input and output are equal
00019     if (output_score == score) {
00020         printf ( "Passed!\n");
00021         return true;
00022     } else {
00023         printf ( "Failed!\n");
00024         return false;
00025     }
00026 }
00027 
00028 void HighScore::run_save_test(SDFileSystem &sd, N5110 &lcd) {
00029     printf ("\nTesting high_score_save_test() \n\n");
00030     int passed_counter = 0;
00031 
00032     // Runs test with all different possible inputs
00033     if (save_test(4, sd, lcd)) passed_counter++;
00034     if (save_test(3, sd, lcd)) passed_counter++;
00035     if (save_test(250, sd, lcd)) passed_counter++;
00036     if (save_test(-3, sd, lcd)) passed_counter++;
00037     if (save_test(222, sd, lcd)) passed_counter++;
00038     if (save_test(9999, sd, lcd)) passed_counter++;
00039     if (save_test(-9999, sd, lcd)) passed_counter++;
00040     if (save_test(0, sd, lcd)) passed_counter++;
00041        
00042     // prints results
00043     printf ("\n high_score_save_test %d tests out of 8\n\n\n",
00044     passed_counter);
00045 }
00046 
00047 void HighScore::init() {
00048     error_ = false;
00049 }
00050 
00051 void HighScore::no_high_scores(N5110 &lcd) {
00052     // Print error screen
00053     lcd.clear();
00054     lcd.printString("No High Scores",0,3);
00055     lcd.printString("Or No SD Card",3,4);
00056     lcd.refresh();
00057     wait(3); 
00058 }
00059 
00060 void HighScore::error_open_file(N5110 &lcd) {
00061     error_ = true;
00062 
00063     // Print error screen
00064     lcd.clear();
00065     lcd.printString(" Unable to  ",9,3);
00066     lcd.printString("Save",30,4);
00067     lcd.printString("High Score",12,4);
00068     lcd.refresh();
00069 }
00070 
00071 void HighScore::save_new_high_score(SDFileSystem &sd, N5110 &lcd, 
00072 int new_high_score) {
00073     FILE *fp;
00074     
00075     // Overwrites previous high score
00076     fp = fopen("/sd/HighScore.csv", "w");
00077     
00078      
00079     if (fp == NULL) {
00080         error_open_file(lcd);
00081             
00082     }else{   
00083         
00084         // Add data to file if no error
00085         fprintf(fp, "%d", new_high_score); 
00086         fclose(fp);
00087     }      
00088 }
00089 
00090 int HighScore::read_high_score(SDFileSystem &sd) {
00091     FILE *fp;
00092     int saved_high_score = 0;
00093        
00094     fp = fopen("/sd/HighScore.csv", "r");
00095 
00096     // Check if high score game files
00097     if (fp == NULL) {
00098         error_ = true;   
00099         
00100     // Reads current high score if no high score is set zero is returned
00101     }else{
00102         
00103         // Read save file and add values to saved_high_score int
00104         fscanf(fp, "%d", &saved_high_score);
00105           
00106         fclose(fp);           
00107     } 
00108      
00109     return saved_high_score;
00110 }
00111