Advertisement
Topik0

Untitled

Apr 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. //Critter Caretaker
  2. //Simulates caring for a virtual pet
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class Critter
  9. {
  10. public:
  11. Critter(int hunger = 0, int boredom = 0);
  12. void Talk();
  13. void Eat(int food = 4);
  14. void Play(int fun = 4);
  15.  
  16. private:
  17. int m_Hunger;
  18. int m_Boredom;
  19.  
  20. int GetMood() const;
  21. void PassTime(int time = 1);
  22.  
  23. };
  24.  
  25. Critter::Critter(int hunger, int boredom) :
  26. m_Hunger(hunger),
  27. m_Boredom(boredom)
  28. {}
  29.  
  30. inline int Critter::GetMood() const
  31. {
  32. return (m_Hunger + m_Boredom);
  33. }
  34.  
  35. void Critter::PassTime(int time)
  36. {
  37. m_Hunger += time;
  38. m_Boredom += time;
  39. }
  40.  
  41. void Critter::Talk()
  42. {
  43. cout << "I'm a critter and I feel ";
  44.  
  45. int mood = GetMood();
  46. if (mood > 15)
  47. {
  48. cout << "mad.\n";
  49. }
  50. else if (mood > 10)
  51. {
  52. cout << "frustrated.\n";
  53. }
  54. else if (mood > 5)
  55. {
  56. cout << "okay.\n";
  57. }
  58. else
  59. {
  60. cout << "happy.\n";
  61. }
  62.  
  63. PassTime();
  64. }
  65.  
  66. void Critter::Eat(int food)
  67. {
  68. cout << "Brruppp.\n";
  69.  
  70. m_Hunger -= food;
  71. if (m_Hunger < 0)
  72. {
  73. m_Hunger = 0;
  74. }
  75.  
  76. PassTime();
  77. }
  78.  
  79. void Critter::Play(int fun)
  80. {
  81. cout << "Wheee!\n";
  82.  
  83. m_Boredom -= fun;
  84. if (m_Boredom < 0)
  85. {
  86. m_Boredom = 0;
  87. }
  88.  
  89. PassTime();
  90. }
  91.  
  92. int main()
  93. {
  94. Critter crit;
  95.  
  96. int choice = 1; //start the critter off talking
  97. while (choice != 0)
  98. {
  99. cout << "\nCritter Caretaker\n\n";
  100. cout << "0 - Quit\n";
  101. cout << "1 - Listen to your critter\n";
  102. cout << "2 - Feed your critter\n";
  103. cout << "3 - Play with your critter\n";
  104. cout << "4 - View the hunger and boredum levels of your critter\n\n";
  105.  
  106. cout << "Choice: ";
  107. cin >> choice;
  108.  
  109. switch (choice)
  110. {
  111. case 0:
  112. cout << "Good-bye.\n";
  113. break;
  114. case 1:
  115. crit.Talk();
  116. break;
  117. case 2:
  118. crit.Eat();
  119. break;
  120. case 3:
  121. crit.Play();
  122. break;
  123. case 4:
  124.  
  125. default:
  126. cout << "\nSorry, but " << choice << " isn't a valid choice.\n";
  127. }
  128. }
  129.  
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement