Skip to content

Commit dc428ba

Browse files
committed
Merge pull request #73 from Databean/player_refactor
Player refactor
2 parents 2413d36 + 1ef8a75 commit dc428ba

File tree

5 files changed

+43
-60
lines changed

5 files changed

+43
-60
lines changed

include/Player.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class Player {
7373

7474
std::tuple<float, float, float> getColor() const;
7575

76-
int getVictoryPointsWithoutCards();
77-
int getVictoryPointCards();
78-
7976
int getDevCardsInHand();
8077

8178
bool buyCard(std::unique_ptr<DevelopmentCard> &card);

src/GameBoard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ bool GameBoard::hasWinner() {
12191219
* @return reference to the winner if there is one, null otherwise
12201220
*/
12211221
Player& GameBoard::getWinner() const {
1222-
if (winner != -1 && winner < players.size())
1222+
if (winner != -1 && winner < (int)players.size())
12231223
return *players[winner];
12241224

12251225
return *players[0];

src/Player.cpp

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,13 @@ bool Player::canBuyRoad(){
140140

141141
/**
142142
* Subtracts the cost of a road from a player's resources if they have enough
143-
* @return true if the resources were subtracted, false otherwise
143+
* @return true if the resources were subtracted, false if the player had insufficient resources
144144
*/
145145
bool Player::buyRoad(){
146146
if(canBuyRoad()){
147147
addMultiple(-1,-1,0,0,0);
148148
return true;
149149
}
150-
//insufficient funds
151150
return false;
152151
}
153152

@@ -160,49 +159,52 @@ bool Player::canBuySettlement(){
160159
}
161160

162161
/**
163-
* Returns the number of knight cards this player has played
162+
* @return The number of knight cards this player has played
164163
*/
165164
int Player::getArmySize() const{
166165
return armySize;
167166
}
168167

169168
/**
170-
* Returns true if this player has the largest army, false otherwise
169+
* @return True if this player has the largest army, false otherwise
171170
*/
172171
bool Player::hasLargestArmy() const{
173172
return largestArmy;
174173
}
175174

176175
/**
177176
* Sets whether or not this player has the largest army
177+
* @param newVal The value of the largest army in the game
178178
*/
179179
void Player::setLargestArmy(bool newVal){
180180
largestArmy = newVal;
181181
}
182182

183183
/**
184-
* Returns the size of this player's longest road
184+
* @return The size of this player's longest road
185185
*/
186186
int Player::getLongestRoadSize() const{
187187
return longestRoadSize;
188188
}
189189

190190
/**
191-
* Returns true if this player has the longet road, false otherwise
191+
* @return True if this player has the longet road, false otherwise
192192
*/
193193
bool Player::hasLongestRoad() const{
194194
return longestRoad;
195195
}
196196

197197
/**
198198
* Sets whether or not this player has the longest road
199+
* @param newVal The value of the longest road on the board
199200
*/
200201
void Player::setLongestRoad(bool newVal){
201202
longestRoad = newVal;
202203
}
203204

204205
/**
205206
* Sets how long this player's longest road is
207+
* @param newVal The value of this player's longest road
206208
*/
207209
void Player::setLongestRoadSize(int newVal){
208210
longestRoadSize = newVal;
@@ -234,7 +236,7 @@ bool Player::canBuyCity(){
234236

235237
/**
236238
* Subtracts the cost of a city from a player's resources if they have enough
237-
* @return true if the resources were subtracted, false otherwise
239+
* @return True if the resources were subtracted, false otherwise
238240
*/
239241
bool Player::buyCity(){
240242
if(canBuyCity()){
@@ -246,15 +248,15 @@ bool Player::buyCity(){
246248

247249
/**
248250
* Determine if the player has enough resources to buy a wonder.
249-
* @return True if the player has enough resources to buy a wonder, false otherwise
251+
* @return True if the player has enough resources to buy a wonder, false if the player has insufficient resources
250252
*/
251253
bool Player::canBuyWonder(){
252254
return getWood() >= 5 && getBrick() >= 5 && getWheat() >= 5 && getWool() >= 5 && getOre() >= 5;
253255
}
254256

255257
/**
256258
* Subtracts the cost of a wonder from a player's resources if they have enough
257-
* @return true if the resources were subtracted, false otherwise
259+
* @return True if the resources were subtracted, false if the player had insufficient resources
258260
*/
259261
bool Player::buyWonder(){
260262
if(canBuyWonder()){
@@ -266,15 +268,15 @@ bool Player::buyWonder(){
266268

267269
/**
268270
* Determine if the player has enough resources to buy a card.
269-
* @return True if the player has enough resources to buy a card, false otherwise
271+
* @return True if the player has enough resources to buy a card, false if the player has insufficient resources
270272
*/
271273
bool Player::canBuyCard(){
272274
return getWheat() >= 1 && getWool() >= 1 && getOre() >= 1;
273275
}
274276

275277
/**
276278
* Subtracts the cost of a card from a player's resources if they have enough
277-
* @return true if the resources were subtracted, false otherwise
279+
* @return True if the resources were subtracted, false if they player has insufficient resources
278280
*/
279281
bool Player::buyCard(){
280282
if(canBuyCard()){
@@ -288,6 +290,7 @@ bool Player::buyCard(){
288290

289291
/**
290292
* Updates the player's victory points to the amount of points they have.
293+
* @todo Think about merging this with getVictoryPoints()
291294
*/
292295
void Player::updateVictoryPoints()
293296
{
@@ -308,17 +311,7 @@ void Player::updateVictoryPoints()
308311
}
309312

310313
/**
311-
* The number of victory points a player has, not counting victory point cards.
312-
* @return Victory points sans cards.
313-
*/
314-
int Player::getVictoryPointsWithoutCards()
315-
{
316-
updateVictoryPoints();
317-
return victoryPoints - developmentCards[VICTORYPOINT];
318-
}
319-
320-
/**
321-
* The number of victory points a player has.
314+
* @return The number of victory points a player has.
322315
*/
323316
int Player::getVictoryPoints()
324317
{
@@ -327,19 +320,10 @@ int Player::getVictoryPoints()
327320
}
328321

329322

330-
/**
331-
* The number of victory points the player has from victory point cards.
332-
* @return Victory points from cards.
333-
*/
334-
int Player::getVictoryPointCards()
335-
{
336-
return developmentCards[VICTORYPOINT];
337-
}
338-
339-
340323
/**
341324
* Acquire a development card.
342325
* @param card An owning pointer to the card the player acquired.
326+
* @return True if the player successfully bought a card, or false if the player had insufficient resources
343327
*/
344328
bool Player::buyCard(std::unique_ptr<DevelopmentCard>& card)
345329
{
@@ -461,7 +445,7 @@ void Player::setGenralModifier()
461445

462446
/**
463447
* Plays a victory point card if it exists. This means, the Player will 'consume' the card to gain a baseVictoryPoint
464-
* @ return true if the card was played, false otherwise
448+
* @return True if the card was played, false otherwise
465449
*/
466450
bool Player::playVictoryCard(){
467451
if(developmentCards[VICTORYPOINT] > 0){
@@ -474,7 +458,7 @@ bool Player::playVictoryCard(){
474458

475459
/**
476460
* Plays a knight card if it exists. This means the player will move the Knight and gain 1 random resource from a player around the robber's new position
477-
* @ param Coordinate of the robber's new position and Player& of the opponent to rob
461+
* @param Coordinate of the robber's new position and Player& of the opponent to rob
478462
* @return true if the card was used, false otherwise
479463
*/
480464
bool Player::playKnight(Coordinate location, Player& opponent){
@@ -495,8 +479,8 @@ bool Player::playKnight(Coordinate location, Player& opponent){
495479

496480
/**
497481
* Plays a year of plenty card if it exists. This means the player will gain 2 of 1 resource of their choice
498-
* @ param the resource the player will recieve
499-
* @ returns true if the card was played, false otherwise
482+
* @param the resource the player will recieve
483+
* @return true if the card was played, false otherwise
500484
*/
501485
bool Player::playYearOfPlenty(int resourceType){
502486
if(resourceType >= 5)
@@ -513,8 +497,8 @@ bool Player::playYearOfPlenty(int resourceType){
513497

514498
/**
515499
* Plays a monopoly card if it exists. This means the player will steal all of 1 resource type from all the other players
516-
* @ param the resource type the player wants to steal
517-
* @ return true if the card was played, false otherwise
500+
* @param The resource type the player wants to steal
501+
* @return True if the card was played, false otherwise
518502
*/
519503
bool Player::playMonopoly(int resourceType){
520504
if (resourceType >= 5)
@@ -533,8 +517,8 @@ bool Player::playMonopoly(int resourceType){
533517

534518
/**
535519
* Plays a road building card if it exists. This means the player will place two roads of their choosing for free
536-
* @ param the start and end Coordinates of the two roads the Player wants to place
537-
* @ return true if the card was played
520+
* @param the start and end Coordinates of the two roads the Player wants to place
521+
* @return true if the card was played
538522
*/
539523
bool Player::playRoadBuilding(Coordinate start1, Coordinate end1, Coordinate start2, Coordinate end2){
540524
if(developmentCards[ROADBUILDING] > 0){
@@ -562,31 +546,37 @@ void Player::setStartingValues(){
562546
developmentCards[ROADBUILDING] = 1;
563547
}
564548

565-
549+
/*
550+
* @todo Refactor this and the following methods
551+
*/
566552
int Player::getDevelopmentCards(int card_type) const{
567553
return developmentCards[card_type];
568554
}
569555

570556
int Player::getVictoryCards() const{
571557
return developmentCards[VICTORYPOINT];
572558
}
559+
573560
int Player::getKnightCards() const{
574561
return developmentCards[KNIGHT];
575562
}
563+
576564
int Player::getYearOfPlentyCards() const{
577565
return developmentCards[YEAROFPLENTY];
578566
}
567+
579568
int Player::getMonopolyCards() const{
580569
return developmentCards[MONOPOLY];
581570
}
571+
582572
int Player::getRoadBuildingCards() const{
583573
return developmentCards[ROADBUILDING];
584574
}
585575

586576
/**
587577
* The player gives all their resources of a specific resource type. Meant to compliment the Monopoly card
588-
* @ param the resource type to give
589-
* @ return the number of resources given
578+
* @param the resource type to give
579+
* @return the number of resources given
590580
*/
591581
int Player::giveAllResources(int resourceType){
592582
int resource_count = resources[resourceType];
@@ -663,12 +653,12 @@ bool Player::makeBankTrade(std::array<int, 5> offer, std::array<int, 5> demand)
663653
}
664654

665655
/**
666-
* picks any one resource at random for robber to steal
667-
* @return type of resource to steal
656+
* Picks any one resource at random for robber to steal
657+
* @return Type of resource to steal
658+
* @todo Make this shit actually random
668659
*/
669660
int Player::getRandomResource()
670661
{
671-
//int total = getWood() + getBrick() + getOre() + getWheat() + getWool();
672662
int randomNo = 0;
673663

674664
if(getWood()!=0 && randomNo <= getWood())
@@ -715,7 +705,7 @@ int Player::getResource(int resourceType) const {
715705

716706
/**
717707
* Determine if the player has a valid (nonnegative) set of resources.
718-
* @return If the player's resources are valid.
708+
* @return True if the player has a non-zero number of resources, or false otherwise
719709
*/
720710

721711
bool Player::checkResources(int resourceList[5])
@@ -776,7 +766,9 @@ int Player::getWool() const
776766
}
777767

778768

779-
769+
/*
770+
*@todo Refactor these add* methods
771+
*/
780772

781773
void Player::addWood(int resource)
782774
{
@@ -842,7 +834,6 @@ void Player::addWool(int resource)
842834
* Adds (or subtracts) the amount of resources a player has
843835
* Param order: wood, brick, ore, wheat, wool
844836
* @param [resource], the number to add (negative to subtract)
845-
*
846837
*/
847838
void Player::addMultiple(int wood, int brick, int ore, int wheat, int wool){
848839
addWood(wood);
@@ -878,7 +869,7 @@ void Player::addResource(int resourceType, int delta) {
878869
/**
879870
* Check to see if a player's resources are equal to the input
880871
* @param [resource]x5 the amount of (wood, brick, ore, wheat, wool) you are checking
881-
* @return bool if the values match
872+
* @return True if the player has the specified amounts of resources, or false otherwise
882873
*/
883874
bool Player::validateResourceAmount(int wood, int brick, int ore, int wheat, int wool){
884875
return wood==getWood() && brick==getBrick() && ore==getOre() && wheat==getWheat() && wool==getWool();
@@ -906,7 +897,7 @@ void Player::accept(GameVisitor& visitor) {
906897
/**
907898
* Compare equality with another player.
908899
* @param player The player to test equality with.
909-
* @return If the other player is equivalent to this player.
900+
* @return True if the other player is equivalent to this player, or false otherwise
910901
*/
911902
bool Player::operator==(const Player& player) const {
912903
return getName() == player.getName() &&

tests/test_DevelopmentCards.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,12 @@ TEST(DevCardTest, RoadBuildingCard){
9898
}
9999

100100
void testVictoryPointCard(Player & test_player, bool correct_result){
101-
int prevPoints = test_player.getVictoryPointsWithoutCards();
102101
int prevCards = test_player.getVictoryCards();
103102

104103
ASSERT_EQ(test_player.playVictoryCard(), correct_result);
105104
if(correct_result){
106-
ASSERT_EQ(prevPoints, test_player.getVictoryPointsWithoutCards() -1);
107105
ASSERT_EQ(prevCards, test_player.getVictoryCards()+1);
108106
}else{
109-
ASSERT_EQ(prevPoints, test_player.getVictoryPointsWithoutCards());
110107
ASSERT_EQ(prevCards, test_player.getVictoryCards());
111108
}
112109

tests/test_Player.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ TEST(PlayerTest, UpdateVictoryPoints_VictoryCards){
283283
Player& test_player = test_board.getPlayer(0);
284284

285285
ASSERT_EQ(test_player.getVictoryPoints(), 0);
286-
ASSERT_EQ(test_player.getVictoryPointsWithoutCards(), 0);
287286

288287
std::unique_ptr<DevelopmentCard> test_VictoryCard = std::unique_ptr<DevelopmentCard>(new VictoryPointCard());
289288
test_player.addOre(1);
@@ -292,7 +291,6 @@ TEST(PlayerTest, UpdateVictoryPoints_VictoryCards){
292291

293292
test_player.buyCard(test_VictoryCard);
294293
ASSERT_EQ(test_player.getVictoryPoints(), 1);
295-
ASSERT_EQ(test_player.getVictoryPointsWithoutCards(), 0);
296294
}
297295

298296
TEST(PlayerTest, UpdateVictoryPoints_LargestArmy){

0 commit comments

Comments
 (0)