Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a C++ question, in this question you need to create HightScoreManager an

ID: 665368 • Letter: T

Question

This is a C++ question, in this question you need to create HightScoreManager and User class.

HighScoreManager class should have a function to create a record and save it to a file organized descending based on the score and to print the top 10 High Scores to the screen. The class should look like the following. The High score structure should be stored in a binary file called highScore.dat file.

Class HighScoreManager

Private: Structure (username, score, date)

Public: CreateHighScore, UpdateHighScore, PrintHighScore

The user class will have the user login with his username if the username does not exist it will be added to a list of records. Once logged in the user will be able to update his info, set a high score, delete his information, or print all the high scores in a table format. The users will be stored in a text file called user.txt

The user class will contain the following:

Class User

Private: Structure (username, user info(between 1-4 fields)

Public: Login, CheckIfUserExist, UpdateUserList, DeleteUser, SetHighScore

           

In the main, the user will first login and then be shown a menu with the options to update his info, set a high score, delete his information, print all the high scores, exit.

Explanation / Answer

Here is the program :

include "race/highscore_manager.hpp"

#include <stdexcept>
#include <fstream>

#include "config/user_config.hpp"
#include "io/file_manager.hpp"
#include "io/utf_writer.hpp"
#include "race/race_manager.hpp"
#include "utils/constants.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"

HighscoreManager* highscore_manager=0;
const unsigned int HighscoreManager::CURRENT_HSCORE_FILE_VERSION = 3;

HighscoreManager::HighscoreManager()
{
    m_can_write=true;
    setFilename();
    loadHighscores();
}   // HighscoreManager

// -----------------------------------------------------------------------------
HighscoreManager::~HighscoreManager()
{
    saveHighscores();
    for(type_all_scores::iterator i = m_all_scores.begin();
                                  i != m_all_scores.end(); i++)
        delete *i;
}   // ~HighscoreManager

// -----------------------------------------------------------------------------
/** Determines the path to store the highscore file in
*/
void HighscoreManager::setFilename()
{
    if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL )
    {
        m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR")
                   + std::string("/highscore.xml");
    }
    else
    {
        m_filename=file_manager->getUserConfigFile("highscore.xml");
    }

    return;
}  

// -----------------------------------------------------------------------------
void HighscoreManager::loadHighscores()
{
    XMLNode *root = NULL;
    root = file_manager->createXMLTree(m_filename);
    if(!root)
    {
        saveHighscores();
        if(m_can_write)
        {
            Log::info("Highscore Manager", "New highscore file '%s' created. ",
                    m_filename.c_str());
        }
        delete root;
        return;
    }

    try
    {
        if(!root || root->getName()!="highscores")
        {
            if(root) delete root;
            root = NULL;
            throw std::runtime_error("No 'highscore' node found.");
        }

        // check file version
        int v;
        if (!root->get("version", &v) || v<(int)CURRENT_HSCORE_FILE_VERSION)
        {
            Log::error("Highscore Manager", "Highscore file format too old, a new one will be created. ");
            irr::core::stringw warning =
                _("The highscore file was too old, all highscores have been erased.");
            user_config->setWarning( warning );

         
            saveHighscores();
            delete root;
            root = NULL;
            return;
        }

        for(unsigned int i=0; i<root->getNumNodes(); i++)
        {
            const XMLNode *node = root->getNode(i);
            Highscores *highscores;
            try
            {
                highscores = new Highscores(*node);
            }
            catch (std::logic_error& e)
            {
                Log::error("Highscore Manager", "Invalid highscore entry will be skipped : %s ", e.what());
                continue;
            }
            m_all_scores.push_back(highscores);
        }  

        if(UserConfigParams::logMisc())
            Log::error("Highscore Manager", "Highscores will be saved in '%s'. ",
                    m_filename.c_str());
    }
    catch(std::exception& err)
    {
        Log::error("Highscore Manager", "Error while parsing highscore file '%s': ",
                m_filename.c_str());
        Log::error("Highscore Manager", "%s", err.what());
        Log::error("Highscore Manager", " ");
        Log::error("Highscore Manager", "No old highscores will be available. ");
    }
    if(root)
        delete root;
}


void HighscoreManager::saveHighscores()
{
   
    if(!m_can_write) return;

    try
    {
        UTFWriter highscore_file(m_filename.c_str());
        highscore_file << L"<?xml version="1.0"?> ";
        highscore_file << L"<highscores version="" << CURRENT_HSCORE_FILE_VERSION << ""> ";

        for(unsigned int i=0; i<m_all_scores.size(); i++)
        {
            m_all_scores[i]->writeEntry(highscore_file);
        }
        highscore_file << L"</highscores> ";
        highscore_file.close();
    }
    catch(std::exception &e)
    {
        Log::error("Highscore Manager","Problems saving highscores in '%s' ", m_filename.c_str());
        puts(e.what());
        m_can_write=false;
    }

}  

Highscores* HighscoreManager::getHighscores(const Highscores::HighscoreType &highscore_type,
                                            int num_karts,
                                            const RaceManager::Difficulty difficulty,
                                            const std::string &trackName,
                                            const int number_of_laps,
                                            const bool reverse)
{
    Highscores *highscores = 0;


    for(type_all_scores::iterator i = m_all_scores.begin();
                                  i != m_all_scores.end(); i++)
    {
        if((*i)->matches(highscore_type, num_karts, difficulty, trackName,
                         number_of_laps, reverse) )
        {
           
            return (*i);
        }
    }  

    highscores = new Highscores(highscore_type, num_karts, difficulty,
                                trackName, number_of_laps, reverse);
    m_all_scores.push_back(highscores);
    return highscores;
}  

Hope it helps !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote