TGC Codebase Backup



Score Table Functions by Lord_Migit

15th May 2011 17:00
Summary

These are some functions to establish and access a basic score table.



Description

See additions for the documentation.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    #include <fstream>
#include <string>
using namespace std;

int* iTopScores;
fstream Scores;
void InitalizeScoreTable();
bool CheckScore(int iPlayerScore);

void InitalizeScoreTable()
{
	int iNumberOfScores = 10;
	int iScore;
	iTopScores = new int[iNumberOfScores];
	string sScore;
	const char* cScore;

	if(!dbFileExist("scores.csv"))
	{
		dbMakeFile("scores.csv");
		for(int i=0; i<10; i++)
		{
			iTopScores[i] = 0;
		}
	}
	else
	{
		Scores.open("scores.csv");
		for(int i=0; i<iNumberOfScores; i++)
		{
			getline(Scores, sScore, ',');
			cScore = sScore.c_str();
			iScore = atoi(cScore);
			iTopScores[i] = iScore;
		}
		Scores.clear();
		Scores.seekg(0, ios_base::beg);
		Scores.close();
	}
}

bool CheckScore(int iPlayerScore)
{
	bool bHighScore;
	if(iPlayerScore > iTopScores[9]) 
	{
		//while((int i = 0) < 10)
		for(int i=0; i<10; i++) //replace with a while loop
		{
			if(iPlayerScore > iTopScores[i])
			{
				for(int j=9; j>i; j--)
					iTopScores[j] = iTopScores[j-1];
				iTopScores[i] = iPlayerScore;
				break;
			}
		}
		bHighScore = true;
	}
	else
		bHighScore = false;

	if(Scores.is_open())
	{
		MessageBox(NULL, TEXT("Scores was open."), TEXT("Info"), MB_OK);	
		Scores.close();
	}
	Scores.open("scores.csv", ios::in | ios::out | ios::trunc);
	
	if(Scores.is_open())
	{
		for(int i=0; i<9; i++)
		{
			Scores<<iTopScores[i];
			Scores<<",";
		}
		Scores<<iTopScores[9];
		Scores.close();
	}
	return bHighScore;
}