Skip to content

Commit bd49648

Browse files
committed
Fixed compilation errors.
1 parent 2fd5f73 commit bd49648

File tree

9 files changed

+42
-22
lines changed

9 files changed

+42
-22
lines changed

include/DevelopmentCard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum DevCardType { KNIGHT, VICTORYPOINT, YEAROFPLENTY, MONOPOLY, ROADBUILDING };
2323

2424
class DevelopmentCard {
2525

26-
private:
26+
protected:
2727
DevCardType type;
2828
GameBoard *board;
2929
public:

include/GameBoard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class GameBoard {
104104
const std::vector<std::unique_ptr<Player>>& getPlayers() const;
105105

106106
int getNoOfPlayers();
107-
std::unique_ptr<Player> getPlayer(int index);
107+
Player& getPlayer(int index);
108108

109109
bool testRollChecking(int* rolls);
110110

include/Player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Player {
103103
void addWheat(int resource);
104104
void addWool(int resource);
105105

106-
int getResource(int resourceType); //
106+
int getResource(int resourceType) const; //
107107
void addResource(int resourceType, int delta);
108108

109109
void accept(GameVisitor& visitor);

include/Serialization.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class XMLVisitor : public GameVisitor {
1919
std::map<std::string, tinyxml2::XMLElement*> playerElementMap;
2020
std::set<Road*> serializedRoads;
2121

22+
Player* lastPlayer;
23+
2224
tinyxml2::XMLElement* coordinateElement(const Coordinate& c);
2325
public:
2426
XMLVisitor();

src/Deck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ Deck::Deck()
1616
// TODO Auto-generated constructor stub
1717
for(int i = 0; i < 15; i++)
1818
{
19-
DevelopmentCard* card = new KnightCard(NULL);
19+
DevelopmentCard* card = new KnightCard();
2020
this->deck.push_back(card);
2121
}
2222
for(int i = 0; i < 4; i++)
2323
{
24-
DevelopmentCard* card = new VictoryPointCard(NULL);
24+
DevelopmentCard* card = new VictoryPointCard();
2525
this->deck.push_back(card);
2626
}
2727
for(int i = 0; i < 2; i++)
2828
{
29-
DevelopmentCard* card = new YearOfPlentyCard(NULL);
29+
DevelopmentCard* card = new YearOfPlentyCard();
3030
this->deck.push_back(card);
3131
}
3232
for(int i = 0; i < 2; i++)
3333
{
34-
DevelopmentCard* card = new MonopolyCard(NULL);
34+
DevelopmentCard* card = new MonopolyCard();
3535
this->deck.push_back(card);
3636
}
3737
for(int i = 0; i < 2; i++)
3838
{
39-
DevelopmentCard* card = new RoadBuildingCard(NULL);
39+
DevelopmentCard* card = new RoadBuildingCard();
4040
this->deck.push_back(card);
4141
}
4242

src/DevelopmentCard.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ void MonopolyCard::playCard(Player *player, int rType)
9292

9393
for(int i=0; i<board->getNoOfPlayers(); i++)
9494
{
95-
Player* p = board->getPlayer(i);
96-
totalResourceCount += p->getResource(rType);
97-
p->addResource(rType, (-1*p->getResource(rType)) );
95+
Player& p = board->getPlayer(i);
96+
totalResourceCount += p.getResource(rType);
97+
p.addResource(rType, (-1*p.getResource(rType)) );
9898
}
9999
player->addResource(rType, totalResourceCount);
100100
}

src/GameBoard.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,15 @@ int GameBoard::getNoOfPlayers()
735735

736736

737737
/**
738+
* @param index The index to look at.
738739
* @return player at index index
739740
*/
740-
std::unique_ptr<Player> GameBoard::getPlayer(int index)
741+
Player& GameBoard::getPlayer(int index)
741742
{
742743
if(index >= getNoOfPlayers())
743-
return NULL;
744+
throw std::runtime_error("Invalid player index.");
744745

745-
return players[index];
746+
return *players[index];
746747
}
747748

748749

src/Player.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ Player::Player(XMLElement* elem)
5858
XMLElement* cardsElement = elem->FirstChildElement("cards");
5959
for(auto cardElem = cardsElement->FirstChildElement("card"); cardElem; cardElem = cardElem->NextSiblingElement("card")) {
6060
static const map<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>> typeToCard = {
61-
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("knight", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new KnightCard(this)); }),
62-
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("victorypoint", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new VictoryPointCard(this)); }),
63-
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("yearofplenty", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new YearOfPlentyCard(this)); }),
64-
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("monopoly", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new MonopolyCard(this)); }),
65-
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("roadbuilding", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new RoadBuildingCard(this)); }),
61+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("knight", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new KnightCard()); }),
62+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("victorypoint", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new VictoryPointCard()); }),
63+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("yearofplenty", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new YearOfPlentyCard()); }),
64+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("monopoly", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new MonopolyCard()); }),
65+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("roadbuilding", [this]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new RoadBuildingCard()); }),
6666
};
6767
auto typeIt = typeToCard.find(std::string(cardElem->FirstChildElement("type")->FirstChild()->Value()));
6868
if(typeIt == typeToCard.end()) {
@@ -384,7 +384,7 @@ bool Player::acceptOffer(Player* p, int offer[], int demand[])
384384
*/
385385
int Player::getRandomResource()
386386
{
387-
int total = getWood() + getBrick() + getOre() + getWheat() + getWool();
387+
//int total = getWood() + getBrick() + getOre() + getWheat() + getWool();
388388
int randomNo = 0;
389389

390390
if(getWood()!=0 && randomNo <= getWood())
@@ -416,6 +416,18 @@ int Player::getRandomResource()
416416

417417
}
418418

419+
/**
420+
* Get the resources a player has of a given type.
421+
* @param resourceType The index to get the resource count of.
422+
* @return The amount of the resource the player has.
423+
*/
424+
int Player::getResource(int resourceType) const {
425+
if(resourceType < 5) {
426+
return resources[resourceType];
427+
} else {
428+
throw std::runtime_error("Type index is out of bounds.");
429+
}
430+
}
419431

420432
/**
421433
* Determine if the player has a valid (nonnegative) set of resources.

src/Serialization.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using tinyxml2::XMLText;
2121
/**
2222
* Construct the serialization visitor.
2323
*/
24-
XMLVisitor::XMLVisitor() {
24+
XMLVisitor::XMLVisitor() : lastPlayer(nullptr) {
2525
xmldoc.InsertEndChild(xmldoc.NewElement("catangame"));
2626
//xmldoc.RootElement()->SetName("catangame");
2727
}
@@ -150,6 +150,8 @@ void XMLVisitor::visit(Player& player) {
150150

151151
playersElement->InsertEndChild(newPlayerElement);
152152
playerElementMap[player.getName()] = newPlayerElement;
153+
154+
lastPlayer = &player;
153155
}
154156

155157
/**
@@ -193,7 +195,10 @@ void XMLVisitor::visit(ResourceTile& tile) {
193195
* @param card The card to serialize.
194196
*/
195197
void XMLVisitor::visit(DevelopmentCard& card) {
196-
auto playerElementIt = playerElementMap.find(card.getOwner()->getName());
198+
if(lastPlayer == nullptr) {
199+
throw runtime_error("Don't know which player to assign this card to.");
200+
}
201+
auto playerElementIt = playerElementMap.find(lastPlayer->getName());
197202
if(playerElementIt == playerElementMap.end()) {
198203
throw runtime_error("This card belongs to a player that hasn't been saved!");
199204
}

0 commit comments

Comments
 (0)