[C/C++] Spiel - Connect

Dieses Thema im Forum "Projekte / Codes" wurde erstellt von Rushh0ur, 27. Februar 2011 .

Schlagworte:
  1. 27. Februar 2011
    Zuletzt von einem Moderator bearbeitet: 14. April 2017
    Spiel - Connect

    Hallo Comunity,

    ich möchte mein kleines Projekt vorstellen was heut sein Alpha-Stadium erreicht hat.
    Die Grundidee basiert auf dem Wikiartikel {bild-down: http://img809.imageshack.us/img809/4990/screenwv.jpg}


    Downloads:
    Projekt Source [Visual Studio C++ 2008]
    Projekt Binäris [Win 32/64]
    (Binäris sind hier Verboten von daher gibts keine, wenn jemand trotzdem welche haben will kann er sie in diesem Forum, wo ich das ganze auch Vorstelle, hollen)

    Und damit es nicht so leer ist ein Auszug der HighScoreManager Klasse:
    HighscoreManager.h
    Code:
    ////////////////////////////////////////////////////////////
    //
    // Highscore Manager
    // Copyright (C) 2011 ѕυвZєяo
    //
    ////////////////////////////////////////////////////////////
    
    #ifndef _HIGHSCOREMANAGER_H_
    #define _HIGHSCOREMANAGER_H_
    
    ////////////////////////////////////////////////////////////
    // Headers
    ////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    
    ////////////////////////////////////////////////////////////
    /// Defines a simple higshcore entry
    /// with name and points of a player
    ////////////////////////////////////////////////////////////
    struct SHighscoreEntry
    {
     TCHAR name[16];
     unsigned int points;
    };
    
    ////////////////////////////////////////////////////////////
    /// CHighscoreManager provides an highscore manager
    /// to load and save higscores from and to a file,
    /// and also to read, add and clear entrys.
    ////////////////////////////////////////////////////////////
    class CHighscoreManager
    {
     public:
    
     ////////////////////////////////////////////////////////////
     /// Default constructor
     ///
     /// \param sFile: path to file for load and save data
     ///
     ////////////////////////////////////////////////////////////
     CHighscoreManager (const tstring &sFile);
    
     ////////////////////////////////////////////////////////////
     /// Default destructor
     ///
     ////////////////////////////////////////////////////////////
     ~CHighscoreManager ();
    
     ////////////////////////////////////////////////////////////
     /// Compute the count of entrys in list
     ///
     /// \return coutn of entrys
     ///
     ////////////////////////////////////////////////////////////
     int GetEntryCount ();
    
     ////////////////////////////////////////////////////////////
     /// Returns the Data of an entry
     ///
     /// \param idx: the entry index
     /// from 0 to GetEntryCount-1
     /// \param name: stores the name of player to this
     /// \param points: stores the points of player to this
     /// \retrun true on success
     ///
     ////////////////////////////////////////////////////////////
     bool GetEntry (const int &idx, tstring &name, unsigned int &points);
    
     ////////////////////////////////////////////////////////////
     /// Check if a highscore is reached
     ///
     /// \param score the score to check
     /// \return rang if the given value is a new
     /// highscore, else 0
     ///
     ////////////////////////////////////////////////////////////
     int CheckHighscore(const unsigned int score);
    
     ////////////////////////////////////////////////////////////
     /// Add a new highscore to list
     ///
     /// \param name the players name
     /// \param score the players score
     /// \return rang of player
     ///
     ////////////////////////////////////////////////////////////
     int AddHighscore(const tstring &name, const unsigned int score);
    
     ////////////////////////////////////////////////////////////
     /// Clear and reset the entry list
     ///
     ////////////////////////////////////////////////////////////
     void ClearList ();
    
     ////////////////////////////////////////////////////////////
     /// Determinate if an file was found and loaded
     ///
     /// \retrun true if loaded, else false
     ///
     ////////////////////////////////////////////////////////////
     bool Loaded ();
    
     ////////////////////////////////////////////////////////////
     /// Loads an another file
     ///
     /// \param sFile: path to file for load and save data
     /// \return true if loaded, else false
     ///
     ////////////////////////////////////////////////////////////
     bool LoadFile (const tstring &sFile);
    
     ////////////////////////////////////////////////////////////
     /// Saves data to file
     ///
     /// \param sFile: path to file for save data
     /// \return true on sccuess
     ///
     ////////////////////////////////////////////////////////////
     bool SaveFile (const tstring &sFile);
    
     ////////////////////////////////////////////////////////////
     // Member data
     ////////////////////////////////////////////////////////////
     public:
     static const int MAXENTRYCOUNT = 10; //< Max entry count
     private:
     bool pbLoaded; //< Is a file loaded sucessfully
     tstring psFile; //< path to file for storage of data
     SHighscoreEntry plEntryList[MAXENTRYCOUNT]; //< name and points list
    };
    
    #endif // _HIGHSCOREMANAGER_H_
    
    HighscoreManager.cpp
    Code:
    ////////////////////////////////////////////////////////////
    //
    // Highscore Manager
    // Copyright (C) 2011 ѕυвZєяo
    //
    ////////////////////////////////////////////////////////////
    #include "HighscoreManager.h"
    
    /*************************************************************************************************/
    CHighscoreManager::CHighscoreManager(const tstring &sFile)
    {
     this->ClearList();
     this->LoadFile(sFile);
    }
    
    /*************************************************************************************************/
    CHighscoreManager::~CHighscoreManager()
    {
     this->SaveFile(this->psFile);
    }
    
    /*************************************************************************************************/
    int CHighscoreManager::GetEntryCount()
    {
     for (int i=0; i < this->MAXENTRYCOUNT; ++i)
     if (!this->plEntryList[i].name[0])
     return (i+1);
    
     return 0;
    }
    
    /*************************************************************************************************/
    bool CHighscoreManager::GetEntry(const int &idx, tstring &name, unsigned int &points)
    {
     if (idx < this->MAXENTRYCOUNT)
     {
     if (this->plEntryList[idx].name[0])
     {
     name = tstring(this->plEntryList[idx].name, 32);
     points = this->plEntryList[idx].points;
     return true;
     }
     }
    
     return false;
    }
    
    /*************************************************************************************************/
    int CHighscoreManager::CheckHighscore(const unsigned int score)
    {
     for (int i=0; i < this->MAXENTRYCOUNT; ++i)
     {
     if (this->plEntryList[i].name[0])
     {
     if (this->plEntryList[i].points < score)
     return (i+1);
     }
     else
     return (i+1);
     }
    
     return 0;
    }
    
    /*************************************************************************************************/
    int CHighscoreManager::AddHighscore(const tstring &name, const unsigned int score)
    {
     int rang = this->CheckHighscore(score);
     if (rang)
     {
     for(int i=this->MAXENTRYCOUNT-1; i >= rang; --i)
     this->plEntryList[i] = this->plEntryList[i-1];
    
     int len = sizeof(TCHAR) * 32;
     if (int(name.length() * sizeof(TCHAR)) < len)
     len = int(name.length() * sizeof(TCHAR));
    
     memcpy(this->plEntryList[rang-1].name, name.c_str(), len);
     this->plEntryList[rang-1].points = score;
     }
     return rang;
    }
    
    /*************************************************************************************************/
    void CHighscoreManager::ClearList()
    {
     memset(this->plEntryList, 0, sizeof(this->plEntryList));
    }
    
    /*************************************************************************************************/
    bool CHighscoreManager::Loaded()
    {
     return this->pbLoaded;
    }
    
    /*************************************************************************************************/
    bool CHighscoreManager::LoadFile(const tstring &sFile)
    {
     fstream file(sFile.c_str(), ios::in | ios::binary);
     if (!file.fail())
     {
     char cMagicID[4];
     unsigned int iCount;
     unsigned int iSize;
    
     // Get File Size
     file.seekg(0, ios::end);
     iSize = (unsigned int)file.tellg();
     file.seekp(0);
    
     // Check Data
     file >> cMagicID;
     if (!memcmp(cMagicID, "CHL",3))
     {
     file.read((char*)&iCount, sizeof(iCount));
     if (iSize == 3 + sizeof(iCount) + sizeof(this->plEntryList))
     {
     file.read((char*)&this->plEntryList, sizeof(this->plEntryList));
     this->pbLoaded = true;
     }
     else
     this->pbLoaded = false;
     }
     else
     this->pbLoaded = false;
     }
     else
     this->pbLoaded = false;
    
     this->psFile = sFile;
     return this->pbLoaded;
    }
    
    /*************************************************************************************************/
    bool CHighscoreManager::SaveFile(const tstring &sFile)
    {
     fstream file(sFile.c_str(), ios::out | ios::binary);
    
     if (!file.fail())
     {
     int iMaxEntryCount = this->MAXENTRYCOUNT;
     file << "CHL";
     file.write((char*)&iMaxEntryCount, sizeof(iMaxEntryCount));
     file.write((char*)&this->plEntryList, sizeof(this->plEntryList));
     return true;
     }
    
     return false;
    }
    
    Da dies mein erstes Projekt, im Sinne eines (komplexeren) Spieles ist, wäre ich über Anregung, Kritik und Wünsche erfreut.
    Und am Rande gesagt, es sind noch spezielle Objekt bei höheren Levels geplannt wie zusätzlich Zeit, Bomben und ja mal schauen evtl. kommt noch was dazu.

    PS: Die Kommentare im Source sind auf Englisch und da mein Englisch nicht das Beste ist wahrscheinlich mit vielen Fehlern; Übung macht den Meister.

    Mfg Rushh0ur
     
  2. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.