A couple questions on Sequential Files

mostly c++ and coding questions in general

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1956 Views - Last Post: 04 May 2010 - 10:07 AM Rate Topic: -----

#1 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

A couple questions on Sequential Files

Posted 03 May 2010 - 01:26 PM

I hate asking for help immediately but with this final Project I am starting early so the stress does not explode and do me in. I have some questions that I am trying to figure out. First I am trying to make a game using c++ and the biggest thing that I am trying to do, is create the program that saves everything much like a save game file. It would include the stats of the player's character and items he has, as well as location on a map. I have to admit that I am feeling overwhelmed and that even the smallest of games right now is starting to overwhelm me. But first the questions I have.

I am trying to create a monster appendix, that on the random encounter which is based on the player's level it would be a random encounter. This appendix has to be a txt file. I have been looking online all over to try to find out(figure out) how I can take a monster file with a setup like this:

Name: White Dragon
Race: Dragon
HP: 250
Attack: 35
Defense: 60
EXP: 80
Gold: 300

And when the number for it comes up print those stats out( for example the dragon being something like NO.25). I can work out the math in subtracting health and figure the loops out. That is what I am having trouble with for the sequential files but also I cannot figure out where to begin by having it all in a class.

Other questions involves the dynamic objects by using pointers. I hate saying it but I am so confused and lost right now on where to begin its not funny and really aggravating as I've been doing reasonably well.

I'll post the code I have so far if wanted. so far I have fragments of files written up, and I am trying to figure out how to work out movement on a map.

I have a worldmap, character, monster, and the rand encounter files typed up roughly. Just overall I am stumped in trying to figure out how to have everything written and saved. Without having to create everything through programming. If I can at least for the monsters create a rough appendix and call them up that way great. Saving the game and all info and then later on loading it however is going to be difficult for me.

Is This A Good Question/Topic? 0
  • +

Replies To: A couple questions on Sequential Files

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 02:17 PM

When dealing with a complex problem, break it down. Tackle a subset of it. Or, try to solve a simpler variation of the problem.
Was This Post Helpful? 0
  • +
  • -

#3 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 02:22 PM

Right now, I want to at least get a monster appendix going. My professor wants at least something to be able to be written, saved and then loaded up for wed. this week. One bit of code I think is right is the random encounter I got going. I can at least, when I am relaxed enough was able to write up a quick pseudocode for the random encounter. It may not be prefect but I think the foundation I have is correct and close to a real game style encounter generator. I haven't had anything inherit it yet. The encounter setup I think is the easiest thing to generate as it uses random numbers. The other pieces for it however are a little bit harder.

#include <iostream>
#include <ctime>
using namespace std;

Class RandomEncounters()

int ChanceEncount;
int Encounter;
int Spawn; // or SpawnRate
srand((time(0));
void ChanceEncounter()
{
	CEncount= rand()%101;
	if(CEncount <=10)
	{
		return Spawn; // txt file to be made later for now
	}
}
void MonsterEncounter()
{
	if (ChanceEncount ==1)
	{
		spawn= rand()%PlayerLevel;
	}
	else
	{
		ChanceEncount=0;
	}
}

This post has been edited by Vou: 03 May 2010 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 02:23 PM

Have you decided how you're going to parse the file?

Based on your example you could read in a whole line at a time and break it at the first white space (after the identifier "name", etc...) or read it all into one structure and break accordingly, up to you really.

Have you though about serialization at all or are the text files with the monster data just for reference? It would be easier if it was stored like this:

White Dragon, Dragon, 250, 35, 60, 80, 300


or another delimiter of some nature
Was This Post Helpful? 0
  • +
  • -

#5 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 02:39 PM

I haven't thought about that part really. And Strings (and arrays) oddly are not really a strong point. The text files I plan on using are to be used in the final project I'm working on for class.
What do you mean by serialization? as in making the stats in the text file used in game? That was the plan eventually, the only other problem is making a saved game but that is towards the end. (if I have time to even include it)
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 02:43 PM

Serialization


Instead of bits, we could write text directly to a file. Loading/unloading file stuff, etc...
Was This Post Helpful? 0
  • +
  • -

#7 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 03:15 PM

So in how I have it written in the text everything would be saved and brought up like that? It looks like in a way it streamlines the whole process by calling up exactly what you want, not pieces. So we can use the data, then for the battle everything will go normally? As in if the dragon takes 20 damage the health will show it.
I understand that you would use a pointer to that monster by use of monster, and when it is dead it is deleted. Or when you die it is deleted and the game can be reloaded or start completely over from the beginning Correct? It is hard for me to convey questions and thoughts, I hope it makes sense. So much stuck in my head that trying to sort out my questions, thoughts and such is difficult especially when I have stress building of finals approaching.
Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 03:43 PM

Not quite following. You would "load" the appropriate data at the beginning and store it in monster files created at run-time. Loading pieces on the fly, directly from the text file doesn't make sense in this context.
Was This Post Helpful? 0
  • +
  • -

#9 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 03:57 PM

I'm probably confusing myself with instantiating objects and dynamic memory use. I think that what I'm trying to understand is if all the monster data is loaded when the program is started. How would you create the random encounter scenario in the game? As in how would you use the pointers. Like if the character is level 30, and they have a random encounter. How would you bring that monster's stats up from the text file so the character can fight the monster.
I understand that you can use the pointers, and use the new and delete to create and delete objects on the fly. This is the part I am trying to understand.

For what I am trying to do is a typical rpg random monster encounter. So is it better for everything to load up in the beginning such as the whole monster list. Or when you encounter the monster, the random generation runs through and pulls out that monster(rather a copy of, since it is a temporary object as the idea is you kill it. and it's space in memory is deleted, freeing up memory for something else. ) you fight the monster kill it. Get your points and gold. Then go on until the next encounter.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 04:39 PM

Quote

I think that what I'm trying to understand is if all the monster data is loaded when the program is started.
It doesn't have to be. It needs to be read from the file before it gets used by your program, obviously. But when is up to you. Modern games that load huge amounts of data usually spread and defer loading, so as to avoid long loading times.

Quote

How would you bring that monster's stats up from the text file so the character can fight the monster.
You a starting point. File data. You have an ending point. Data structures in game. You simply to have bridge this as needed. If you determine that you need certain monster data, and you don't already have this data at hand in the appropriate data structure, you at that moment must: read file into some data structure. Then process data structure appropriately until you get into the form you want.

Quote

I understand that you can use the pointers, and use the new and delete to create and delete objects on the fly. This is the part I am trying to understand.
Pointers has nothing to do with your whole issue about loading data. Pick a problem and solve that problem.

Quote

So is it better for everything to load up in the beginning such as the whole monster list.
No way to tell in an abstract manner. Ultimately, it's all viewable as part of a caching strategy. You need to strike the right balance between feasibility, performance, and player acceptability. If you try and load everything in the beginning, you impose high wait times in the beginning. You also then use up that much memory all at once.

Tradeoffs. Tradeoffs.
Was This Post Helpful? 0
  • +
  • -

#11 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 05:03 PM

I know there's a few key pieces that I need that all characters will have. Those being Name, Class(race) Hit points, experience points, base attack and defense, and for the monsters how much gold they'll have. The player will have his own gold which is a variable. other things would be their locations and movement. I had printed out the lecture my professor had done and I am trying to figure out how to take the code for reading(but also writing) and put it into a class so it can be used quickly and not be one extra piece in the Main function.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 05:19 PM

You're wrestling with a number of completely independent concepts. Functions, classes, file i/o, concepts of data structures, caching, file formats, etc. are all decoupled. If you're struggling with creating a separate function, focus on learning to do that. If you're struggling with OO syntax, focus on that. And so on.

Don't assume your solution takes a certain form. Don't assume it requires a class with 5 functions and 7 variables. Figure out what needs to happen with pen and paper, and then once you have a blueprint on paper, consider how to form it in code.
Was This Post Helpful? 0
  • +
  • -

#13 Vou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-May 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 07:02 PM

After sitting down and organizing myself at least a little bit. The first thing I want to do, is as Kya mentioned Serialize the monster list. So I can use it when I need it. Initially the only monsters that will be loaded after creation are like 5 low level monsters. As I had posted earlier, I want to do a numeric system. To be this seems easier.

The random encounter rolls, if the encounter is 1 then it will roll to see what spawns.
for level one character, I am thinking for at least here(until I am used to writing and reading files using fstream and ios) one of three monsters. The overall output I think would be something like this.

Monster No. 1
Name:Blue Slime
Race:Slime
HP:6
Attack:2
Defense:1
EXP:2
Gold: 1

This is my ideal set up. This is my header file (for now)
#ifndef Monster_h
#define Monster_h

class Monster
{
	private:
	int damageHi;
	int damageLo;
	int xpModify;
public:
~Monster();
Monster();
void monsStats();
void setXpMod(int xpMod);
void sayhi();
int getdmgHi()
{
	return damageHi;
}
int getdmgLo()
{
	return damageLo;
}
int getxpMod()
{
	return xpModify;
}
};
#endif 


I have two questions, but I'll ask the first(kind of multiple question/part) now as it is more important. Ok, I have my Monster_Appendix.txt file in the same folder as my project and all the source files with it. Just so I can run this in a main function just so I can make it display the stats.
(I'll try to work it as clearly as I can.)

How would I when that number comes up as the result from the random generation print out the text? Should they all be on one line in the txt file? Then use .getline() function with each of the pieces such as the monster number, name, class, hp ect ect and then print it out in the format I want. All of the pieces between the () of the function is what I mean. The other piece to this is it's location. But I want to right now get things so I can Write up the whole appendix, then write the code and be able to have things generate and show the results that I want. The sad thing is that I know it is a very simple procedure, the problem is that I am still learning(very much a newbie to programming as a whole) and the simple steps elude me.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 08:19 PM

you need to #include <fstream>

use ofstream like cout
use ifstream like cin

then you need to parse the file using a marker like "-" or ",".

Hope this helps you a little.
Such a big task can be daunting but as the rest have said you need to break it down into more mangaeable segments.
Then code each segment then hopefully piece it all together.
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: A couple questions on Sequential Files

Posted 03 May 2010 - 08:37 PM

Doesn't every respectable monster have a name and/or description associated with it? All the ones I know do ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2