Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PIC10A Battle with Voldemort
- * Coded by Weiqi Chu
- */
- #include"player.h"
- player::player()
- {
- name = "MyPlayer";
- stamina = 0;
- n_battles = 0;
- win = 0;
- }
- // Write the other constructors and methods of class player.
- _____________________________________
- /* PIC10A Battle with Voldemort
- * Coded by Weiqi Chu
- */
- #ifndef __PLAYER_H__
- #define __PLAYER_H__
- #include<string>
- #include<iostream>
- #include<assert.h>
- using namespace std;
- class player
- {
- public:
- // constructors with different parameter lists
- player();
- player(string name);
- player(string name, int stamina, int n_battles);
- // retrive private data members
- string get_name() const;
- int get_stamina() const;
- int get_n_battles() const;
- // reset stamina/n_battles by passing a parameter
- void set_stamina(int i);
- void set_n_battles(int n);
- // add 1 to the private member win
- void add_win();
- /* this function mimics the process of having one battle and returns true if the player wins the battle
- * See homework instructions for details.
- */
- bool battle(player& x);
- /* determin if the object wins more battles than player x
- * @params: player x
- * @return true if the object wins more or equal battles than the player x
- */
- bool operator>(player x) const;
- private:
- string name;
- int stamina;
- int n_battles;
- int win;
- };
- #endif
- __________________________
- /* PIC10A Battle with Voldemort
- * Coded by Weiqi Chu
- */
- #include<iostream>
- #include<ctime>
- #include<stdlib.h>
- #include"player.h"
- using namespace std;
- int main()
- {
- srand(time(0));
- // set total stamina and number of battles
- int total_stamina = 100;
- int n_battles = 3;
- // initialize two players: Hermione and Voldemort
- // User will play as Hermione and computer will play as Voldemort
- player Hermione("Hermione Granger", total_stamina, n_battles);
- player Voldemort("Lord Voldemort");
- Voldemort.set_stamina(Hermione.get_stamina());
- Voldemort.set_n_battles(Hermione.get_n_battles());
- for (int i=n_battles; i>0; i--)
- {
- if (Hermione.battle(Voldemort))
- Hermione.add_win();
- else
- Voldemort.add_win();
- }
- if ( Hermione > Voldemort )
- cout << "Gryffindor rocks!\n";
- else if ( Voldemort > Hermione)
- cout << "You-know-who is back!\n";
- else
- cout << "Tie for now!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement