@@ -63,6 +63,27 @@ GameBoard::GameBoard(istream& in) {
6363 players.emplace_back (std::move (player));
6464 }
6565 }
66+
67+ auto roadElements = doc.RootElement ()->FirstChildElement (" roads" );
68+ if (roadElements) {
69+ for (auto roadElement = roadElements->FirstChildElement (); roadElement; roadElement = roadElement->NextSiblingElement ()) {
70+ Coordinate start = xmlElementToCoord (*(roadElement->FirstChildElement (" start" )->FirstChildElement (" coordinate" )));
71+ Coordinate end = xmlElementToCoord (*(roadElement->FirstChildElement (" end" )->FirstChildElement (" coordinate" )));
72+ std::string ownerName = roadElement->FirstChildElement (" owner" )->FirstChild ()->Value ();
73+ Player* owner = nullptr ;
74+ for (auto & playerUnique : players) {
75+ if (playerUnique->getName () == ownerName) {
76+ owner = playerUnique.get ();
77+ }
78+ }
79+ if (owner == nullptr ) {
80+ throw std::runtime_error (" Road is owned by a nonexistant player." );
81+ }
82+ Road* newRoad = new Road (start, end, *owner);
83+ roads[start].push_back (newRoad);
84+ roads[end].push_back (newRoad);
85+ }
86+ }
6687}
6788
6889GameBoard::~GameBoard () {
@@ -99,11 +120,9 @@ void GameBoard::freeRoads(){
99120 */
100121void GameBoard::removeRoadEnd (Road * startRoad){
101122 std::vector<Road*> endRoadVector = roads[startRoad->getEnd ()];
102- for (std::vector<Road*>::iterator endRoad = endRoadVector.begin (); endRoad != endRoadVector.end (); ++endRoad ){
123+ for (std::vector<Road*>::iterator endRoad = endRoadVector.begin (); endRoad != endRoadVector.end (); endRoad++ ){
103124 if ((*endRoad) == startRoad){
104- endRoadVector.erase (endRoad);
105- // Need to decrement the iterator to account for the lost item
106- endRoad--;
125+ (*endRoad) = nullptr ;
107126 }
108127 }
109128}
@@ -363,6 +382,11 @@ void GameBoard::accept(GameVisitor& visitor) {
363382 for (auto & it : resources) {
364383 it.second ->accept (visitor);
365384 }
385+ for (auto & roadCoordVec : roads) {
386+ for (auto & road : roadCoordVec.second ) {
387+ road->accept (visitor);
388+ }
389+ }
366390 for (auto & it : players) {
367391 it->accept (visitor);
368392 }
@@ -390,6 +414,24 @@ bool GameBoard::operator==(const GameBoard& other) const {
390414 return false ;
391415 }
392416 }
417+ for (auto & roadCoordVec : roads) {
418+ const auto & otherVecIt = other.roads .find (roadCoordVec.first );
419+ if (otherVecIt == other.roads .end ()) {
420+ return false ;
421+ }
422+ auto & otherCoordVec = *otherVecIt;
423+ if (roadCoordVec.second .size () != otherCoordVec.second .size ()) {
424+ return false ;
425+ }
426+ for (size_t i = 0 ; i < roadCoordVec.second .size (); i++) {
427+ const Road& myRoad = *(roadCoordVec.second [i]);
428+ const Road& otherRoad = *(otherCoordVec.second [i]);
429+ if (myRoad == otherRoad) {}
430+ else {
431+ return false ;
432+ }
433+ }
434+ }
393435 if (players.size () != other.players .size ()) {
394436 return false ;
395437 }
0 commit comments