Reputation: 11
I am creating a game when the player lands on "F", the enumeration in the player class has to change between good
and bad
.
For example, if the player already has good
and lands on "F", it will change to bad
, and if players lands back on 'F' it will be good
again and so on.
player.h
class Player {
public:
int health = 100;
int weapon = 1;
int lvl = 0;
Player(bool hero) {
if (hero) {
health;
weapon;
} else {
health = 1;
}
}
enum Alignment { good, bad };
void attack(Enemy &e);
friend class Enemy;
};
main.cpp
if (gameBoard[positionX][positionY] == gameBoard[0][4]) {
cout << "\nWell Of Reflection\n" << endl;
cout << "You increases your XP by 2" << endl;
p.lvl += 2;
cout << "Total XP: " << p.lvl << endl;
Alignment = // HERE
}
Upvotes: 0
Views: 1020
Reputation: 1065
player.h
class Player {
public:
int health = 100;
int weapon = 1;
int lvl = 0;
Player(bool hero) {
if (!hero) {
health = 1;
}
}
enum Alignment { good, bad };
Alignment al = Alignment::good; // creates the new field in the class to store the aligment
void attack(Enemy &e);
friend class Enemy;
};
main.cpp
if (gameBoard[positionX][positionY] == gameBoard[0][4]) {
cout << "\nWell Of Reflection\n" << endl;
cout << "You increases your XP by 2" << endl;
p.lvl += 2;
cout << "Total XP: " << p.lvl << endl;
// to switch value do something like that
if (p.al == Aligment::good) {
p.al = Aligment::bad;
} else {
p.al = Aligment::good;
}
}
But your approach is very basic. It's not recommended to change fields in a class directly and only by setters and getters. To store of some state you can use a flag variable and binary operations (for example byte flags = 0b00000000 and flags = flags & 0b00000010 to save some state in the second bit of the flags variable (if it have two states)).
Upvotes: -2
Reputation: 29041
enum Alignment { good, bad };
Declares a type called Alignment
, not a variable. Your line
Alignment = // HERE
would attempt to assign a value to a type, which makes no sense.
You'd need something like this:
enum Alignment { good, bad };
Alignment myAlignment = good;
I really prefer to use scoped enums like this:
enum class Alignment { good, bad };
Aligmnent myAlignment = Alignment::good;
They're functionally equivalent, but the latter gives the compiler some hints that can catch coding errors at compile time.
On a side note: note that in your post, the word Alignment
is displayed in that blue/green color reserved for types.
Applying this to your class definition:
class Player {
public:
int health = 100;
int weapon = 1;
int lvl = 0;
Player(bool hero) {
if (hero) {
// health; THESE LINES DO NOTHING BUT GENERATE A WARNING.
// weapon;
} else {
health = 1;
}
}
// Alignment is a nested type that can be referred to as Player::Alignment.
enum class Alignment { good, bad };
Alignment playerAlignment = Alignment::good;
void attack(Enemy &e);
friend class Enemy;
};
And later on...
if (gameBoard[positionX][positionY] == gameBoard[0][4]) {
cout << "\nWell Of Reflection\n" << endl;
cout << "You increases your XP by 2" << endl;
p.lvl += 2;
cout << "Total XP: " << p.lvl << endl;
p.playerAlignment = Player::Alignment::bad;
}
Or if you want to display the player's alignment:
std::string to_string(Player::Alignment alignment)
{
switch(alignment)
{
case Player::Alignment::good:
return "good";
case Player::Alignment::bad:
return "bad";
}
return "Unknown alignment";
}
And elsewhere when you want to use that:
cout << "Alignment: " << to_string(p.playerAlignment) << endl;
Upvotes: 3