SHARE
TWEET

Player.cpp

a guest Feb 25th, 2020 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Player.h"
  2. #include <cstring>
  3. //inicializirasht spisuk, po-burzo
  4. CPlayer::CPlayer():score(0),lives(2),state(1),nick(new char[6]){
  5.     //tui kato ne move v spisuka
  6.     strcpy_s(nick, 5, "Gosho");
  7. }
  8. //trqbva da se dobavqt vsichki chlen danni
  9. CPlayer::CPlayer(char * nick): score(0), lives(2), state(1), nick(new char [strlen(nick)+1] ){
  10.     //zashtoto ima kolizii izpolzvame this->
  11.     strcpy_s(this->nick, strlen(nick), nick);
  12. }
  13. CPlayer::CPlayer(unsigned score, unsigned lives, bool state, char* nick): nick(new char[strlen(nick) + 1]) {
  14.     //nqma da slagame drugite v spisuka, za da moje da napravim proverka dali sa validni danni
  15.     this->score = (score >= 0) ? score : 0;
  16.     this->lives = (lives >= 0) ? lives : 2;
  17.     this->state = (state ==false && lives>0) ?true : state;
  18.     strcpy_s(this->nick, strlen(nick), nick);
  19. }
  20. CPlayer::CPlayer(bool, char*):score(0), lives(2), nick(new char[strlen(nick) + 1])
  21. {
  22.     this->state = (state == false && lives > 0) ? true : state;
  23.     strcpy_s(this->nick, strlen(nick), nick);
  24.    
  25. }
  26. //zapochvame cpy construktor
  27. //CPlayer ivan; ima defaulten construktor
  28. //cplayer joe, prisvoqva stoinosti na ivan(defaultnatite)
  29. CPlayer::CPlayer(const CPlayer &rhs): score(rhs.score),lives(rhs.lives),state(rhs.state), nick(new char[strlen(rhs.nick) + 1]) {
  30.     strcpy_s(this->nick, strlen(rhs.nick),rhs. nick);
  31. }
  32. //ne e konstruktor nqma spisuk
  33. //neka imame CPlayer ivan,joe,gosho;
  34. //joe=ivan=gosho
  35. //dokato ima funkciqta sushtestvuva i obekta, ako nqmashe & v nachaloto shteshe da e tmp
  36. CPlayer& CPlayer::operator=(const CPlayer &rhs){
  37.     if (this != &rhs) {
  38.         if (nick != nullptr) {
  39.             delete[] nick;
  40.             nick = nullptr;
  41.         }
  42.     }
  43.     score = rhs.score;
  44.     lives = rhs.lives;
  45.     state = rhs.state;
  46.     nick=new char[strlen(rhs.nick) + 1];
  47.     strcpy_s(this->nick, strlen(rhs.nick), rhs.nick);
  48.     //funkciq e za tova trqbva da vurne rezultat
  49.     //vrushtame obekt za tova e s *
  50.     return *this;
  51. }
  52.  
  53. CPlayer::~CPlayer(){}
  54.  
  55. int CPlayer::setScore(unsigned score){
  56.     this->score = (score > 0) ? score : 0;
  57.     return 0;
  58. }
  59. int CPlayer::setLives(unsigned lives){
  60.     this->lives = (lives > 0) ? lives : 0;
  61.     return 0;
  62. }
  63. //nqma proverka za state
  64. int CPlayer::setState(bool state){
  65.     this->state = state;
  66.     return 0;
  67. }
  68. int CPlayer::setNick(char * nick){
  69.         if (this->nick != nullptr) {
  70.             delete[]this-> nick;
  71.             this->nick = nullptr;
  72.         }
  73.         this->nick = new char[strlen(nick) + 1];
  74.         strcpy_s(this->nick, strlen(nick), nick);
  75.     return 0;
  76. }
  77.  
  78. unsigned CPlayer::getScore() const{
  79.     return score;
  80. }
  81. unsigned CPlayer::getLives() const{
  82.     return lives;
  83.  
  84. }
  85. bool CPlayer::getState() const{
  86.     return state;
  87.  
  88. }
  89. char * CPlayer::getNick() const {
  90.     return nick;
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top