
include
include
using namespace std;
class Location {
private: string x;
string y;
public: Location(string x, string y) {
// TODO Auto-generated constructor stub
this->x = x;
this->y = y;
}
string getX() {
return x;
}
string getY() {
return y;
}
void setX(string x) {
this->x = x;
}
void setY(string y) {
this->y = y;
}
};
class Piece {
public: virtual string getColor()=0;
void locate(Location *lo) {
cout<<this->getColor() << " " <<lo->getX() << "," <<lo->getY()<<endl;
}
};
class WhitePiece:public Piece{
public: string getColor() {
// TODO Auto-generated method stub
return "白棋";
}
};
class BlackPiece :public Piece{
public: string getColor() {
// TODO Auto-generated method stub
return "黑棋";
}
};
class PieceFatory {
private: //PieceFatory *instance;
Piece *wp=new WhitePiece() ;
Piece *bp=new BlackPiece() ;
public:
PieceFatory *getInstance() {
// cout << " dsv1 " << endl;
return new PieceFatory();
}
Piece *getPiece(char *color) {if (strcmp("白棋",color)==0) {//cout << 1;return wp;}else {return bp;}
}
};
int main() {
Piece *b1, *b2, *w1, *w2;
//cout << " dsv " << endl;
PieceFatory *pf = new PieceFatory();
//cout << " dsv1 " << endl;
b1 = pf->getPiece("黑棋");
//cout << " dsv1 " << endl;
b2 = pf->getPiece("黑棋");
//cout << " dsv1 " << endl;
cout<<"判断两颗黑棋是否相同:" <<(b1 == b2)<<endl;
w1 = pf->getPiece("白棋");w2 = pf->getPiece("白棋");cout<<"判断两颗白棋是否相同:" <<(w1 == w2)<<endl;b1->locate(new Location("1", "2"));b2->locate(new Location("3", "2"));//cout << 1;w1->locate(new Location("10", "2"));w2->locate(new Location("1", "21"));