c++ - Returning an integer to determine which switch statement to show -


i'm creating board game (stratego) in c++ , wondering if considered poor practice return integer class method in order determine case in switch statement show user.

example: in stratego, can't attack board pieces part of own army have message "you cannot attack own army" when user tries so.

same thing if movement performed result in player jumping off board, moving many spaces, etc.

each of these invalid movements has it's own unique message, avoid printing them class.cpp file, player's moves validated, have class.cpp file returning integer switch statement in main() called from. recommended way handle how messages called?

class test { public:     test()     {      }     int validate_move(int valid)     {         if (valid > 0 && valid < 5)         {             return 1;         }         else if (valid > 5)         {             return 2;         }     } };  int main() {     int entry;     std::cout << "enter move: ";     std::cin >> entry;      test obj;      switch (obj.validate_move(entry))     {     case 1:         std::cout << "move valid" << std::endl;     case 2:         std::cout << "move invalid" << std::endl;     default:         std::cout << "error occured" << std::endl;     }      return 0; } 

there's nothing wrong technique. if want more explicit, make enum

class test { public:     test() = default;      enum evalidity {eerror, evalid, einvalid};      evalidity validate_move(int valid)     {         if (valid > 0 && valid < 5)         {             return evalid;         }         else if (valid > 5)         {             return einvalid;         }         else         {             return eerror;         }     } };  int main() {     int entry;     std::cout << "enter move: ";     std::cin >> entry;      test obj;      switch (obj.validate_move(entry))     {     case test::evalid:         std::cout << "move valid" << std::endl;         break;     case test::einvalid:         std::cout << "move invalid" << std::endl;         break;     case test::eerror:         std::cout << "error occured" << std::endl;         break;     default:         assert(false);     }      return 0; } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -