fork(1) download
  1. #include <iostream>
  2. #include <bitset>
  3.  
  4. class State{
  5. public:
  6. //Observer
  7. std::string ToString() const { return state_.to_string();};
  8. //Getters
  9. bool MoveUp() const{ return state_[0];};
  10. bool MoveDown() const{ return state_[1];};
  11. bool MoveLeft() const{ return state_[2];};
  12. bool MoveRight() const{ return state_[3];};
  13. bool Still() const{ return state_[4];};
  14. bool Jump() const{ return state_[5];};
  15. //Setters
  16. void MoveUp(bool on) {state_[0] = on;}
  17. void MoveDown(bool on) {state_[1] = on;}
  18. void MoveLeft(bool on) {state_[2] = on;}
  19. void MoveRight(bool on) {state_[3] = on;}
  20. void Still(bool on) {state_[4] = on;}
  21. void Jump(bool on) {state_[5] = on;}
  22. private:
  23. std::bitset<6> state_;
  24. };
  25.  
  26.  
  27. int main() {
  28. State s;
  29. auto report = [&s](std::string const& msg){
  30. std::cout<<msg<<" "<<s.ToString()<<std::endl;
  31. };
  32. report("initial value");
  33. s.MoveUp(true);
  34. report("move up set");
  35. s.MoveDown(true);
  36. report("move down set");
  37. s.MoveLeft(true);
  38. report("move left set");
  39. s.MoveRight(true);
  40. report("move right set");
  41. s.Still(true);
  42. report("still set");
  43. s.Jump(true);
  44. report("jump set");
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
initial value 000000
move up set 000001
move down set 000011
move left set 000111
move right set 001111
still set 011111
jump set 111111