Skip to content

Commit cdfa123

Browse files
committed
Added functionality for discarding cards
1 parent 25e6993 commit cdfa123

File tree

4 files changed

+102
-3
lines changed

4 files changed

+102
-3
lines changed

include/GameBoard.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "tinyxml2.h"
1818
#include "Road.h"
1919
#include "GameDice.h"
20+
#include "Deck.h"
21+
22+
#include "DevelopmentCard.h"
23+
2024

2125
class GameVisitor;
2226

@@ -31,7 +35,7 @@ class GameBoard {
3135

3236
GameDice dice;
3337

34-
38+
Deck deck;
3539

3640
std::map<Coordinate, std::vector<std::shared_ptr<Road>>> roads;
3741

@@ -77,6 +81,9 @@ class GameBoard {
7781

7882
void save(std::ostream& out);
7983

84+
void buyCard(Player& owner);
85+
void discardCard(DevelopmentCard * card);
86+
8087
ResourceTile& getResourceTile(Coordinate location) const;
8188

8289
const std::map<Coordinate, std::unique_ptr<ResourceTile>>& getResources() const;

src/GameBoard.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "CornerPiece.h"
1616
#include "GameDice.h"
17-
1817
#include "Settlement.h"
1918
#include "City.h"
2019
#include "Wonder.h"
@@ -997,3 +996,54 @@ void GameBoard::payoutResources(int roll)
997996
}
998997
}
999998
}
999+
1000+
/**
1001+
* Buys a card drawn from the deck
1002+
*/
1003+
void GameBoard::buyCard(Player& owner){
1004+
if(owner.canBuyCard() && deck.getSize() > 0){
1005+
1006+
DevelopmentCard * card_ptr = deck.drawCard();
1007+
1008+
std::unique_ptr<DevelopmentCard> knight = std::unique_ptr<DevelopmentCard>(new KnightCard());
1009+
std::unique_ptr<DevelopmentCard> victorypoint = std::unique_ptr<DevelopmentCard>(new VictoryPointCard());
1010+
std::unique_ptr<DevelopmentCard> monopoly = std::unique_ptr<DevelopmentCard>(new MonopolyCard());
1011+
std::unique_ptr<DevelopmentCard> yearofplenty = std::unique_ptr<DevelopmentCard>(new YearOfPlentyCard());
1012+
std::unique_ptr<DevelopmentCard> roadbuilding = std::unique_ptr<DevelopmentCard>(new RoadBuildingCard());
1013+
1014+
switch (card_ptr->getType()){
1015+
case KNIGHT:
1016+
owner.buyCard(knight);
1017+
break;
1018+
case VICTORYPOINT:
1019+
owner.buyCard(victorypoint);
1020+
break;
1021+
case MONOPOLY:
1022+
owner.buyCard(monopoly);
1023+
break;
1024+
case YEAROFPLENTY:
1025+
owner.buyCard(yearofplenty);
1026+
break;
1027+
case ROADBUILDING:
1028+
owner.buyCard(roadbuilding);
1029+
break;
1030+
default:
1031+
break;
1032+
}
1033+
1034+
delete(card_ptr);
1035+
}
1036+
}
1037+
1038+
/**
1039+
* Discards a card back into the deck
1040+
*/
1041+
void GameBoard::discardCard(DevelopmentCard * card){
1042+
deck.discard(card);
1043+
}
1044+
1045+
1046+
1047+
1048+
1049+

src/Player.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,51 @@ bool Player::canBuySettlement(){
159159
return getWood() >= 1 && getBrick() >= 1 && getWheat() >= 1 && getWool() >= 1;
160160
}
161161

162+
/**
163+
* Returns the number of knight cards this player has played
164+
*/
162165
int Player::getArmySize() const{
163166
return armySize;
164167
}
168+
169+
/**
170+
* Returns true if this player has the largest army, false otherwise
171+
*/
165172
bool Player::hasLargestArmy() const{
166173
return largestArmy;
167174
}
175+
176+
/**
177+
* Sets whether or not this player has the largest army
178+
*/
168179
void Player::setLargestArmy(bool newVal){
169180
largestArmy = newVal;
170181
}
171182

183+
/**
184+
* Returns the size of this player's longest road
185+
*/
172186
int Player::getLongestRoadSize() const{
173187
return longestRoadSize;
174188
}
189+
190+
/**
191+
* Returns true if this player has the longet road, false otherwise
192+
*/
175193
bool Player::hasLongestRoad() const{
176194
return longestRoad;
177195
}
196+
197+
/**
198+
* Sets whether or not this player has the longest road
199+
*/
178200
void Player::setLongestRoad(bool newVal){
179201
longestRoad = newVal;
180202
}
181203

204+
/**
205+
* Sets how long this player's longest road is
206+
*/
182207
void Player::setLongestRoadSize(int newVal){
183208
longestRoadSize = newVal;
184209
}
@@ -261,7 +286,7 @@ bool Player::buyCard(){
261286

262287

263288
/**
264-
* Update the player's internal state with their victory states.
289+
* Updates the player's victory points to the amount of points they have.
265290
*/
266291
void Player::updateVictoryPoints()
267292
{
@@ -479,6 +504,7 @@ bool Player::playYearOfPlenty(int resourceType){
479504
if(developmentCards[YEAROFPLENTY] > 0){
480505
developmentCards[YEAROFPLENTY]--;
481506
addResource(resourceType, 2);
507+
board.discardCard(new YearOfPlentyCard());
482508
return true;
483509
}
484510
return false;
@@ -498,6 +524,7 @@ bool Player::playMonopoly(int resourceType){
498524
for(auto& player : board.getPlayers()) {
499525
addResource(resourceType, player->giveAllResources(resourceType));
500526
}
527+
board.discardCard(new MonopolyCard());
501528
return true;
502529
}
503530
return false;
@@ -520,6 +547,7 @@ bool Player::playRoadBuilding(Coordinate start1, Coordinate end1, Coordinate sta
520547
board.PlaceRoad(start2, end2, *this);
521548
}
522549
developmentCards[ROADBUILDING]--;
550+
board.discardCard(new RoadBuildingCard());
523551
return true;
524552
}
525553
}

tests/test_GameBoard.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,21 @@ TEST(canRobberRob){
348348
}
349349
**/
350350

351+
TEST(buyCard){
352+
GameBoard test_board({"tester1"});
353+
Player& test_player = test_board.getPlayer(0);
354+
355+
CHECK(test_player.getDevCardsInHand() == 0);
356+
test_board.buyCard(test_player);
357+
CHECK(test_player.getDevCardsInHand() == 0);
351358

359+
test_player.addOre(1);
360+
test_player.addWheat(1);
361+
test_player.addWool(1);
362+
363+
test_board.buyCard(test_player);
364+
CHECK(test_player.getDevCardsInHand() == 1);
365+
}
352366

353367

354368

0 commit comments

Comments
 (0)