Skip to content

Commit 29b0a0c

Browse files
Kyle GrageKyle Grage
authored andcommitted
Small cornerpiece tweaks and completion of cornerpiece testing suite
1 parent 3b2b1ed commit 29b0a0c

File tree

9 files changed

+118
-37
lines changed

9 files changed

+118
-37
lines changed

include/City.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class City : public CornerPiece {
2121

2222
int getResourceModifier();
2323
int getVictoryPoints();
24-
25-
bool operator==(const City&) const;
2624
};
2725

2826
#endif

include/CornerPiece.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class CornerPiece : public GamePiece {
2020
Player& getOwner();
2121
const Player& getOwner() const;
2222

23+
void accept(GameVisitor& visitor)
24+
2325
virtual int getResourceModifier();
2426

2527
virtual int getVictoryPoints();
2628

27-
virtual bool operator==(const CornerPiece&) const;
29+
virtual bool operator==(const GamePiece&) const;
2830
};
2931

3032
#endif

include/GameVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class GameVisitor {
3030
virtual void visit(ResourceTile&) = 0;
3131
virtual void visit(DevelopmentCard&) = 0;
3232
virtual void visit(Wonder&) = 0;
33+
virtual void visit(CornerPiece&) = 0;
3334
};
3435

3536
#endif

include/Settlement.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class Settlement : public CornerPiece {
2020
virtual bool operator==(const GamePiece&) const;
2121
int getResourceModifier();
2222
int getVictoryPoints();
23-
24-
bool operator==(const Settlement&) const;
2523
};
2624

2725
#endif

include/Wonder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class Wonder : public CornerPiece {
2626
int getResourceModifier();
2727
int getVictoryPoints();
2828

29-
bool operator==(const Wonder&) const;
3029
};
3130

3231
#endif /* WONDER_H_ */

src/City.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ void City::accept(GameVisitor& visitor) {
2828

2929
/**
3030
* Test for equality with another game piece.
31-
* @param p The piece to test with
31+
* @param other The piece to test with
3232
* @return True if this object is equal to the other, false if not.
3333
*/
34-
bool City::operator==(const GamePiece& p) const {
35-
return false; //TODO: Actually compare.
34+
bool City::operator==(const GamePiece& other) const {
35+
auto city = dynamic_cast<const City*>(&other);
36+
if(city) {
37+
return getOwner().getName() == city->getOwner().getName() && getLocation() == city->getLocation();
38+
} else {
39+
return false;
40+
}
3641
}
3742

3843
/**
@@ -59,15 +64,4 @@ City::City(CornerPiece& sett) : CornerPiece(sett.getBoard(), sett.getLocation(),
5964

6065
}
6166

62-
/**
63-
* Determines if a piece is the same
64-
* @return boolean
65-
*/
66-
bool City::operator==(const City& other) const {
67-
auto city = dynamic_cast<const City*>(&other);
68-
if(city) {
69-
return getOwner().getName() == city->getOwner().getName() && getLocation() == city->getLocation();
70-
} else {
71-
return false;
72-
}
73-
}
67+

src/CornerPiece.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ CornerPiece::~CornerPiece() {
1717

1818
}
1919

20+
/**
21+
* Visitor double-dispatch method.
22+
* @param visitor The visiting instance.
23+
*/
24+
void CornerPiece::accept(GameVisitor& visitor) {
25+
visitor.visit(*this);
26+
}
27+
2028
/**
2129
* Getter for the owner of the piece.
2230
* @return The piece's owner.
@@ -53,7 +61,7 @@ int CornerPiece::getResourceModifier() {
5361
* Determines if a piece is the same
5462
* @return boolean
5563
*/
56-
bool CornerPiece::operator==(const CornerPiece& other) const {
64+
bool CornerPiece::operator==(const GamePiece& other) const {
5765
auto cp = dynamic_cast<const CornerPiece*>(&other);
5866
if(cp) {
5967
return getOwner().getName() == cp->getOwner().getName() && getLocation() == cp->getLocation();

src/Wonder.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ void Wonder::accept(GameVisitor& visitor) {
4141
* @param p The piece to test with
4242
* @return True if this object is equal to the other, false if not.
4343
*/
44-
bool Wonder::operator==(const GamePiece& p) const {
45-
return false; //TODO: Actually compare.
44+
bool Wonder::operator==(const GamePiece& other) const {
45+
auto won = dynamic_cast<const Wonder*>(&other);
46+
if(won) {
47+
return getOwner().getName() == won->getOwner().getName() && getLocation() == won->getLocation();
48+
} else {
49+
return false;
50+
}
4651
}
4752

4853
/**
@@ -69,16 +74,5 @@ Wonder::Wonder(CornerPiece& sett) : CornerPiece(sett.getBoard(), sett.getLocatio
6974

7075
}
7176

72-
/**
73-
* Determines if a piece is the same
74-
* @return boolean
75-
*/
76-
bool City::operator==(const City& other) const {
77-
auto won = dynamic_cast<const Wonder*>(&other);
78-
if(won) {
79-
return getOwner().getName() == won->getOwner().getName() && getLocation() == won->getLocation();
80-
} else {
81-
return false;
82-
}
83-
}
77+
8478

tests/test_cornerpiece.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ TEST(CornerPiece_constructor){
1818
Player& test_player = board.getPlayer(0);
1919

2020
CornerPiece test_cp(&board, loc, test_player);
21+
CHECK_EQUAL(loc, test_cp.getLocation());
22+
CHECK_EQUAL(&board, test_cp.getBoard());
23+
CHECK_EQUAL(&test_player, test_cp.getOwner());
24+
CHECK_EQUAL(0, test_cp.getVictoryPoints());
2125
}
2226

2327
TEST(Settlement_constructor){
@@ -26,6 +30,10 @@ TEST(Settlement_constructor){
2630
Player& test_player = board.getPlayer(0);
2731

2832
Settlement test_cp(&board, loc, test_player);
33+
CHECK_EQUAL(loc, test_cp.getLocation());
34+
CHECK_EQUAL(&board, test_cp.getBoard());
35+
CHECK_EQUAL(&test_player, test_cp.getOwner());
36+
CHECK_EQUAL(1, test_cp.getVictoryPoints());
2937
}
3038

3139
TEST(City_constructor){
@@ -34,6 +42,10 @@ TEST(City_constructor){
3442
Player& test_player = board.getPlayer(0);
3543

3644
City test_cp(&board, loc, test_player);
45+
CHECK_EQUAL(loc, test_cp.getLocation());
46+
CHECK_EQUAL(&board, test_cp.getBoard());
47+
CHECK_EQUAL(&test_player, test_cp.getOwner());
48+
CHECK_EQUAL(2, test_cp.getVictoryPoints());
3749
}
3850

3951
TEST(Wonder_constructor){
@@ -42,48 +54,123 @@ TEST(Wonder_constructor){
4254
Player& test_player = board.getPlayer(0);
4355

4456
Wonder test_cp(&board, loc, test_player);
57+
CHECK_EQUAL(loc, test_cp.getLocation());
58+
CHECK_EQUAL(&board, test_cp.getBoard());
59+
CHECK_EQUAL(&test_player, test_cp.getOwner());
60+
CHECK_EQUAL(10, test_cp.getVictoryPoints());
4561
}
4662

4763
TEST(City_upgrade_constructor){
64+
Coordinate loc = Coordinate(0,0);
65+
GameBoard board({"test board"});
66+
Player& test_player = board.getPlayer(0);
4867

68+
Settlement intermediate_cp(&board, loc, test_player);
69+
City test_cp(&intermediate_cp);
70+
CHECK_EQUAL(loc, test_cp.getLocation());
71+
CHECK_EQUAL(&board, test_cp.getBoard());
72+
CHECK_EQUAL(&test_player, test_cp.getOwner());
73+
CHECK_EQUAL(2, test_cp.getVictoryPoints());
4974
}
5075

51-
TEST(Wonder_upgrade_constructor){
76+
TEST(Wonder_upgrade_settlement_constructor){
77+
Coordinate loc = Coordinate(0,0);
78+
GameBoard board({"test board"});
79+
Player& test_player = board.getPlayer(0);
80+
81+
Settlement intermediate_cp(&board, loc, test_player);
82+
Wonder test_cp(&intermediate_cp);
83+
CHECK_EQUAL(loc, test_cp.getLocation());
84+
CHECK_EQUAL(&board, test_cp.getBoard());
85+
CHECK_EQUAL(&test_player, test_cp.getOwner());
86+
CHECK_EQUAL(10, test_cp.getVictoryPoints());
87+
}
88+
89+
TEST(Wonder_upgrade_city_constructor){
90+
Coordinate loc = Coordinate(0,0);
91+
GameBoard board({"test board"});
92+
Player& test_player = board.getPlayer(0);
5293

94+
City intermediate_cp(&board, loc, test_player);
95+
Wonder test_cp(&intermediate_cp);
96+
CHECK_EQUAL(loc, test_cp.getLocation());
97+
CHECK_EQUAL(&board, test_cp.getBoard());
98+
CHECK_EQUAL(&test_player, test_cp.getOwner());
99+
CHECK_EQUAL(10, test_cp.getVictoryPoints());
53100
}
54101

55102
//TEST RESOURCE MODIFIERS
56103
TEST(CornerPiece_Resource_Mod){
104+
Coordinate loc = Coordinate(0,0);
105+
GameBoard board({"test board"});
106+
Player& test_player = board.getPlayer(0);
57107

108+
CornerPiece test_cp(&board, loc, test_player);
109+
CHECK_EQUAL(0, test_cp.getResourceModifier());
58110
}
59111

60112
TEST(Settlement_Resource_Mod){
113+
Coordinate loc = Coordinate(0,0);
114+
GameBoard board({"test board"});
115+
Player& test_player = board.getPlayer(0);
61116

117+
Settlement test_cp(&board, loc, test_player);
118+
CHECK_EQUAL(1, test_cp.getResourceModifier());
62119
}
63120

64121
TEST(City_Resource_Mod){
122+
Coordinate loc = Coordinate(0,0);
123+
GameBoard board({"test board"});
124+
Player& test_player = board.getPlayer(0);
65125

126+
City test_cp(&board, loc, test_player);
127+
CHECK_EQUAL(2, test_cp.getResourceModifier());
66128
}
67129

68130
TEST(Wonder_Resource_Mod){
131+
Coordinate loc = Coordinate(0,0);
132+
GameBoard board({"test board"});
133+
Player& test_player = board.getPlayer(0);
69134

135+
Wonder test_cp(&board, loc, test_player);
136+
CHECK_EQUAL(10, test_cp.getResourceModifier());
70137
}
71138

72139
//TEST VICTORY POINTS
73140
TEST(CornerPiece_Victory_Pts){
141+
Coordinate loc = Coordinate(0,0);
142+
GameBoard board({"test board"});
143+
Player& test_player = board.getPlayer(0);
74144

145+
CornerPiece test_cp(&board, loc, test_player);
146+
CHECK_EQUAL(0, test_cp.getVictoryPoints());
75147
}
76148

77149
TEST(Settlement_Victory_Pts){
150+
Coordinate loc = Coordinate(0,0);
151+
GameBoard board({"test board"});
152+
Player& test_player = board.getPlayer(0);
78153

154+
Settlement test_cp(&board, loc, test_player);
155+
CHECK_EQUAL(1, test_cp.getVictoryPoints());
79156
}
80157

81158
TEST(City_Victory_Pts){
159+
Coordinate loc = Coordinate(0,0);
160+
GameBoard board({"test board"});
161+
Player& test_player = board.getPlayer(0);
82162

163+
City test_cp(&board, loc, test_player);
164+
CHECK_EQUAL(2, test_cp.getVictoryPoints());
83165
}
84166

85167
TEST(Wonder_Victory_Pts){
168+
Coordinate loc = Coordinate(0,0);
169+
GameBoard board({"test board"});
170+
Player& test_player = board.getPlayer(0);
86171

172+
Wonder test_cp(&board, loc, test_player);
173+
CHECK_EQUAL(10, test_cp.getVictoryPoints());
87174
}
88175

89176

0 commit comments

Comments
 (0)