Advertisement
Guest User

brando

a guest
Nov 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. /* PIC10A Battle with Voldemort
  2. * Coded by Weiqi Chu
  3. */
  4. #include"player.h"
  5.  
  6. player::player()
  7. {
  8. name = "MyPlayer";
  9. stamina = 0;
  10. n_battles = 0;
  11. win = 0;
  12. }
  13.  
  14. // Write the other constructors and methods of class player.
  15.  
  16. _____________________________________
  17.  
  18. /* PIC10A Battle with Voldemort
  19. * Coded by Weiqi Chu
  20. */
  21.  
  22. #ifndef __PLAYER_H__
  23. #define __PLAYER_H__
  24.  
  25. #include<string>
  26. #include<iostream>
  27. #include<assert.h>
  28. using namespace std;
  29.  
  30. class player
  31. {
  32. public:
  33. // constructors with different parameter lists
  34. player();
  35. player(string name);
  36. player(string name, int stamina, int n_battles);
  37.  
  38. // retrive private data members
  39. string get_name() const;
  40. int get_stamina() const;
  41. int get_n_battles() const;
  42.  
  43. // reset stamina/n_battles by passing a parameter
  44. void set_stamina(int i);
  45. void set_n_battles(int n);
  46.  
  47. // add 1 to the private member win
  48. void add_win();
  49.  
  50. /* this function mimics the process of having one battle and returns true if the player wins the battle
  51. * See homework instructions for details.
  52. */
  53. bool battle(player& x);
  54.  
  55. /* determin if the object wins more battles than player x
  56. * @params: player x
  57. * @return true if the object wins more or equal battles than the player x
  58. */
  59. bool operator>(player x) const;
  60.  
  61. private:
  62. string name;
  63. int stamina;
  64. int n_battles;
  65. int win;
  66. };
  67.  
  68.  
  69. #endif
  70.  
  71.  
  72.  
  73. __________________________
  74.  
  75. /* PIC10A Battle with Voldemort
  76. * Coded by Weiqi Chu
  77. */
  78. #include<iostream>
  79. #include<ctime>
  80. #include<stdlib.h>
  81. #include"player.h"
  82.  
  83. using namespace std;
  84.  
  85. int main()
  86. {
  87. srand(time(0));
  88. // set total stamina and number of battles
  89. int total_stamina = 100;
  90. int n_battles = 3;
  91.  
  92. // initialize two players: Hermione and Voldemort
  93. // User will play as Hermione and computer will play as Voldemort
  94. player Hermione("Hermione Granger", total_stamina, n_battles);
  95. player Voldemort("Lord Voldemort");
  96. Voldemort.set_stamina(Hermione.get_stamina());
  97. Voldemort.set_n_battles(Hermione.get_n_battles());
  98.  
  99. for (int i=n_battles; i>0; i--)
  100. {
  101. if (Hermione.battle(Voldemort))
  102. Hermione.add_win();
  103. else
  104. Voldemort.add_win();
  105. }
  106. if ( Hermione > Voldemort )
  107. cout << "Gryffindor rocks!\n";
  108. else if ( Voldemort > Hermione)
  109. cout << "You-know-who is back!\n";
  110. else
  111. cout << "Tie for now!\n";
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement