1

I have single game instance class in my study rock-scissors-paper game on C++. I want to create integer constant, which represent count of allowed signs in a game. For classic rsp game it is a 3(rock, scissors and paper), but there are some interesting rcs game extensions with additional signs and later I'm going to implement them and expand my dummy game.

How to implement same constant, following good coding style? Should I create special private constant game instant class member or declare this constant in namespace? Maybe it is better to create special class for game configuration and put this constant, sign types and other there?

Another question is how to implement game rules (rock > scissors, scissors > paper etc), which would be easy to extend.

vard
  • 2,142
  • 4
  • 30
  • 40
  • if it's going to vary how is it constant – Cheers and hth. - Alf Nov 30 '12 at 08:13
  • You should ask your second question in a separate question. –  Nov 30 '12 at 08:18
  • I'm going to use this constant to generate random number for PC turn (0-x) and check if player's input was correct. I want to make game expanding much more easier. It will be great, if to add several signs, I just need to change this constant, add rules for them and maybe changing game rules hint string. – vard Nov 30 '12 at 08:19

1 Answers1

4
class Game {

private:
  enum Sign {
    PAPER,
    SCISSORS,
    ROCK
  };
};

If you want a convenient way to get the number of signs:

class Game {

private:
  enum Sign {
    PAPER,
    SCISSORS,
    ROCK,
    NUM_SIGNS
  };
};

NUM_SIGNS will equal 3 in this case.

  • 3
    If you list the enumerations (which always start from 0 unless otherwise specified) in the order PAPER, SCISSORS and ROCK, the order is there already and can be used by the game engine. – otto Nov 30 '12 at 08:20
  • @OttoHarju very nice. i'll switch that up –  Nov 30 '12 at 08:21
  • Does sizeof(Sign) return signs count in this case? Does it good to use sizeof() in C++? – vard Nov 30 '12 at 08:21
  • 1
    @Sancho: enums start from 0, so NUM_SIGNS is 3, which is the number of relevant enums. – stefaanv Nov 30 '12 at 08:42
  • @vard No, `sizeof(Sign)` will return the amount of memory needed to store the data, [possibly the same as `sizeof(int)`](http://stackoverflow.com/questions/1113855/is-the-sizeofenum-sizeofint-always) – Peter Wood Nov 30 '12 at 08:53