Proper object usage in a simple game setting [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Assuming that I wanted to make a simple dos-based game and I wanted to create some mobs, I've come up with the following rough object that I would need, and it looks something like this:
class createMob{
private:
int healthMax;
int healthCurrent;
int manaMax;
int manaCurrent;
int experiencePoints;
public:
void setHealth();
int getHealth();
void setCurrentHealth();
int getCurrentHealth();
void setMaxMana();
int getMaxMana();
void setCurrentMana();
int getCurrentMana();
int getExperience();
//etc etc functions truncated for space
};
My question is how do I use this? Assume that I create a simple constructor to take in a hp / mp / experience for a mob called "green slime" (final fantasy theme going on here). My basic goal is to create a monster with x / y /z attributes and set them based on what happens in combat... What would be the easiest way to do this and then clean up efficiently afterwards?

If you have two mobs in a fight, the fight is going to take longer than one turn, so one fight turn is going to need the following global function:
void fight(Mob &m1, Mob &m2, int fightstep);
Now this function can change properties of both mobs. Now if mobs are actually doing some actions to choose how the fight is going, you'll need something different:
void fight(Mob &m1, Mob &m2,
std::vector<Action> vec1, std::vector<Action> vec2,
int fightstep);
with the action being something like this:
enum Action { EDoNothing, EBash, ECastSpellXXXX ... };
Now the fight functions can use the interface of Mob class to do the changes.

Related

C pointers and objects as part of different classes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have relatively less experience with C++ coding, and I'm working on a project that requires me to use classes, pointers and objects in C++. I'm facing some basic problems while practising coding.
This is what part of my .cpp file looks like:
A::A(int x, int y):CD()
{
//some code
}
B::B(int z):CD()
{
//some code
}
If I need to pass values from A to B, how would I be able to do it in such a scenario? Could anyone please clarify and help me.
Edit: According to the code I have, CD is also defined as a class like A and B.
Here A is a class and B is another class right,
Let's take a simple addition example
A(int x,int y)
{
b=new B(x+y);//let be a reference of class B
//this will call B's constructor
}
B(int z)
{
this.z=z;//lets's assume that z is a variable in B class
}

should a class manage its instantiated objects? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
lets assume I have a number instance of a class vehicle instantiated. if the vehicle object wants to query other existing vehicles for some reason ( for example finding the nearest vehicle), should the class manages the instantiated objects through static members and methods as in the code below? is this a good design pattern? is this a common standard? is there any pitfall if i take this approach?
#include <vector>
#include<algorithm>
struct Location {
double x, y, z;
Location(double xx, double yy, double zz) : x(xx), y(yy), z(zz) {}
};
class Vehicle {
private:
Location l;
public:
Vehicle (Location ll) : l(ll) {
vv.push_back(this);
}
static std::vector<Vehicle*> vv;
~Vehicle() {
std::vector<Vehicle*>::iterator it;
// removing the Vehicle object from the list of existing vehicles
for (it = vv.begin(); it != vv.end(); it++){
if (*it == this) {
vv.erase(std::remove(vv.begin(), vv.end(), this), vv.end());
}
}
}
Vehicle& find_nearest_vehicle () {
// code to iterate through list of existing vehicles and find the nearest vehicle 
}
};
static std::vector<Vehicle*> vv;
Put it into any of the typical OO scenarios used as examples:
Does an animal know about every other animal?
Does a car know about every other car?
Does a color know about every other color?
What you are asking is really opinion based, but I'm sure most people would say "no". You use some kind of manager class to control the instances.
In your case I'd have a Vehicle which knows it's location and a VehicleManager which knows about all the Vehicles. If you want to know what color a Vehicle is you ask the Vehicle. If you want to know where all the Red Vehicles are - you ask the VehicleManager.
Your solution has a combined Vehicle/VehicleManager which relies on a static collection of vehicles so you can only ever have one set. If you use two classes as I've described you can have multiple sets. e.g. Vehicles of different companies or trucks vs cars etc - sure there are other ways to do this as well, but your solution locks you in. Using 2 classes is much more flexible.
So to answer your last comment: do you think it's ok or is it a terrible design? - It is terrible design.

How to specify a type parameter using <> to create a template class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code:
class Grid {
public:
vector<vector<int> > grid;
int width;
int height;
Grid (int width, int height) width(width), height(height) {
...
}
};
It makes a class called Grid which is a 2D array of integers. The problem is, however, that at the moment it can only be integers, but I want it so it's kind of like the std::vector class in which you can use <> parentheses to choose the type that it will store. My question is, how can I use these in my class in order to replace all the current ints with any other class.
Also, you might say to look it up but I tried and I could't find anything, probably because I didn't know what to search so if anyone could give me an idea on what this is even called then that'd be helpful too.
It seems like you just want to template your Grid class:
template <typename T>
class Grid {
public:
vector<vector<T> > grid;
// initialize the vector with the correct dimensions:
Grid (int width, int height)
: grid(width, vector<double>(height)) {}
};
and then instantiate:
Grid<double> g(x, y);
This will create a Grid object where T is double

Is there anyway in c++ to make a function of a class who's objects each do something different for that function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Just some background:
I am making a monopoly game and now I have to implement the actions of each space such as GO, properties (upgrading), chance, community chest, jail, etc.
I've considered making a different class for each space (but obviously that would be very time consuming). I believe there is also a way do to it with inheritance and pure virtual functions. Any way you guys can think that would make this a simpler process?
Thanks!
There are only a few different types of spaces:
properties
railroads
chance / community chest
utilities
other single ones like go, jail, parking, tax
For example you could have a Property class where each instance of the class has a different name/colour/price. You wouldn't have to make 22 different classes, just have 22 instances of the same class with different names.
Note that having class instances that represent spaces you can land on is only one way to implement a game like that. I'm pretty sure I wouldn't choose that option.
There are two ways you can make a function do different things given an object:
Differentiate the behavior based on the data of the object.
You could capture the differences in the various spaces using data.
enum SpaceType
{
GO, CHANCE, COMMUNITY_CHEST, // etc...
};
class Space
{
public:
void foo()
{
switch (spaceType)
{
case GO:
// DO stuff for GO
break;
case CHANCE:
// DO stuff for CHANCE
break;
// etc..
}
}
private:
SpaceType spaceType;
}
Differentiate the behavior based on the type of an object.
class Space
{
public:
virtual void foo() = 0;
};
class GoSpace : public Space
{
public:
virtual void foo()
{
// Do stuff for GO
}
};
class ChanceSpace : public Space
{
public:
virtual void foo()
{
// Do stuff for CHANCE
}
};
// Similarly for other classes.
Pick your method. Personally, I would pick the second method because the logic for each different type is put into their own functions, without the complications of what other types do.

Differentiating between data class and logic class in agile [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've been reading an agile book about clean coding, mainly in Java and c#. Considering the concept of differentiating between a data class and a logic/object class. I have the following situation in c++, where I can't decide which variant is a clean code. I have a Plane class with some attributes and these attributes change when only one of them changes, so
First Method
class Plane3D {
public:
BBox bbox;
float l,w,h;
void setbbox(BBox bbox) {
this->bbox = bbox;
recalculateLWH();
}
void setLWH(float l, float w, float h) {
//set here
recalculateBBOX();
}
};
This makes sense to me, since to the user he is just calling one method and doesn't have to care about internal work of the class. but this is a violation for a data class, that it contains logic
Now the
Second method
class Plane3D {
public:
BBox bbox;
float l,w,h;
void setbbox(BBox bbox) {
this->bbox = bbox;
}
void setLWH(float l, float w, float h) {
//set here LWH
}
};
int main() {
BBox bbox;//init here
Plane plane;
plane.setBBox(bbox);
recalculateLWH(plane);
}
Now the second method actually separates the data class from the implementation but it increases the responsibilities of the class user and forces him to make an extra call. To my understanding the second method is the correct one from an agile POV, but I find the first method more logical to use.
I'd like to know which of the two methods would make more sense for you guys to undertsand and use
Regards
In this case I think you should prefer first method to second.
Your method calls should transform object from one correct state to another correct state. Using the second method after your setBBox(bbox); call your object moves to some incorrect state indeed.
But 'logic class way' can however take place in another situation.
Consider you should move plane:) from hangar to landing strip. Now it will be naturally to introduce new class named Tractor and use it like tractor.move(plane)