Advertisement

C++ Workshop - Project 2

Started by September 27, 2006 04:56 PM
94 comments, last by Warlord_Shaun 16 years, 9 months ago
Quote:
Original post by Zahlman
That's not a reason, and to be honest you're making less and less sense. Please, I'm trying to make this easier for you. Why resist that?


Please dont take me the wrong way. Im not resisting you in anyway, infact im incredibly gracious for your help. You have to be patient with me as this is my first real programing language and ive only been at this since a few weeks before project 1 was assigned. I know what I want to do, but unfortunately I dont know how to convey it.

If you look at how I did the menu system in my first project, you might get a sense at how im approaching this. clickie

The idea is for my menu system to dynamically build itself based on the users location in the world. Each location is its own object stored in a multi array. There will be menu object pointers belonging to each object that will be used to create a custom menu based on where the user is. This idea will also be ported over to the item/container system.

That being said, im still working on concepts before I can start coding for project 2. The largest concept being vectors and reading/writing from/to a file.

After your posts I went back and cleaned up the code and came close to what I want...

#include <fstream>#include <string>#include <vector>#include "object.h"#include <iostream>using namespace std;int main(){    vector <Object*> Objv;	ifstream fin("test.csv"); // open the file	std::string objtext;	int objid;     	  	  	  while (fin>>ws && getline(fin,objtext,',') && fin>>objid)	  {	    Object *newobj=new Object(objtext,objid);	    Objv.push_back(newobj);	 	 	  } // end end of line while	  fin.close();	std::string word;	int id;	int size=Objv.size();	for (int i=0; i<size; i++)	{	word=Objv->Getchar();	id=Objv->Getid();	cout << "Its word: " << word << " and its id: " << id << endl;	}	for(int i=0; i<size; i++)	{	 delete Objv;	}    char ch;	cin >> ch; //just a stop break before exiting console		return 0;}


This works and compiles fine... but I want a pointer to the Objv vector. I tried this below, but the word=Objv->Getchar() and Getid() break and want a "." instead.

vector <Object*> *Objv;Objv=new vector <Object*>;


Also, cin and cout will not work unless i #include <iostream>... but I thought iostream was included in fstream. What did I miss there? Even dropped the using namespace and tried std::cin..etc
Quote:
Original post by jwalsh
Quote:
I was thinking about using a more readible format, either XML or some pseudo class declarations, and build some kind of parser to instantiate objects for this. Or maybe instantiate my objects with DOM nodes which contain their starting values.


I am very much in favor of XML as a language to use for your data files. Its organized, easy to read, and is used in many standard and commercial engines for things such as Web Applications all the way to World of Warcraft Interface files.

How you parse/load the XML is up to you, but I can assure you, design such as this for the rooms, objects, and perhaps even NPC's will make a huge difference in your ability to create interesting rooms and objects. The easier it is to create the world, the more likely you are to do so. As well, the less times it takes a "world designer" to create your world, the more time they'd have to think up unique features.

Cheers!


Which are the most common used XML parsers? I'm guessing there must be a lot of open source XML parsers around, so I don't have to invent the wheel again.

Although writing an XML parser can be fun...

Advertisement
TinyXML is a fairly common choice.
Quote:
Original post by westond
I know what I want to do, but unfortunately I dont know how to convey it.


That much is normal. [smile]

Quote:
This works and compiles fine... but I want a pointer to the Objv vector.


See, the resistance that I perceive is in terms of "wanting" something that I can tell you (from my years of experience) is more complex and doesn't accomplish anything extra. :
Quote:
I tried this below, but the word=Objv[i]->Getchar() and Getid() break and want a "." instead.

*** Source Snippet Removed ***


Currently, you index into the vector, yielding an Object*; then you can use -> to dereference the Object* to get an Object, and select its Getchar() member function.

If you have a pointer to the vector, then 'Objv[i]' is no longer indexing into the vector, but instead operating upon the pointer - i.e. assuming you have an array of some kind (in particular, an array of vectors). That selects a vector, so you can't use -gt;Getchar() at that point - first off because that tries to do a dereference (and you don't have a pointer), and secondly because the *vector* doesn't have a Getchar(). Although the compiler is generally very smart about the code that it writes when it gets a correct program, it's usually very dumb about how it reports errors - it basically reads one "token" at a time and reports the error as soon as it knows there is one, using only the information it has so far (even if the very next token could give it a much better understanding of what you're really trying to do). That's why it tells you it wants a '.': the only thing it sees for the time being is a -> applied to a non-pointer.

What you would need to do is dereference the pointer first (to get the vector) and *then* index in. That looks like (*Objv)[i]->Getchar().

... You see? More complicated, and it won't work any better: the whole point of using the vector in the first place is to let *it* manage the dynamic allocation.

Quote:
Also, cin and cout will not work unless i #include <iostream>... but I thought iostream was included in fstream. What did I miss there? Even dropped the using namespace and tried std::cin..etc


It is not (necessarily - it could on someone else's implementation, because the person putting that implementation together found it made his/her life easier to do so, but you should not rely on that), because cin and cout aren't file streams - they're console streams. :)
Having finished the first project I am tempted to have a go at the second project however this seems to be a much more tricky project than I imagined.

I have to say reading through some of the replies concerning how to handle inventory items was quite interesting but I am not sure I can comprehend some of the more difficult concepts suggested like visitor patterns and using opaque pointers. Are these concepts covered in the book being used(I don't have the book in question). The way I thought about the inventory was to do something like adding:

vector<Weapon> wInventory;
vector<Armor> aInventory;
vector<Items> iInventory;

in the character class. I'm not sure if I should try attempting some of the ideas suggested or just use the above. It does seem crude in comparison to the various ideas suggested but much more comprehendable to me.

Anyway how are other people finding the project?
Being honest I'm finding it difficult just to start.
How about them apples?
DOUBLE POST FTW!!!!

Sorry about that ev1.
Advertisement

jwalsh has proly said it best, dont let what you dont know keep you from attempting these projects. Just do it the best way you know how... even if it isnt very elegant or proper coding. We are all learning here, and noone expects us to be on par with a coder that has a degree in CS and several years of experiance. We are bound to do it wrong despite our best efforts. The point of the projects is to make us think about a goal within the context of what we know. The more we use what we know, then the easier it is to pick up on those new and more difficult aspects of C++. Dont let anything hold you back... just go for it. And if anyone here says otherwise, they ought not be here.

Quote:
Original post by popcorn
Having finished the first project I am tempted to have a go at the second project however this seems to be a much more tricky project than I imagined.

I have to say reading through some of the replies concerning how to handle inventory items was quite interesting but I am not sure I can comprehend some of the more difficult concepts suggested like visitor patterns and using opaque pointers. Are these concepts covered in the book being used(I don't have the book in question). The way I thought about the inventory was to do something like adding:

vector<Weapon> wInventory;
vector<Armor> aInventory;
vector<Items> iInventory;

in the character class. I'm not sure if I should try attempting some of the ideas suggested or just use the above. It does seem crude in comparison to the various ideas suggested but much more comprehendable to me.

Anyway how are other people finding the project?
Being honest I'm finding it difficult just to start.


As far as starting the project. I would suggest putting pen to paper first. I designed the world on paper as far as each cell the player could interact with. This then showed me what direction the character could move when in anyone cell. This also lead me to create multiple levels/zones to the world in which the character could move. It also inspired me to come up with a structrue of how character, item, and the world would interact. By the time I was finished drawing out my world I had a pretty good idea of to attack the code. Ill scan my drawings here so you can see what I did.

Here is the link to a pdf. My paper was much larger than the scanner bed, but you get enough to get the idea clickie
Quote:
Original post by westond

jwalsh has proly said it best, dont let what you dont know keep you from attempting these projects. Just do it the best way you know how... even if it isnt very elegant or proper coding. We are all learning here, and noone expects us to be on par with a coder that has a degree in CS and several years of experiance. We are bound to do it wrong despite our best efforts. The point of the projects is to make us think about a goal within the context of what we know. The more we use what we know, then the easier it is to pick up on those new and more difficult aspects of C++. Dont let anything hold you back... just go for it. And if anyone here says otherwise, they ought not be here.


I realise that westond, I will try to continue regardless even if my code does look messy but I also want to try out some of the techniques suggested by some people - it is a project designed to help you learn more after all. I feel quite interested by the idea of handle classes and I would like to try to implement this for the items class.

I'm not quite sure I understand this yet though so I may resort to something simpler but I would like to ask if you create an item class and derive a weapon and armor class from it - how would you create the handle class for this - does the interface for the handle class have to provide all the member functions that the item class as well as the derived classes have?

How about them apples?
Quote:
Original post by popcorn
I'm not quite sure I understand this yet though so I may resort to something simpler but I would like to ask if you create an item class and derive a weapon and armor class from it - how would you create the handle class for this - does the interface for the handle class have to provide all the member functions that the item class as well as the derived classes have?


Only if you derive the handle class from the the weapon or armor class. Then yes it will include all the methods of the Item and Weapon/Armor classes. You can hide methods in the Weapon/Armor classes by declaring those methods private. Your Handle class would not see or be able to use those methods then. Same goes for the Item class, but those methods would not only be hidden from the Handle class but from the Weapon/Armor classes aswell.

I think that a handle class would be unecesary. Once you derive a weapon and armor class from an item class then you can treat those objects created as generic items that can be held in a container of <Items> to be found or carried as in a backpack. Then to invoke "use or handle" simply add the wanted item to a container of <Weapon> or <Armor> in a character class object. The Weapon and Armor Classes will have all of the methods defined in the base class <Item> available to them. Add the method of use to the Weapon and Armor Classes directly. That is, if the only time the weapon and armor mods will be available is during combat... that way all you have to do is call those methods in your combat resolving code when you need the stats to apply.

You can take this further... say you wanted to loot weapons and armor off of an enemy player. You can create a method that upon death the weapon and armor objects would be moved to an inspectable <Item> container in the enemy character object. Upon looting move that object to the player's <Item> container.

Things like potions that are usable would work the same way. They would have all the methods defined in the base Item class, but their methods of use would be defined directly in the Potions class. Just like Weapons and Armor, they could be stored in a container of <Item> to be used. To distinguish wether or not something is useable...a potion is usable out of the backpack, where Weapons and Armor have to be equiped, just place a bool isuseable=(true/false) in your base item class.
Im thinking of trying to compress my classes a bit by hashing some of my ID variables.

I have a set of variables that give an individual game cell a location in the game world...

int zone_id
int zone_y
int zone_x

each zone id can be broken down into a description of the area of the world its in as well as being the declaration of the array the cells are held in.

zone_id=1001 World zone 1 sub zone 1
zone_id=1101 World zone 1 town zone 1 subzone 1
etc...

the position is array1001<zone_y,zone_x>

what I would like to do is create a cell id that combines all this info
ex.
zone_id=1001
zone_y=2
zone_x=1

long cell_id=100121

but i need a way of breaking that up when I need zone_id, zone_y, and zone_x.

I've looked, but cant find anything that will just read the first 4 digits of cell_id and assign it to zone_id and the 5th digit to zone_y and so on. Im sure there is an easy way of doing this that doesnt involve character arrays and casting. Or am I mistaken?

This topic is closed to new replies.

Advertisement