Archived

This topic is now archived and is closed to further replies.

Reading from a file for a game

This topic is 5153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, i''m creating a wrestling simulator. It''s going to put you in charge of a wrestling promotion and you have to run it. Anyways, the way I''m thinking about doing it is through inheritance, example: Have a Wrestler class then have different classes of different styles of wrestling that inherit from the wrestler class. I understand and can do inheritance, i''m just trying to lay it all out for you. Then I considered making a class for each individual wrestler and inherit his style of wrestling. Now here''s my problem: I don''t want all the wrestlers hardcoded into the program. I want them in a file where the program will read the needed stats from the wrestler at an appropriate time and change them as the wrestler gets better or worse. An example of what the file might look like: [Wrestler 1] Strength = 5 Intelligence = 8 etc. [Wrestler 2] Strength = 7 etc. And when it is needed, the wrester is called from the file along with his stats. Another thing i was thinking of a way of doing this was to have the wrestlers wrestling style be determined in the file as an attribute. If this made no sense, i''m sorry. But for anyone who understood it, any help or advice? I guess the basic thing i want to know is how to get the program to read certain aspects of a file and retrieve it''s attributes and test them, etc.

Share this post


Link to post
Share on other sites
Do you know how to use binary files? If you don't google for it, I don't think you'll have a problem with what you are trying to do if you know how to use binary files, if I understand what you are saying...

[edited by - FtMonkey on November 8, 2003 5:45:25 PM]

Share this post


Link to post
Share on other sites
I would recommend reading the entire file into a memory structure at the beginning of the game, then accessing the wrestling data in memory during the game. That assumes that you don''t have megabytes and megabytes of data.

If the game changed the information, you could write the data back out to the file at the end of the game.

Share this post


Link to post
Share on other sites
You probably don''t want to inherit the styles; you want to configure the styles. Has-a is usually much more flexible than Is-a.

Regarding parsing, I suggest using text format, and read the entire file in on start-up, and parse each line (OldGuy is right here). Note that binary vs text should be very little difference, although text is much easier to edit.

You''d configure each wrestler with a style which you get from a factory which can manufacture styles based on their names. Something like:



class Style {
public:
virtual void attack( WrestlerState * from, WrestlerState * to ) = 0;
virtual char const * name() = 0;
};

class StyleA : public Style {
public:
void attack( ... ) { ... }
char const * name() { return "Style A"; }
};

class StyleB : public Style {
public:
void attack( ... ) { ... }
char const * name() { return "Style B"; }
};

Style * StyleFromText( char const * text ) {
if( !strcmp( text, "StyleA" ) ) return new StyleA;
if( !strcmp( text, "StyleB" ) ) return new StyleB;
return 0;
}



You can make this better, more templatized or macro-driven, later, if necessary, but this structure will make it very flexible to begin with.

Share this post


Link to post
Share on other sites
So, you have your text file, or your save file(really doesn''t matter what extension it is), It''s as simple as opening the file and then reading the data, then closing the file. You determine the layout of the file, so you read in the file according to how it''s layed out. You can use keywords or certain integers to let your function that reads the file know where it is. For example say your file was like this.

Wrestler
Name 1
2
3
Wrestler
Name 2
3
5
END

you would read in a line, check if it''s end, then if it is you''re done, if not then it''s wrestler, so you''d read the name, strength, and intelligence of that wrestler. then read another line and repeat.

If you wanted a variable amount of attributes(say titles) you could do something like this:

Wrestler
name 1
2(strength)
3(intelligence)
4(This would be the number of titles the wrestler has)
titleA
titleB
titleC
titleD
END


So you pretty much just read in the file the way it''s layed out.

Share this post


Link to post
Share on other sites
Ok, so i''ll load all the information into memory when the program starts up. Then when the player saves the game, it''s written to a file and dumped out of memory. Do i understand this correctly? That sounds the best way to go. How do i teach my program to read the files line by line and stop or start on certain keywords and load it all into memory to be used by the game? And then how do i write it all back into the file? Is there any way someone can demonstrate loading information line by line from a file into memory and then once the information has been changed, writing it back into memory? I would be appreciative, as i learn by example.

Share this post


Link to post
Share on other sites
A binary file is like a text file let's say you wanted to write two numbers to a file you would do something like this.

//header declarations
#include <cstdio>

//put this in main or whatever
int a = 10;
int b = 15;
FILE *f=fopen("file.dat","w+"); //open the file for writting
fwrite(&a,1,sizeof(a),f);
fwrite(&b,1,sizeof(b),f);
fclose(f); //close the file


then to read it back in you would read it in the same order


int a =0, b= 0;
FILE *f=fopen("file.dat","r"); //open the file for reading
fread(&a,1,sizeof(a),f);
fread(&b,1,sizeof(b),f);
fclose(f);//close the file since you don't need it anymore


you could also have a structure and dump it into the file...for example..


struct A
{
int n1,n2;
}a;

//fill the structure with whatever values

void SaveFile()
{
FILE *f=fopen("poop","w");
fwrite(&a,1,sizeof(a),f);
fclose(f);
}

void OpenFile()
{
FILE *f=fopen("poop","r");
fread(&a,1,sizeof(a),f);
fclose(f);
}



You could also read the file as a text file and parse the information if you feel that would be better...You have to
make sure the sizeof() part is correct if is not you would
end up reading the information incorrectly.

[edited by - FtMonkey on November 8, 2003 6:30:58 PM]

Share this post


Link to post
Share on other sites
Ok, so the file is loaded to variables (obviously). And what exactly does sizeof() do? And how do u tell it to start reading information at a keyword, then stop when it hits the other keyword, example:

//////////////////// begin characters.dat ///////////////////

Wreslter
The Rock
Power
7
7
2
1



Announcer
Jim Ross
None
9
9
7

Interviewer
The Coach
None
4
7
7
8

Wrestler
Rey Mysterio
Highflyer
6
8
2
2

/////////////////// end characters.dat ///////////////////////



This would be a file that holds the characters in the game. When it is loaded into memory, it would read each individual character by noticing the keyword of his position in the company i.e Wrestler, interviewer, announcer, bookie, whatever their position. The second line would be their name, then they''re wrestling style (where none classifies them as a non-wrestler), then their strength on line 4, Intelligence on line 5, announcing skills on line 6, and skills as an interviewer on line 7. All skills are based on 1-10. There will be more skill and attributes, i''m just trying to give an example. It would load the characters into memory for the game to use, then when game is saved, written to a new file in a save game folder. So the original isn''t used and changed if a new game wants to be started. And how is each character loaded into memory, as an array assigned to a variable? Thanx for everyones help. I''m just trying to figure out the best way to do this!

Share this post


Link to post
Share on other sites
Ok, sizeof() simply returns the size in bytes for instance if you passed sizeof(int); then that would return the size of an integer in bytes. So when you are reading or writting to a file you need to tell the function how many bytes you want to write/read so you use sizeof(). About your problem since you don't have to deal with so much data simply load everything in memory here's an example on how you might want to do it...


struct WRESTLER
{
char position[20];
char name[20];
char style[20];
int strength,intelligence;
int skills,iSkills;
};

WRESTLER w[3]; //declare 3 wretlers



//just set the data from your program for example

w[0].intelligence = 6;
w[0].skills = 5;
w[1].skills = 4;
w[1].iSkills = 6;
//...

//save your file

FILE *f = fopen("data.dat","w"); //open a file for writting

fwrite(&w,1,sizeof(w),f); //write w to a file

fclose(f); //close the file


//when you want to read it

FILE *f = fopen("data.dat","r"); //open your file for reading

fread(&w,1,sizeof(w),f); //read the data into your array

fclose(f); //close the file




Another thing you might want to use text files instead so its easier to edit your data. Just have different keywords and use them to read in your game info.

[edited by - FtMonkey on November 9, 2003 4:15:35 AM]

Share this post


Link to post
Share on other sites