Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

MenuClasses/Stats/Stats.cpp

Committer:
AhmedPlaymaker
Date:
2019-04-30
Revision:
61:f3c402bc2ad0
Parent:
50:3cf9a94a264e
Child:
62:ebf6ecf8a6d5

File content as of revision 61:f3c402bc2ad0:

#include "Stats.h"

Stats::Stats()
{
    
}

Stats::~Stats()
{
    
}

FILE *fp; // this is the file pointer
//serial.baud(115200);  // full-speed!
    
    
void Stats::StatsDisplay(N5110 &lcd, Gamepad &pad)
{
    lcd.clear();
    wait(0.2);
    //pad.init();
    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {  //Change this to if button pressed.
        lcd.printString("Highest Level",3,1);
        sprintf(bufferlevel,"%d",_stored_top_level);
        lcd.printString(bufferlevel,38,3);
        lcd.refresh();
    }
    lcd.clear();
    pad.tone(1000.0,0.1);
    /////////////////////// Deleting file ////////////////////////

    // comment this line out if I don't want to delete the file!
    //delete_file("/sd/highestlevel.txt");
}

void Stats::write(int level, SDFileSystem &sd)
{
    //////////////////////  writing to file while reading previous state //////////////////////////

    // open file for writing ('w') - creates file if it doesn't exist and overwrites
    // if it does. If you wish to add a score onto a list, then you can
    // append instead 'a'.

    Stats::read(sd);
    fp = fopen("/sd/highestlevel.txt", "w");
    _current_level = level;
    if (_stored_top_level < _current_level)  {
        _top_level = _current_level;
    }

    else {
        _top_level = _stored_top_level;
    }
    
    if (fp == NULL) {  // if it can't open the file then print error message
        //serial.printf("Error! Unable to open file!\n");
    } else {  // opened file so can write
        //serial.printf("Writing to file....");
        fprintf(fp, "%d",_top_level); // ensure data type matches
        //serial.printf("Done.\n");
        fclose(fp);  // ensure you close the file after writing
    }


}

void Stats::read(SDFileSystem &sd)
{
    ////////////////////// reading from previous state //////////////////////////

    // now open file for reading
    fp = fopen("/sd/highestlevel.txt", "r");
    _stored_top_level = 0;  // level 0

    if (fp == NULL) {  // if it can't open the file then print error message
        //serial.printf("Error! Unable to open file!\n");
    } else {  // opened file so can write
        fscanf(fp, "%d",&_stored_top_level); // ensure data type matches - note address operator (&)
        //serial.printf("Read %d from file.\n",stored_top_level);
        fclose(fp);  // ensure you close the file after reading
    }
}

void Stats::delete_file(char filename[])
{
    //serial.printf("Deleting file '%s'...",filename);
    FILE *fp = fopen(filename, "r");  // try and open file
    if (fp != NULL) {  // if it does open...
        fclose(fp);    // close it
        remove(filename);  // and then delete
        //serial.printf("Done!\n");
    }
    // if we can't open it, it doesn't exist and so we can't delete it
}