Skip to content

Commit 670e903

Browse files
Kyle GrageKyle Grage
authored andcommitted
Implement placing wonders
1 parent 8281136 commit 670e903

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

include/GameBoard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ class GameBoard {
8888

8989
void PlaceSettlement(Coordinate location, Player& Owner);
9090
void UpgradeSettlement(Coordinate location);
91-
//void PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
91+
void UpgradeToWonder(Coordinate location);
9292

9393

9494
bool buyRoad(Coordinate start, Coordinate end, Player& Owner);
9595

9696
//void PlaceSettlement(Coordinate location, Player& Owner);
9797
void PlaceCity(Coordinate location, Player& Owner);
98+
void PlaceWonder(Coordinate location, Player& Owner)
9899
bool PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
99100

100101
void accept(GameVisitor& visitor);

src/GameBoard.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
#include "CornerPiece.h"
1616

17+
#include "Settlement.h"
1718
#include "City.h"
19+
#include "Wonder.h"
1820

1921
using std::shared_ptr;
2022
using std::random_shuffle;
@@ -199,7 +201,7 @@ GameBoard::GameBoard(istream& in) {
199201
}
200202
}
201203
if(owner == nullptr) {
202-
throw std::runtime_error("Road is owned by a nonexistant player.");
204+
throw std::runtime_error("Settlement is owned by a nonexistant player.");
203205
}
204206
PlaceSettlement(location, *owner);
205207
}
@@ -218,7 +220,26 @@ GameBoard::GameBoard(istream& in) {
218220
}
219221
}
220222
if(owner == nullptr) {
221-
throw std::runtime_error("Road is owned by a nonexistant player.");
223+
throw std::runtime_error("City is owned by a nonexistant player.");
224+
}
225+
PlaceCity(location, *owner);
226+
}
227+
}
228+
229+
auto wonderElements = doc.RootElement()->FirstChildElement("wonders");
230+
if(wonderElements) {
231+
for(auto wonderElement = wonderElements->FirstChildElement(); wonderElement; wonderElement = wonderElement->NextSiblingElement()) {
232+
Coordinate location = xmlElementToCoord(*(wonderElement->FirstChildElement("coordinate")));
233+
234+
std::string ownerName = wonderElement->FirstChildElement("owner")->FirstChild()->Value();
235+
Player* owner = nullptr;
236+
for(auto& playerUnique : players) {
237+
if(playerUnique->getName() == ownerName) {
238+
owner = playerUnique.get();
239+
}
240+
}
241+
if(owner == nullptr) {
242+
throw std::runtime_error("Wonder is owned by a nonexistant player.");
222243
}
223244
PlaceCity(location, *owner);
224245
}
@@ -587,13 +608,32 @@ void GameBoard::PlaceCity(Coordinate location, Player& Owner){
587608

588609
}
589610

611+
/**
612+
* Place a city on the board.
613+
* @param location Where to place it on the board.
614+
* @param Owner The player placing the city.
615+
*/
616+
void GameBoard::PlaceWonder(Coordinate location, Player& Owner){
617+
corners[location] = std::unique_ptr<CornerPiece>(new Wonder(*this, location, Owner));
618+
619+
}
620+
590621
/**
591622
* Upgrade a settlement to a city.
592623
* @param location Where the settlement being upgraded is.
593624
*/
594625
void GameBoard::UpgradeSettlement(Coordinate location){
595626
if(corners.find(location) != corners.end())
596-
corners[location] = std::unique_ptr<CornerPiece>(new City(*corners[location])); //TODO test for memory leak
627+
corners[location] = std::unique_ptr<CornerPiece>(new City(*corners[location])); //TODO test for memory leak
628+
}
629+
630+
/**
631+
* Upgrade a settlement to a city.
632+
* @param location Where the settlement being upgraded is.
633+
*/
634+
void GameBoard::UpgradeToWonder(Coordinate location){
635+
if(corners.find(location) != corners.end())
636+
corners[location] = std::unique_ptr<CornerPiece>(new Wonder(*corners[location])); //TODO test for memory leak
597637
}
598638

599639
/**

0 commit comments

Comments
 (0)