473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 helpful,
thanks
Dec 28 '07 #1
4 5394

"kyle christian" <sa*****@cox.netwrote in message
news:v8*******************@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

Dec 28 '07 #2
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 helpful,
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, MyClassData;
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, intScores;
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
ta*******@rocketmail.com
Dec 28 '07 #3
Thanks for the help 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::DisplayStats(){
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" <ta*******@rocketmail.comwrote in message
news:Gl************@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 helpful,
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, MyClassData;
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, intScores;
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
ta*******@rocketmail.com

Dec 30 '07 #4
[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
Jan 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rui Pacheco | last post by:
Hi everyone I have a servlet that queries a database, processes the ResultSet, turns it into a n String and sends it by Serialization into an applet that contains basically a JTable to display...
2
by: Anthony | last post by:
I have a team website that has several games for entertainment. One of the games is a javascript version of yahtzee. Can someone suggest a way, using javascript, to record a high score for the...
2
by: =?Utf-8?B?RGFtZW9u?= | last post by:
Hi - I am attempting to write lines to a file at high volume, multiple threads. Here is my scenario: (initial "WriteToFile" object created via a parent multithreaded process, which receives...
0
by: Learning.Net | last post by:
I have a window application that uses ActiveX browser component for testing web site automatically using mshtml. Though application is running fine but there is abnormally high page file usage....
1
by: santhescript01 | last post by:
I have a window application that uses ActiveX browser component for testing web site automatically using mshtml. Though application is running fine but there is abnormally high page file usage....
1
by: Flanders | last post by:
I have developed a small arcade game with the help of a few VB books. A scoring system was implemented in the design of the game but I as hoping that some one would be able to instruct me on how...
8
by: toefraz | last post by:
Hi, guys. I'm writing a little game, and I want to include a high scores list. I've got everything coded except for the sorting part. I want to use the std::sort() function, but I'm not quite sure...
3
by: hamishmcgee | last post by:
Ok, so for a project at university I have to create a High Score table in C++ with Visual Studio. Just to let you know this is my first year at university and also my first time ever learning C++....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.