fork(2) download
  1. #include <cassert>
  2. #include <cstddef>
  3. #include <bitset>
  4.  
  5. using weapon_id = unsigned long long;
  6.  
  7. // Available weapons - reduced for brevity
  8. // Ideally, adding a new weapon here wouldn't require editing anything below.
  9. static const weapon_id WEAPON_HARSH_LANGUAGE = 0;
  10. static const weapon_id WEAPON_SHARP_STICK = 1;
  11. static const weapon_id WEAPON_KNIFE = 2;
  12. static const weapon_id WEAPON_NUKE = 3;
  13. static const std::size_t WEAPON_COUNT = 4; // Could potentially be > 64
  14.  
  15. using weapon_set = std::bitset< WEAPON_COUNT >;
  16.  
  17.  
  18. // Select the next available weapon.
  19. // If only one weapon is available, return that weapon.
  20. // If the last weapon is currently selected then cycle back around to the first.
  21. weapon_id next_weapon(const weapon_set& weapons, const weapon_id current_weapon)
  22. {
  23. assert(weapons.any());
  24.  
  25. if (weapons.count() == 1)
  26. {
  27. return current_weapon;
  28. }
  29.  
  30. auto new_weapon = current_weapon;
  31. // Insert magic here.
  32. return new_weapon;
  33. }
  34.  
  35.  
  36. // Select the previous available weapon.
  37. // If only one weapon is available, return that weapon.
  38. // If the first weapon is currently selected then cycle back around to the last.
  39. weapon_id prev_weapon(const weapon_set& weapons, const weapon_id current_weapon)
  40. {
  41. assert(weapons.any());
  42.  
  43. if (weapons.count() == 1)
  44. {
  45. return current_weapon;
  46. }
  47.  
  48. auto new_weapon = current_weapon;
  49. // Insert magic here
  50. return new_weapon;
  51. }
  52.  
  53. // Test cases I can think of. Have I missed any?
  54. int main()
  55. {
  56. // Test 1 - One weapon
  57. {
  58. weapon_set weapons;
  59. weapons.set(WEAPON_HARSH_LANGUAGE);
  60. assert(next_weapon(weapons, WEAPON_HARSH_LANGUAGE) == WEAPON_HARSH_LANGUAGE);
  61. assert(prev_weapon(weapons, WEAPON_HARSH_LANGUAGE) == WEAPON_HARSH_LANGUAGE);
  62. }
  63.  
  64. // Test 2 - Two 'sequential' weapons
  65. {
  66. weapon_set weapons;
  67. weapons.set(WEAPON_SHARP_STICK);
  68. weapons.set(WEAPON_KNIFE);
  69. assert(next_weapon(weapons, WEAPON_SHARP_STICK) == WEAPON_KNIFE);
  70. assert(prev_weapon(weapons, WEAPON_SHARP_STICK) == WEAPON_KNIFE);
  71. assert(next_weapon(weapons, WEAPON_KNIFE) == WEAPON_SHARP_STICK);
  72. assert(prev_weapon(weapons, WEAPON_KNIFE) == WEAPON_SHARP_STICK);
  73. }
  74.  
  75. // Test 3 - Two 'non sequential' weapons
  76. {
  77. weapon_set weapons;
  78. weapons.set(WEAPON_HARSH_LANGUAGE);
  79. weapons.set(WEAPON_KNIFE);
  80. assert(next_weapon(weapons, WEAPON_HARSH_LANGUAGE) == WEAPON_KNIFE);
  81. assert(prev_weapon(weapons, WEAPON_HARSH_LANGUAGE) == WEAPON_KNIFE);
  82. assert(next_weapon(weapons, WEAPON_KNIFE) == WEAPON_HARSH_LANGUAGE);
  83. assert(prev_weapon(weapons, WEAPON_KNIFE) == WEAPON_HARSH_LANGUAGE);
  84. }
  85.  
  86. // Test 4 - All weapons
  87. {
  88. weapon_set weapons;
  89. weapons.set(WEAPON_HARSH_LANGUAGE);
  90. weapons.set(WEAPON_SHARP_STICK);
  91. weapons.set(WEAPON_KNIFE);
  92. weapons.set(WEAPON_NUKE);
  93. assert(next_weapon(weapons, WEAPON_HARSH_LANGUAGE) == WEAPON_SHARP_STICK);
  94. assert(prev_weapon(weapons, WEAPON_HARSH_LANGUAGE) == WEAPON_NUKE);
  95. assert(next_weapon(weapons, WEAPON_NUKE) == WEAPON_HARSH_LANGUAGE);
  96. assert(prev_weapon(weapons, WEAPON_NUKE) == WEAPON_KNIFE);
  97. }
  98.  
  99. return 0;
  100. }
Runtime error #stdin #stdout #stderr 0s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: prog.cpp:69: int main(): Assertion `next_weapon(weapons, WEAPON_SHARP_STICK) == WEAPON_KNIFE' failed.