#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "a5.h"
/*************
*PLAYER CLASS*
**************/
//private member function, used to set the object (instantiat),
//should be called all the constructors
void Player::Set(const char* PlayerName,const Card* CardsDealt,int Dealt){//start function Set
//cards dealt to the player
if(Dealt_ < 0)
Dealt_ = 0; //set to 0 if Dealt_ is less than 0, safe empty state
else
Dealt_ = Dealt; //set to Dealt_ if more than 0
//Players name
if(PlayerName_ != NULL){//start if
//allocate freestore memory for Player Name
PlayerName_ = new char[strlen(PlayerName) + 1];
strcpy(PlayerName_,PlayerName);
PlayerName_[strlen(PlayerName)] = '\0';
}//end if
//safe empty state
else{ //start else
PlayerName_ = new char[1];
strcpy(PlayerName_,"");
}//end else
//Array with cards initially dealt to the Player
if(CardsDealt_ != NULL){
CardsDealt_ = new Card[Dealt]; //allocate freestore memory for Cards Dealt
//start of for loop
for(int i=0; i<Dealt; i++){
CardsDealt_[i] = CardsDealt[i];
}//end of for loop
}
else
CardsDealt_ = NULL; //setting the pointer to NULL, ie no cards
}//end function Set
//defualt constructor, PlayerName = "unknown" and
//the Player initially holds no cards by calling
//the Set private member function
Player::Player(){
//calling Set function
Set("Unknown",0,0);
}
//3 argument constructor, takes in a char value,
//a Card value and an int and calls the Set private
//member function
Player::Player(const char* PlayerName,const Card* CardsDealt,int Dealt){
//calling Set function
Set(PlayerName,CardsDealt,Dealt);
}
//2 argument constructor, takes in a Card value and
//an int, in this case the value of PlayerName = "Unknown",
//calls the Set private member function
Player::Player(const Card* CardsDealt,int Dealt){
//calling Set function
Set("Unknown",CardsDealt,Dealt);
}
//copy constructor for Player class
Player::Player(const Player& source){
//calls Init
Init(source);
}
//assignment operator for Player
Player& Player:[IMG]http://www.3dbuzz.com/vbforum/images/smilies/redface.gif[/IMG]perator=(const Player& source){
//start of if statement
if(this != &source){
//delete PlayerName
if (PlayerName_)
delete [] PlayerName_;
//delete CardsDealt
if (CardsDealt_)
delete [] CardsDealt_;
//calls Init
Init(source);
}//end of if
return *this;
}
//called by copy constructor and assignment operator
void Player::Init(const Player& source){
//Player Name
PlayerName_ = new char[strlen(source.PlayerName_) + 1];
strcpy(PlayerName_,source.PlayerName_);
//Cards Dealt
CardsDealt_ = new Card[source.Dealt_];
//Dealt
Dealt_ = source.Dealt_;
//start of for loop
for(int i=0; i<source.Dealt_; i++){
CardsDealt_[i] = source.CardsDealt_[i];
}//end of for loop
}
//function recieves an array const Card cardsdealt[]
//(represents the cards dealt to the Player) and an
//int numcards (number of cards dealt). This function
//replaces any cards currently held by the player with
//the cards in the cardsdealt array
void Player:[IMG]http://www.3dbuzz.com/vbforum/images/smilies/biggrin.gif[/IMG]eal(const Card cardsdealt[],int numcards){
//deallocate Cards Dealt
if (CardsDealt_)
delete [] CardsDealt_;
//reallocate Cards Dealt
CardsDealt_ = new Card[numcards];
//start of for loop
for(int i=0; i<numcards; i++){
CardsDealt_[i] = cardsdealt[i];
}//end of for loop
}
//function recieves a reference to a Card, then finds
//lowest Card that the Player holds and then places the
//lowest Card's value into the Card refered to by c
void Player::GetLowest(Card& c) const{
//call Sort() funtion from a4.h to sort the cards from lowest to highest, putting the lowest card in CardsDealt_[0]
Sort(CardsDealt_,Dealt_);
//puts the lowest card from the CardsDealt_ array into c
c = CardsDealt_[0];
}
//query that returns the number of cards held
int Player::numcards() const{
return Dealt_;
}
//function copies held by Player into cardsheld array.
//If maxsize is less than the number of cards held by
//the Player, only copy maxsize cards. Function returns
//number of cards copied into the cardsheld array
int Player::GetCards(Card cardsheld[],int maxsize) const{
//integer containing the number of times the loop will go
int NumberOfTimes;
if(maxsize < Dealt_)
NumberOfTimes = maxsize;
else if(maxsize > Dealt_)
NumberOfTimes = Dealt_;
//delete cardsheld[]
if(cardsheld)
delete [] cardsheld;
//reallocate memory for cardsheld[]
cardsheld = new Card[NumberOfTimes];
//start of for loop
for(int i=0; i<NumberOfTimes ; i++){
cardsheld[i] = CardsDealt_[i]; //copying the cards from the Player into cardsheld
}//end of for loop
return NumberOfTimes;
}
//query returns the name of the Player
const char* Player::name() const{
return PlayerName_;
}
//function prints the card held by the Player.
//Cards must be printed in a SORTED manner from low to high
void Player::Print() const{
//sort cards from low to high
Sort(CardsDealt_,Dealt_);
//start of for loop
for(int i=0; i<Dealt_; i++){
cout << CardsDealt_[i].Suit() << ' '; //prints out the the suit values aka the letters held by player
}//end of for loop
cout << endl; //next line
//start of for loop
for(int i=0; i<Dealt_; i++){
cout << CardsDealt_[i].Face() << ' '; //prints out the face values aka the numbers held by player
}//end of for loop
cout << endl; //next line
}
//deallocate freestore memory, destructor
Player::~Player(){
//delete PlayerName
if (PlayerName_)
delete [] PlayerName_;
//delete CardsDealt
if (CardsDealt_)
delete [] CardsDealt_;
}