Skip to content

Commit 010d1d8

Browse files
committed
Added operator== to Board and GamePiece, and serialization test
1 parent 40a3f9e commit 010d1d8

File tree

13 files changed

+82
-1
lines changed

13 files changed

+82
-1
lines changed

include/City.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class City : public CornerPiece {
1313
City& operator=(City&) = delete;
1414

1515
virtual void accept(GameVisitor&);
16+
virtual bool operator==(const GamePiece& piece) const;
1617
};
1718

1819
#endif

include/GameBoard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class GameBoard {
4242
void init_resources();
4343

4444
void accept(GameVisitor& visitor);
45+
46+
bool operator==(const GameBoard& other) const;
4547
};
4648

4749
#endif

include/GamePiece.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class GamePiece {
2626
Coordinate location;
2727

2828
virtual void accept(GameVisitor&) = 0;
29+
virtual bool operator==(const GamePiece& other) const = 0;
30+
2931
Coordinate getLocation() const;
3032
};
3133

@@ -45,6 +47,8 @@ class ResourceTile : public GamePiece {
4547
virtual ~ResourceTile();
4648

4749
virtual void accept(GameVisitor&);
50+
virtual bool operator==(const GamePiece& other) const;
51+
4852
resourceType getType() const;
4953
int getDiceValue() const;
5054
};

include/Player.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Player {
6161
void setWool(int resource);
6262

6363
std::string getName() const;
64+
65+
bool operator==(const Player& player) const;
6466
};
6567

6668
#endif

include/Road.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Road {
2222
Coordinate getStart() const;
2323
Coordinate getEnd() const;
2424
Player& getOwner();
25+
const Player& getOwner() const;
26+
27+
bool operator==(const Road&) const;
2528
};
2629

2730
#endif

include/Settlement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Settlement : public CornerPiece {
1313
Settlement& operator=(Settlement&) = delete;
1414

1515
virtual void accept(GameVisitor&);
16+
virtual bool operator==(const GamePiece&) const;
1617
};
1718

1819
#endif

src/City.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ City::~City() {
1313
void City::accept(GameVisitor& visitor) {
1414
visitor.visit(*this);
1515
}
16+
17+
bool City::operator==(const GamePiece& p) const {
18+
return false;
19+
}

src/GameBoard.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,39 @@ void GameBoard::accept(GameVisitor& visitor) {
144144
visitor.visit(*this);
145145
}
146146

147+
bool GameBoard::operator==(const GameBoard& other) const {
148+
if(corners.size() != other.corners.size()) {
149+
return false;
150+
}
151+
for(auto& it : corners) {
152+
auto otherIt = other.corners.find(it.first);
153+
if(otherIt == other.corners.end()) {
154+
return false; // This location isn't in the other array
155+
}
156+
if(!(*(it.second) == *(otherIt->second))) {
157+
return false;
158+
}
159+
}
160+
for(auto& it : resources) {
161+
auto otherIt = other.resources.find(it.first);
162+
if(otherIt == other.resources.end()) {
163+
return false; // This location isn't in the other array
164+
}
165+
if(!(*(it.second) == *(otherIt->second))) {
166+
return false;
167+
}
168+
}
169+
for(auto& it : roads) {
170+
bool hasIt = false;
171+
for(auto& otherIt : other.roads) {
172+
if(*it == *otherIt) {
173+
hasIt = true;
174+
}
175+
}
176+
if(hasIt == false) {
177+
return false;
178+
}
179+
}
180+
return true;
181+
}
182+

src/GamePiece.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ int ResourceTile::getDiceValue() const {
4848
return value;
4949
}
5050

51+
bool ResourceTile::operator==(const GamePiece& other) const {
52+
const ResourceTile* tile = dynamic_cast<const ResourceTile*>(&other);
53+
if(tile == nullptr) {
54+
return false;
55+
}
56+
57+
return getType() == tile->getType() && getDiceValue() == tile->getDiceValue();
58+
}
59+
5160
//pay resource cards to owners of this tile
5261
/*
5362
void ResourceTile::Payout() {

src/Player.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ std::string Player::getName() const
120120
{
121121
return name;
122122
}
123+
124+
bool Player::operator==(const Player& player) const {
125+
return getName() == player.getName();
126+
}

0 commit comments

Comments
 (0)