Skip to content

Commit 99c8cd5

Browse files
committed
Changed GameBoard pointers in Player and DevelopmentCard to references and initialized them. Made the GameBoard initialize the player vector
1 parent bd49648 commit 99c8cd5

File tree

11 files changed

+66
-76
lines changed

11 files changed

+66
-76
lines changed

include/Deck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Deck {
2727
void reshuffleDeck();
2828

2929
public:
30-
Deck();
30+
Deck(GameBoard& board);
3131
virtual ~Deck();
3232

3333
int getSize();

include/DevelopmentCard.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class DevelopmentCard {
2525

2626
protected:
2727
DevCardType type;
28-
GameBoard *board;
28+
GameBoard& board;
2929
public:
30-
DevelopmentCard();
30+
DevelopmentCard(GameBoard& board);
3131
virtual ~DevelopmentCard();
3232

3333
virtual DevCardType getType() const = 0;
@@ -42,7 +42,7 @@ class KnightCard : public DevelopmentCard {
4242
private:
4343

4444
public:
45-
KnightCard();
45+
KnightCard(GameBoard& board);
4646
// virtual ~KnightCard();
4747

4848
virtual DevCardType getType() const;
@@ -54,7 +54,7 @@ class KnightCard : public DevelopmentCard {
5454

5555
class VictoryPointCard : public DevelopmentCard {
5656
public:
57-
VictoryPointCard();
57+
VictoryPointCard(GameBoard& board);
5858
// virtual ~VictoryPointCard();
5959

6060
virtual DevCardType getType() const;
@@ -64,7 +64,7 @@ class VictoryPointCard : public DevelopmentCard {
6464

6565
class YearOfPlentyCard : public DevelopmentCard {
6666
public:
67-
YearOfPlentyCard();
67+
YearOfPlentyCard(GameBoard& board);
6868
// virtual ~YearOfPlentyCard();
6969

7070
virtual DevCardType getType() const;
@@ -77,7 +77,7 @@ class YearOfPlentyCard : public DevelopmentCard {
7777

7878
class MonopolyCard : public DevelopmentCard {
7979
public:
80-
MonopolyCard();
80+
MonopolyCard(GameBoard& board);
8181
// virtual ~MonopolyCard();
8282

8383
virtual DevCardType getType() const;
@@ -91,7 +91,7 @@ class RoadBuildingCard : public DevelopmentCard {
9191
private:
9292

9393
public:
94-
RoadBuildingCard();
94+
RoadBuildingCard(GameBoard& board);
9595
// virtual ~RoadBuildingCard();
9696

9797
virtual DevCardType getType() const;

include/GameBoard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class GameBoard {
6161
void payoutResources(int roll);
6262

6363
public:
64-
GameBoard(std::vector<std::unique_ptr<Player>>&& players);
65-
GameBoard(std::vector<std::unique_ptr<Player>>&& players, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations);
64+
GameBoard(const std::vector<std::string>& playerNames);
65+
GameBoard(const std::vector<std::string>& playerNames, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations);
6666
GameBoard(std::istream& in);
6767
GameBoard(GameBoard&) = delete;
6868
~GameBoard();

include/Player.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Player {
4040
int armySize;
4141
int longestRoad;
4242
int victoryPoints;
43-
GameBoard *board;
43+
GameBoard& board;
4444
int resources[5];
4545
int tradeModifiers[5];
4646

@@ -49,8 +49,8 @@ class Player {
4949

5050
public:
5151

52-
Player(std::string playerName);
53-
Player(tinyxml2::XMLElement*);
52+
Player(GameBoard& board, std::string playerName);
53+
Player(GameBoard& board, tinyxml2::XMLElement*);
5454
~Player();
5555

5656
int getVictoryPoints();
@@ -64,9 +64,6 @@ class Player {
6464
void buyCard(std::unique_ptr<DevelopmentCard> card);
6565
std::string getName() const;
6666

67-
GameBoard* getBoard();
68-
void setBoard(GameBoard* newboard);
69-
7067
// void playCard(int index);
7168
// void playCard(DevelopmentCard* card);
7269

src/Deck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@
1111
/**
1212
* Construct a Deck with the standard cards available in the Settlers of Catan game.
1313
*/
14-
Deck::Deck()
14+
Deck::Deck(GameBoard& board)
1515
{
1616
// TODO Auto-generated constructor stub
1717
for(int i = 0; i < 15; i++)
1818
{
19-
DevelopmentCard* card = new KnightCard();
19+
DevelopmentCard* card = new KnightCard(board);
2020
this->deck.push_back(card);
2121
}
2222
for(int i = 0; i < 4; i++)
2323
{
24-
DevelopmentCard* card = new VictoryPointCard();
24+
DevelopmentCard* card = new VictoryPointCard(board);
2525
this->deck.push_back(card);
2626
}
2727
for(int i = 0; i < 2; i++)
2828
{
29-
DevelopmentCard* card = new YearOfPlentyCard();
29+
DevelopmentCard* card = new YearOfPlentyCard(board);
3030
this->deck.push_back(card);
3131
}
3232
for(int i = 0; i < 2; i++)
3333
{
34-
DevelopmentCard* card = new MonopolyCard();
34+
DevelopmentCard* card = new MonopolyCard(board);
3535
this->deck.push_back(card);
3636
}
3737
for(int i = 0; i < 2; i++)
3838
{
39-
DevelopmentCard* card = new RoadBuildingCard();
39+
DevelopmentCard* card = new RoadBuildingCard(board);
4040
this->deck.push_back(card);
4141
}
4242

src/DevelopmentCard.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
//Base Class
15-
DevelopmentCard::DevelopmentCard()
15+
DevelopmentCard::DevelopmentCard(GameBoard& board) : board(board)
1616
{
1717

1818
}
@@ -31,7 +31,7 @@ bool DevelopmentCard::operator==(const DevelopmentCard& other) {
3131
return getType() == other.getType();
3232
}
3333

34-
KnightCard::KnightCard():DevelopmentCard()
34+
KnightCard::KnightCard(GameBoard& board) : DevelopmentCard(board)
3535
{
3636

3737
}
@@ -43,11 +43,11 @@ DevCardType KnightCard::getType() const {
4343
void KnightCard::playCard(Player *player, Coordinate target)
4444
{
4545
//Call function to play card
46-
board->moveRobber(target);
46+
board.moveRobber(target);
4747
}
4848

4949

50-
VictoryPointCard::VictoryPointCard():DevelopmentCard()
50+
VictoryPointCard::VictoryPointCard(GameBoard& board) : DevelopmentCard(board)
5151
{
5252

5353
}
@@ -58,7 +58,7 @@ DevCardType VictoryPointCard::getType() const {
5858

5959

6060

61-
YearOfPlentyCard::YearOfPlentyCard():DevelopmentCard()
61+
YearOfPlentyCard::YearOfPlentyCard(GameBoard& board) : DevelopmentCard(board)
6262
{
6363

6464
}
@@ -77,7 +77,7 @@ void YearOfPlentyCard::playCard(Player *player, int rType1, int rType2)
7777

7878

7979

80-
MonopolyCard::MonopolyCard():DevelopmentCard()
80+
MonopolyCard::MonopolyCard(GameBoard& board) : DevelopmentCard(board)
8181
{
8282

8383
}
@@ -90,9 +90,9 @@ void MonopolyCard::playCard(Player *player, int rType)
9090
{
9191
int totalResourceCount = 0;
9292

93-
for(int i=0; i<board->getNoOfPlayers(); i++)
93+
for(int i=0; i<board.getNoOfPlayers(); i++)
9494
{
95-
Player& p = board->getPlayer(i);
95+
Player& p = board.getPlayer(i);
9696
totalResourceCount += p.getResource(rType);
9797
p.addResource(rType, (-1*p.getResource(rType)) );
9898
}
@@ -101,7 +101,9 @@ void MonopolyCard::playCard(Player *player, int rType)
101101

102102

103103

104-
RoadBuildingCard::RoadBuildingCard():DevelopmentCard(){};
104+
RoadBuildingCard::RoadBuildingCard(GameBoard& board) : DevelopmentCard(board) {
105+
106+
};
105107

106108
DevCardType RoadBuildingCard::getType() const
107109
{
@@ -111,10 +113,10 @@ DevCardType RoadBuildingCard::getType() const
111113

112114

113115
void RoadBuildingCard::playCard(Player* player, Coordinate start1, Coordinate end1, Coordinate start2, Coordinate end2){
114-
if (!board->PlaceRoad(start1, end1, *player)){
116+
if (!board.PlaceRoad(start1, end1, *player)){
115117
throw std::invalid_argument("The first road passed was not valid, no roads placed");
116118
}
117-
if ((!board->PlaceRoad(start2, end2, *player))){
119+
if ((!board.PlaceRoad(start2, end2, *player))){
118120
throw std::invalid_argument("The second road passed was not valid, only the first road was placed");
119121
}
120122
}

src/GameBoard.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ using std::vector;
3232
* Board tiles and roll numbers are randomized.
3333
* @param players A vector of the players playing the game.
3434
*/
35-
GameBoard::GameBoard(vector<unique_ptr<Player>>&& players) : players(std::move(players)) {
35+
GameBoard::GameBoard(const vector<std::string>& playerNames) {
36+
for(auto& name : playerNames) {
37+
players.push_back(std::unique_ptr<Player>(new Player(*this, name)));
38+
}
39+
3640
std::srand(std::time(0));
3741

3842
const static vector<resourceType> boardResources {BRICK, BRICK, BRICK, STONE, STONE, STONE, WHEAT, WHEAT, WHEAT, WHEAT, WOOD, WOOD, WOOD, WOOD, SHEEP, SHEEP, SHEEP, SHEEP};
@@ -104,7 +108,11 @@ void GameBoard::insertTile(Coordinate location, vector<resourceType>& resources,
104108
* @param resourceLocations A mapping from coordinates to resource types and dice values, representing the tiles.
105109
* @throws std::runtime_error When the configuration is invalid.
106110
*/
107-
GameBoard::GameBoard(std::vector<std::unique_ptr<Player>>&& players, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations) : players(std::move(players)) {
111+
GameBoard::GameBoard(const std::vector<std::string>& playerNames, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations) {
112+
for(auto& name : playerNames) {
113+
players.push_back(std::unique_ptr<Player>(new Player(*this, name)));
114+
}
115+
108116
for(auto& resource : resourceLocations) {
109117
resources[resource.first] = std::unique_ptr<ResourceTile>(new ResourceTile(*this, resource.first, resource.second.first, resource.second.second));
110118
}
@@ -152,7 +160,7 @@ GameBoard::GameBoard(istream& in) {
152160
auto playerElements = doc.RootElement()->FirstChildElement("players");
153161
if(playerElements) {
154162
for(auto playerElement = playerElements->FirstChildElement(); playerElement; playerElement = playerElement->NextSiblingElement()) {
155-
unique_ptr<Player> player(new Player(playerElement));
163+
unique_ptr<Player> player(new Player(*this, playerElement));
156164
players.emplace_back(std::move(player));
157165
}
158166
}

src/Player.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ using std::runtime_error;
2525

2626
/**
2727
* Initialize a player.
28+
* @param board The board the player is playing on.
2829
* @param playerName The name of the player. Should be unique.
2930
*/
30-
Player::Player(std::string playerName) : name(playerName)
31+
Player::Player(GameBoard& board, std::string playerName) : name(playerName), board(board)
3132
{
3233
armySize = 0;
3334
longestRoad = 0;
@@ -42,9 +43,10 @@ Player::Player(std::string playerName) : name(playerName)
4243

4344
/**
4445
* Construct a player from a serialized tinyxml2::XMLElement.
46+
* @param board The board the player is playing on.
4547
* @param elem The XMLElement to read data from.
4648
*/
47-
Player::Player(XMLElement* elem)
49+
Player::Player(GameBoard& board, XMLElement* elem) : board(board)
4850
{
4951
for(auto& r : resources) {
5052
r = 0;
@@ -58,11 +60,11 @@ Player::Player(XMLElement* elem)
5860
XMLElement* cardsElement = elem->FirstChildElement("cards");
5961
for(auto cardElem = cardsElement->FirstChildElement("card"); cardElem; cardElem = cardElem->NextSiblingElement("card")) {
6062
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()); }),
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()); }),
63+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("knight", [&board]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new KnightCard(board)); }),
64+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("victorypoint", [&board]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new VictoryPointCard(board)); }),
65+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("yearofplenty", [&board]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new YearOfPlentyCard(board)); }),
66+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("monopoly", [&board]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new MonopolyCard(board)); }),
67+
std::pair<std::string, std::function<std::unique_ptr<DevelopmentCard>(void)>>("roadbuilding", [&board]() -> std::unique_ptr<DevelopmentCard> { return std::unique_ptr<DevelopmentCard>(new RoadBuildingCard(board)); }),
6668
};
6769
auto typeIt = typeToCard.find(std::string(cardElem->FirstChildElement("type")->FirstChild()->Value()));
6870
if(typeIt == typeToCard.end()) {
@@ -162,23 +164,6 @@ int Player::getVictoryPoints()
162164
return victoryPoints;
163165
}
164166

165-
/**
166-
* The GameBoard that a player is playing on.
167-
* @return The board.
168-
*/
169-
GameBoard* Player::getBoard(){
170-
return board;
171-
}
172-
173-
/**
174-
* Assign the Player to a particular GameBoard.
175-
* @param newboard The new board they are playing on.
176-
*/
177-
void Player::setBoard(GameBoard * newboard){
178-
board = newboard;
179-
}
180-
181-
182167
/**
183168
* Acquire a development card.
184169
* @param card An owning pointer to the card the player acquired.
@@ -206,7 +191,7 @@ void Player::buyCard(std::unique_ptr<DevelopmentCard> card)
206191
// if(!std::any_of(developmentCards.begin(), developmentCards.end(), cardTester)) {
207192
// return;
208193
// }
209-
// card->playCard();
194+
// card->playCard(board);
210195
// if (card->getType() == KNIGHT) {
211196
// armySize++;
212197
// }

src/main.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@ int main(int argc, char *argv[]) {
7676

7777
updateViewport(windowWidth, windowHeight);
7878

79-
vector<unique_ptr<Player>> players;
80-
players.emplace_back(unique_ptr<Player>(new Player("test")));
8179

82-
Player& firstPlayer = *players[0];
83-
84-
GameBoard model(std::move(players));
80+
GameBoard model({"testPlayer"});
8581
GameView view(model);
8682
GameController controller(model, view);
8783

84+
Player& firstPlayer = model.getPlayer(0);
85+
8886
model.PlaceSettlement(Coordinate{0, 0}, firstPlayer);
8987
model.PlaceRoad(Coordinate{0, 0}, Coordinate{1, 0}, firstPlayer);
9088
model.PlaceRoad(Coordinate{1, 0}, Coordinate{1, 1}, firstPlayer);

tests/testSerialization.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ TEST(multiplePlayerSerialization) {
4747
TEST(testCardSerialization) {
4848
vector<unique_ptr<Player>> players;
4949
players.emplace_back(unique_ptr<Player>(new Player("test")));
50-
players[0]->buyCard(unique_ptr<DevelopmentCard>(new KnightCard(players[0].get())));
51-
players[0]->buyCard(unique_ptr<DevelopmentCard>(new VictoryPointCard(players[0].get())));
52-
players[0]->buyCard(unique_ptr<DevelopmentCard>(new YearOfPlentyCard(players[0].get())));
53-
players[0]->buyCard(unique_ptr<DevelopmentCard>(new MonopolyCard(players[0].get())));
54-
players[0]->buyCard(unique_ptr<DevelopmentCard>(new RoadBuildingCard(players[0].get())));
50+
players[0]->buyCard(unique_ptr<DevelopmentCard>(new KnightCard()));
51+
players[0]->buyCard(unique_ptr<DevelopmentCard>(new VictoryPointCard()));
52+
players[0]->buyCard(unique_ptr<DevelopmentCard>(new YearOfPlentyCard()));
53+
players[0]->buyCard(unique_ptr<DevelopmentCard>(new MonopolyCard()));
54+
players[0]->buyCard(unique_ptr<DevelopmentCard>(new RoadBuildingCard()));
5555

5656
GameBoard testBoard(std::move(players));
5757

0 commit comments

Comments
 (0)