0

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
}
Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25

2 Answers2

3
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;
3Dave
  • 28,657
  • 18
  • 88
  • 151
  • Thanks, 3Dave :). And one more question how can I display the current enum for example, "Player alignment is: good/bad" using cout. thank you again – Fifa Casa Mar 29 '20 at 16:44
  • @FifaCasa Added that to my answer. Hope it helps. – 3Dave Mar 29 '20 at 16:50
-2

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)).

Yury Finchenko
  • 1,035
  • 13
  • 19
  • We have `bool` for true or false conditions.Enums are _designed_ to provide types for 'flags', use them. Trying to modify bits to do the same thing is uneccasary and will be a readability nightmare. – Object object Mar 29 '20 at 20:00
  • I would trust an AI to write better code than me, in fact in a manner of speaking an AI writes better code than me all the time, ever heard of compiler optimisations? Programming languages are meant to bridge the gap between human readable and machine readable, if you dont like that go write binary. – Object object Mar 31 '20 at 19:19