highscore structs

Started by
11 comments, last by Rob Loach 18 years, 11 months ago
Quote:Original post by Lonefox1
the main problem im having is i want to have the player names in the array also, would it be better to just have 2 arrays one with names and one with scores? im unsure how id get a name and a number into an array that are somehow related to each other but only sorting by the highscore. that was my main reason for the structs. any ideas? :|


if i understand your question, you can have an array of structs. then you can sort by highscore.score for example.
Advertisement
yeh its just hit me that i could do this O_o didnt even ponder i could have an array of a custom type. learn sumthin new everyday i guess :D
I made a highscore wrapper using TinyXML and some encryption a while ago. Let me see if I can find it.... Here it is:

highscorelist.h
#if !defined(_HIGHSCORELIST_H_)#define _HIGHSCORELIST_H_#include <string>#include <vector>namespace System{namespace File{// TODO Descending orderclass HighScoreItem{	public:	std::string Name;	int Score;	HighScoreItem(std::string name, int score) : Name(name), Score(score) { }};class HighScoreList{public:	bool Load(std::string Filename);	bool Save(std::string Filename, int NumberOfRecords = -1);	int AddRecord(std::string Name, int Score);	void ClearList();	std::string Name(int Position);	int Score(int position);	~HighScoreList(){		ClearList();	}	HighScoreList() : m_descending(true), m_key("lucid") {			}	void Sort();	int NumberOfRecords(){ return list.size(); }	bool DescendingOrder(bool desc){ m_descending = desc; Sort(); return m_descending; }	bool DescendingOrder(){ return m_descending; }		std::string Key(){ return m_key; }	std::string Key(std::string key){ return m_key = key; }	private:	std::vector<HighScoreItem*> list;	bool m_descending;	std::string m_key;};}}#endif


highscorelist.cpp
#include "highscorelist.h"#include "tinyxml/tinyxml.h"#include "file.h"#include "../text/encoding.h"#include <string>#include <vector>#include <algorithm>#include <sstream>namespace System{namespace File{	bool HighScoreItem_comp(HighScoreItem* i1, HighScoreItem* i2){		return (i1->Score > i2->Score);	}	bool HighScoreItem_comp2(HighScoreItem* i1, HighScoreItem* i2){		return (i1->Score < i2->Score);	}	void HighScoreList::Sort(){		if(m_descending)			std::sort(list.begin(), list.end(), HighScoreItem_comp);		else			std::sort(list.begin(), list.end(), HighScoreItem_comp2);	}	bool HighScoreList::Load(std::string Filename){		std::string filecontents;		if(System::File::DecryptFromFile(Filename,filecontents,m_key)){			if(filecontents == "") return true;			TiXmlDocument doc;			try{				doc.Parse(filecontents.c_str());				if(doc.Error())					return System::File::WriteContents(Filename,"");			} catch(std::exception a){				return System::File::WriteContents(Filename,"");			}			TiXmlHandle docHandle( &doc );			TiXmlElement* child = docHandle.FirstChild("record").Element();			for( child; child; child=child->NextSiblingElement() )				AddRecord(child->Attribute("name"), System::Text::ToInteger(child->Attribute("score")));						return true;		}		return System::File::WriteContents(Filename,"");	}	bool HighScoreList::Save(std::string Filename, int NumberOfRecords){		if(NumberOfRecords == 0) return true;		std::stringstream output;		if(NumberOfRecords < 0){			for(std::vector<HighScoreItem*>::iterator pos = list.begin(); pos != list.end(); pos++){				output << "<record name=\"" << (*pos)->Name << "\" score=\"" << (*pos)->Score << "\"/>";			}		} else {			for(std::vector<HighScoreItem*>::iterator pos = list.begin(); pos != list.end(); pos++){				if(NumberOfRecords-- > 0)					output << "<record name=\"" << (*pos)->Name << "\" score=\"" << (*pos)->Score << "\"/>";				else					break;			}		}		std::string s = output.str();		return System::File::EncryptToFile(Filename, s, m_key);	}	std::string HighScoreList::Name(int Position){		try{				HighScoreItem* item = list.at(Position);			return item->Name;		}catch(std::exception a){			return "Nobody";		}	}	int HighScoreList::Score(int Position){		try{				HighScoreItem* item = list.at(Position);			return item->Score;		}catch(std::exception a){			return 0;		}	}	int HighScoreList::AddRecord(std::string Name, int Score){		list.push_back(new HighScoreItem(Name, Score));		Sort();	}	void HighScoreList::ClearList(){		while(!list.empty()){			delete list.back();			list.pop_back();		}		list.clear();	}}}


It uses some of the other backbone from the engine, but it's easy to understand what it's doing. Enjoy.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement