PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > High Score File I/O
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
High Score File I/O

Réponse
 
LinkBack Outils de la discussion
Vieux 28/12/2007, 18h21   #1
kyle christian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut High Score File I/O

I am trying to save the high scores of a game I made. Now Im stumped on
trying to search through the file. Should I use a string, array, or one of
the STL containers to manipulate the information read from the file?

I have tried the
stringname.find("Name");

This only returns the first occurence, and doesn't look for other
occurences.

I got better results just using
if(Name[i] = = NewName)


The file is a simple record like so:

Name wins loses //where wins and loses are int's
Bob 1 0 //example of format

Its to be like an old Pac-Man game record keeper.
I want to convert the name into lowercase and check to make sure only one
name is present in the file. If more than one name erase the others
otherwise check to see if WinsOnFile < CurrentWins.
If so record the new high score to the appropriate name.

This will only hold 10 high scores for different people.

Any thoughts would be ful,
thanks


  Réponse avec citation
Vieux 28/12/2007, 19h04   #2
Christopher Pisz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: High Score File I/O


"kyle christian" <samkyle@cox.net> wrote in message
news:v8bdj.22586$db7.14998@newsfe12.phx...
>I am trying to save the high scores of a game I made. Now Im stumped on
>trying to search through the file. Should I use a string, array, or one of
>the STL containers to manipulate the information read from the file?
>
> I have tried the
> stringname.find("Name");
>
> This only returns the first occurence, and doesn't look for other
> occurences.
>
> I got better results just using
> if(Name[i] = = NewName)
>
>
> The file is a simple record like so:
>
> Name wins loses //where wins and loses are int's
> Bob 1 0 //example of format
>
> Its to be like an old Pac-Man game record keeper.
> I want to convert the name into lowercase and check to make sure only one
> name is present in the file. If more than one name erase the others
> otherwise check to see if WinsOnFile < CurrentWins.
> If so record the new high score to the appropriate name.
>
> This will only hold 10 high scores for different people.



std::string has all the functionality you need for this:

Read the file into a string.
Before writing a high score see if the name exists.
If so, move to the wins and losses part of the record
use std::string replace, or another one of its methods to replace wins and
losses for that name.
Write the string back to the file.
Done.

If you abide by that algorithm, I don't see more than one occurence of the
name ever being in the file.
If for some other reason it is than you can search in a loop until no
occurences are found. std::string has overloaded find methods that can take
an index to start from.

If you want to be more eligent, you could make a record class that holds
name, wins and losses. Then give it serialize and deserialize methods, which
read and write one line of text as a record. Provide a functor that returns
the name of the record. Then when the game loads you can read in the file
using a container of records searchable by name (std::map) . When the game
is over or when it exits. Write the map of records over the previous file.

gl,
Christopher



  Réponse avec citation
Vieux 28/12/2007, 20h51   #3
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: High Score File I/O

kyle christian wrote:
> I am trying to save the high scores of a game I made. Now Im stumped
> on trying to search through the file. Should I use a string, array,
> or one of the STL containers to manipulate the information read from
> the file?
> I have tried the
> stringname.find("Name");
>
> This only returns the first occurence, and doesn't look for other
> occurences.
>
> I got better results just using
> if(Name[i] = = NewName)
>
>
> The file is a simple record like so:
>
> Name wins loses //where wins and loses are int's
> Bob 1 0 //example of format
>
> Its to be like an old Pac-Man game record keeper.
> I want to convert the name into lowercase and check to make sure only
> one name is present in the file. If more than one name erase the
> others otherwise check to see if WinsOnFile < CurrentWins.
> If so record the new high score to the appropriate name.
>
> This will only hold 10 high scores for different people.
>
> Any thoughts would be ful,
> thanks


One easy way is to use a std::map. std::map has the advantage that if you
use code like this:

std::map<std::string, MyClass> Data;
Data["bob"] /*...*/
If "bob" doesn't already exist in the map, it will add it. Otherwise it
will refer to the one that already exists. For simplicity sake lets say you
were just recording number of wins.

std::map<std::string, int> Scores;
std::string Name;
int Wins;
/* Read name and Wins from file */
Scores[name] += Wins;

Now if there are 20 "Bob"s in the file, they will be added to one Score for
wins.

Things to do:
1. Convert name read to lowercase.
2. Store a structure or std::pair to represent wins and loses and not just
loses.


--
Jim Langston
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 30/12/2007, 08h17   #4
kyle christian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: High Score File I/O

Thanks for the guys. The map container works good. Heres what I got
after reading up on it so far:


//Gets user Name to use in file

void BlackJack::GetName(){
cout << "Enter your name: ";
cin >> Name;
UserInput();
}


//Adds record to the file

void BlackJack::RecordStats(){
ofstream InFile("c:/Dev-Cpp/text.txt", ios::app);
if(InFile){InFile << Name << " " << Won;}
else if(!InFile) { cout << "\nCouldn't open file";}
InFile.close();

}


void BlackJack::GetStats(){


//Write stats to file

RecordStats();

//Read the file into the map if open for reading

ifstream OutFile("c:/Dev-Cpp/text.txt");
if(!OutFile){
cout << "\nCouldn't Read File";
}
else{
map<string, int>Score;
string N;
int W;


while(!OutFile.eof()){
OutFile >> N >> W;
Score.insert(make_pair(N,W));
}
OutFile.close();

//Since the map only allows one key with the same name and if two keys of
the same name, it takes the key with the greater value by default
//I simply read the file into the map container so it could organize it for
me, and wrote it back to the file. There is probably a better way
//just dont know it yet


map<string, int>::iterator itr,itr1;
itr = Score.begin();

ofstream InFile("c:/Dev-Cpp/text.txt");
while(itr != Score.end()){
InFile << itr->first << " " << itr->second << endl;
itr++;
}
InFile.close();

itr1 = Score.begin();
while(itr1 != Score.end()){
cout << itr1->first << " wins: " << itr1->second << endl;
itr1++;
}

}
}
void BlackJack:isplayStats(){
cout << " VarWin: " << Won << " VarLost: " << Lost << endl;
GetStats();
}


Only thing I lack now is limiting the size to only the top 10 scores and
instead of the list being organized alphabetically I need the map to be
organized according to highest score.









"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:Glddj.37$pt.12@newsfe06.lga...
> kyle christian wrote:
>> I am trying to save the high scores of a game I made. Now Im stumped
>> on trying to search through the file. Should I use a string, array,
>> or one of the STL containers to manipulate the information read from
>> the file?
>> I have tried the
>> stringname.find("Name");
>>
>> This only returns the first occurence, and doesn't look for other
>> occurences.
>>
>> I got better results just using
>> if(Name[i] = = NewName)
>>
>>
>> The file is a simple record like so:
>>
>> Name wins loses //where wins and loses are int's
>> Bob 1 0 //example of format
>>
>> Its to be like an old Pac-Man game record keeper.
>> I want to convert the name into lowercase and check to make sure only
>> one name is present in the file. If more than one name erase the
>> others otherwise check to see if WinsOnFile < CurrentWins.
>> If so record the new high score to the appropriate name.
>>
>> This will only hold 10 high scores for different people.
>>
>> Any thoughts would be ful,
>> thanks

>
> One easy way is to use a std::map. std::map has the advantage that if you
> use code like this:
>
> std::map<std::string, MyClass> Data;
> Data["bob"] /*...*/
> If "bob" doesn't already exist in the map, it will add it. Otherwise it
> will refer to the one that already exists. For simplicity sake lets say
> you were just recording number of wins.
>
> std::map<std::string, int> Scores;
> std::string Name;
> int Wins;
> /* Read name and Wins from file */
> Scores[name] += Wins;
>
> Now if there are 20 "Bob"s in the file, they will be added to one Score
> for wins.
>
> Things to do:
> 1. Convert name read to lowercase.
> 2. Store a structure or std::pair to represent wins and loses and not just
> loses.
>
>
> --
> Jim Langston
> tazmaster@rocketmail.com
>



  Réponse avec citation
Vieux 01/01/2008, 21h18   #5
Christopher Pisz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: High Score File I/O

[snip]

> Only thing I lack now is limiting the size to only the top 10 scores and
> instead of the list being organized alphabetically I need the map to be
> organized according to highest score.
>

[snip]

if( mymap.size() > 10 )
{
// use some combination of mymap.find() and mymap.erase()
}

To sort by high score. Again, use std::sort and a mymap functor if needed.

,
Chris


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16099 seconds with 13 queries