lab.cpp/Chess/dep/Player.cpp

91 lines
2.0 KiB
C++
Raw Normal View History

// #include "Player.h"
2016-03-08 20:06:36 +00:00
2016-03-08 18:23:35 +00:00
// CONSTRUCTEUR
Player::Player(bool first) : _first(first){
setfont(GREEN, BOLD);
cout << " [+] Player " << (_first?1:2);
2016-03-08 18:23:35 +00:00
setfont();
cout << endl;
}
// GETTERS
bool Player::owns(Piece& p) const{
for( int i = 0 ; i < _pieces.size() ; i++ )
if( _pieces.at(i) == &p ) return true;
return false;
}
Piece* Player::at(const int x, const int y){
for( int i = 0 ; i < _pieces.size() ; i++ )
if( _pieces[i]->getx() == x && _pieces[i]->gety() == y )
return _pieces[i];
return NULL;
}
// SETTERS
void Player::addPiece(Piece& p){
// On definit a la piece si c'est P1 ou P2
p.setPlayer(_first);
2016-03-08 18:23:35 +00:00
_pieces.push_back( &p );
/* DEBUG */
setfont(BLUE, ITALIC);
cout << " [+] linked to Player " << (_first?1:2);
2016-03-08 18:23:35 +00:00
setfont();
cout << endl;
}
void Player::remPiece(Piece& p){
int index = -1;
// On cherche la piece
for( int i = 0 ; i < _pieces.size() ; i++ )
if( _pieces.at(i) == &p )
index = i;
// On ajoute au cimetiere
if( index > -1 ){
_cimetery.push_back( &p );
_pieces.erase(_pieces.begin() + index );
}
/* DEBUG */
setfont(BLUE, ITALIC);
cout << " [-] " << p << " to cimetery of Player " << (_first?1:2);
2016-03-08 18:23:35 +00:00
setfont();
cout << endl;
}
// INITIALISATION DES PIECES DE BASE
void Player::initPieces(){
int y1 = _first ? 7 : 0; // ligne forte
int y2 = _first ? 6 : 1; // ligne de pions
2016-03-08 18:23:35 +00:00
// Premier ligne
this->addPiece( PieceFactory::create(TOUR, 0, y1) );
this->addPiece( PieceFactory::create(CAVALIER, 1, y1) );
this->addPiece( PieceFactory::create(FOU, 2, y1) );
this->addPiece( PieceFactory::create(REINE, 3, y1) );
this->addPiece( PieceFactory::create(ROI, 4, y1) );
this->addPiece( PieceFactory::create(FOU, 5, y1) );
this->addPiece( PieceFactory::create(CAVALIER, 6, y1) );
this->addPiece( PieceFactory::create(TOUR, 7, y1) );
for( int i = 0 ; i < 8 ; i++ )
this->addPiece( PieceFactory::create(PION, i, y2) );
}