Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    122
  • comments
    121
  • views
    68818

My current project, among other things

Sign in to follow this  
Twisol

218 views

Currently I'm working on a set of header functions to be used for creating an "arena battle" game (text-based). My curent focus is on the store (where you spend money you get from winning arena battles), and it's been a bit tough. I managed to get the LoadStore and SaveStore functions working, though. When the user of these headers (specifically, cStore.h - don't ask about the c, please) creates an object of type store (the name I gave to my store class), the user passes a string argument with the name of the file they want to use to load shop data from. The constructor then calls LoadStore(), which parses every line of the file given and separates the lines at certain points. Hmm, maybe I should give an example.


// ...
#include "cStore.h"

store shop("shop.txt"); //Calls the constructor (you're probably thinking, "DUH!")

// ... skipping to cStore.h (EDIT: Everything from here down is in "class store")

private:
map shopMap;
string file;
ifstream shopIn;
ofstream shopOut;

/*
*Please pretend there's a public: here... I don't want to have
*to re-indent my code 8 spaces. >_<
*/

store(string userfile)
{
shopIn.open(userfile.c_str()); //This opens an ifstream with the user-specified file
cout << "Loading Store\n"; //Just for testing purposes; I want to make sure I don't break my code somehow.
LoadStore(); //Uh... loads the store?
}

~store()
{
if (shopIn.is_open()) shopIn.close();
if (shopOut.is_open()) shopOut.close();
SaveStore(); //Saves the store to a file
}

void LoadStore()
{
string line, key;
string item[2];
do
{
shopIn >> line; //Gets a line from shopIn every iteration of the do-while
if ((line != (item[0] + '%' + item[1])) && (line != "")) //If the line is not the same as the line before...
{
item[0] = PairSplit(line); //...then parse the line and save the first half here...
item[1] = PairSplit(line, 1); //..and save the second half here.
shopMap.insert(pair(item[0], item[1])); //Insert the key-value pair into the shopMap.
cout << shopMap[item[0]] << endl; //Debugging cout statement.
}
} while (!shopIn.eof());
}

void SaveStore()
{
shopOut.open(file.c_str()); //opens an ofstream to the user-specified file
if (!shopMap.empty()) //if the shopMap is not empty
{
map::iterator forIter = shopMap.begin(); //iterator starting at the beginning of the map
map::iterator endIter = shopMap.end(); //iterator that stays at the end of the map
string line;
do
{
if (forIter->first != "" && forIter->second != "") //If forIter does not contain null values...
{
line = forIter->first + '%' + forIter->second; //...concatenate them using the %...
cout << line << endl;
shopOut << line << endl; //...and print them to the file.
forIter++; //Increment forIter.
}
} while (forIter != endIter);
shopOut.close();
}
}




Uh... that wasn't as explanatory as I wanted it to be. [sad] Well... basically, when LoadStore is called, it parses the file and splits it into two variables at the '%' character. If the line "lp%Life_Potion$30" was in the file, we'd now have "lp" and "Life_Potion$30", paired and placed into the map. When the shop is saved, the process is reversed (concatenating the key and value with the '%').

Call me a newb, I think I posted this too early in the day. My mind needs to load up on gas. >_> I'm just too excited about getting a journal. :P As a question for anyone reading this, is it acceptable to post twice a day?

EDIT: Forgot to end the source tag. >_<
Sign in to follow this  


3 Comments


Recommended Comments

It is acceptable to post twice in one day when you have a reason. Nothing preventing you from even if you don't.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!