原网址:https://blog.csdn.net/CoderAldrich/article/details/83183271
做出如下修改:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef struct PointTag__ {
int x_;
int y_;
PointTag__(){}
PointTag__(int a, int b) {
x_ = a;
y_ = b;
}
bool operator <(const PointTag__& other) const {
if (x_ < other.x_) {
return true;
} else if (x_ == other.x_) {
return y_ < other.y_;
}
return false;
}
} Point;
typedef enum PieceColorTag__ {
BLACK,
WHITE
} PIECECOLOR;
// 存放坐标点和棋子颜色的类,提供接口可以设置或者获取坐标点和获取颜色
class CPiece {
public:
CPiece(PIECECOLOR color) : color_(color){}
PIECECOLOR GetColor() {
return color_;
}
void SetPoint(Point point) {
point_ = point;
}
Point GetPoint() {
return point_;
}
protected:
PIECECOLOR color_;
Point point_;
};
class CGomoku : public CPiece {
public:
CGomoku(PIECECOLOR color) : CPiece(color){}
};
// 用vec存贮多个CPiece
class CPieceFactory {
public:
// 判断颜色(白色或者黑色)是否已经创建,来避免重复创建CPiece
CPiece *GetPiece(PIECECOLOR color) {
CPiece *piece = NULL;
if (piece_vec_.empty()) {
piece = new CGomoku(color);
piece_vec_.push_back(piece);
} else {
for (vector<CPiece *>::iterator it = piece_vec_.begin(); \
it != piece_vec_.end(); ++it) {
if ((*it)->GetColor() == color) {
piece = *it;//<------------------------->避免重复创建
break;
}
}
if (piece == NULL) {
piece = new CGomoku(color);
piece_vec_.push_back(piece);
}
}
return piece;
}
~CPieceFactory() {
for (vector<CPiece *>::iterator it = piece_vec_.begin(); \
it != piece_vec_.end(); ++it) {
if (*it != NULL) {
delete *it;
}
}
}
private:
vector<CPiece *> piece_vec_;
};
/*
将 CPieceFactory里面判断好的piece(避免重复制造)以及对应的坐标点,
存到一个map里面,以便于全部展现
*/
class ChessBoard {
public:
void Draw(CPiece *piece) {
if (piece->GetColor() == WHITE) {
cout << "Draw a White" << " at (" << piece->GetPoint().x_ << ","\
<< piece->GetPoint().y_ << ")" << endl;
} else if (piece->GetColor() == BLACK) {
cout << "Draw a Black" << " at (" << piece->GetPoint().x_ << ","\
<< piece->GetPoint().y_ << ")" << endl;
}
point_piece_map_.insert(pair<Point, CPiece *>(piece->GetPoint(), piece));
}
void ShowAllPieces() {
for (map<Point, CPiece *>::iterator it = point_piece_map_.begin();\
it != point_piece_map_.end(); ++it) {
if (it->second->GetColor() == WHITE) {
cout << "(" << it->first.x_ << "," << it->first.y_ << \
")has a white chece ." << endl;
} else if (it->second->GetColor() == BLACK) {
cout << "(" << it->first.x_ << "," << it->first.y_ << \
")has a black chece ." << endl;
}
}
}
private:
map<Point, CPiece *> point_piece_map_;
};
int main() {
CPieceFactory *piece_factory = new CPieceFactory();
ChessBoard *chess_board = new ChessBoard();
CPiece *piece = piece_factory->GetPiece(WHITE);// 判断是否已经创建了白色棋子
piece->SetPoint(Point(2, 3));//结构体不需要new对象
chess_board->Draw(piece);
piece = piece_factory->GetPiece(BLACK);
piece->SetPoint(Point(4, 5));
chess_board->Draw(piece);
piece = piece_factory->GetPiece(WHITE);
piece->SetPoint(Point(2, 4));
chess_board->Draw(piece);
piece = piece_factory->GetPiece(BLACK);
piece->SetPoint(Point(3, 5));
chess_board->Draw(piece);
// show all chess
cout<<"Show all cheses"<<endl;
chess_board->ShowAllPieces();
if (piece_factory != NULL) {
delete piece_factory;
piece_factory = NULL;
}
if (chess_board != NULL) {
delete chess_board;
chess_board = NULL;
}
}
运行结果:
Draw a White at (2,3)
Draw a Black at (4,5)
Draw a White at (2,4)
Draw a Black at (3,5)
Show all cheses
(2,3)has a white chece .
(2,4)has a white chece .
(3,5)has a black chece .
(4,5)has a black chece .