1111#include " Serialization.h"
1212#include " tinyxml2.h"
1313
14+ using std::shared_ptr;
1415using std::random_shuffle;
1516using std::time;
1617using std::string;
@@ -79,48 +80,23 @@ GameBoard::GameBoard(istream& in) {
7980 if (owner == nullptr ) {
8081 throw std::runtime_error (" Road is owned by a nonexistant player." );
8182 }
82- Road* newRoad = new Road (start, end, *owner);
83+ std::shared_ptr< Road> newRoad ( new Road (start, end, *owner) );
8384 roads[start].push_back (newRoad);
8485 roads[end].push_back (newRoad);
8586 }
8687 }
8788}
8889
8990GameBoard::~GameBoard () {
90- freeRoads ();
91- }
92-
93- /*
94- * Frees the roads data structure to prevent memory leaks
95- */
96-
97- void GameBoard::freeRoads (){
98- // Iterate over all the points in the roads map
99- for (auto roadVector = roads.begin (); roadVector != roads.end (); ++roadVector)
100- {
101- // Iterate all the roads at a given point
102- for (std::vector<Road*>::iterator road = roadVector->second .begin (); road != roadVector->second .end (); ++road) {
103- Road * roadPtr = *road;
104-
105- // If this is the start of the road we want to remove it, but we must first erase it the list at the other end of the road
106- // If we don't then we may try to access a road which has already been freed
107- if (roadPtr != NULL && roadPtr->getStart () == roadVector->first ){
108- removeRoadEnd (roadPtr);
109- roadVector->second .erase (road);
110- // Need to decrement the iterator to account for the lost item
111- road--;
112- delete roadPtr;
113- }
114- }
115- }
91+
11692}
11793
11894/* *
11995 * Find and remove the road that matches startRoad
12096 */
121- void GameBoard::removeRoadEnd (Road * startRoad){
122- std::vector<Road* > endRoadVector = roads[startRoad->getEnd ()];
123- for (std::vector<Road* >::iterator endRoad = endRoadVector.begin (); endRoad != endRoadVector.end (); endRoad++){
97+ void GameBoard::removeRoadEnd (std::shared_ptr< Road> startRoad){
98+ std::vector<shared_ptr< Road> > endRoadVector = roads[startRoad->getEnd ()];
99+ for (std::vector<shared_ptr< Road> >::iterator endRoad = endRoadVector.begin (); endRoad != endRoadVector.end (); endRoad++){
124100 if ((*endRoad) == startRoad){
125101 (*endRoad) = nullptr ;
126102 }
@@ -209,7 +185,7 @@ bool GameBoard::outOfBounds(const Coordinate& coord) {
209185 * Checks to make sure the road doesn't already exist. If it does, then we don't want to add it again
210186 */
211187bool GameBoard::roadExists (Coordinate start, Coordinate end) {
212- Road * isRoad = getRoad (start, end);
188+ std::shared_ptr< Road> isRoad = getRoad (start, end);
213189 if (isRoad == NULL )
214190 return false ;
215191 return true ;
@@ -253,14 +229,14 @@ void GameBoard::PlaceRoad(Coordinate start, Coordinate end, Player& Owner) {
253229 if (!verifyRoadPlacement (start, end, Owner))
254230 return ;
255231
256- Road * newRoad;
232+ std::shared_ptr< Road> newRoad;
257233 try {
258- newRoad = new Road (start, end, Owner);
234+ newRoad = std::shared_ptr<Road>( new Road (start, end, Owner) );
259235 } catch (int n) {
260236 // Coordinates did not meet the criteria for a valid road
261237 return ;
262238 }
263- std::vector<Road* > roadVector = roads[start];
239+ std::vector<shared_ptr< Road> > roadVector = roads[start];
264240 roadVector.push_back (newRoad);
265241 roads[start] = roadVector;
266242
@@ -272,9 +248,9 @@ void GameBoard::PlaceRoad(Coordinate start, Coordinate end, Player& Owner) {
272248/* *
273249 * returns a pointer to the road located at the specified coordinates. Will return NULL if the road is not found
274250 */
275- Road * GameBoard::getRoad (Coordinate start, Coordinate end){
276- std::vector<Road* > roadVector = roads[start];
277- for (std::vector<Road* >::iterator road = roadVector.begin (); road != roadVector.end (); ++road) {
251+ std::shared_ptr< Road> GameBoard::getRoad (Coordinate start, Coordinate end){
252+ std::vector<shared_ptr< Road> > roadVector = roads[start];
253+ for (std::vector<shared_ptr< Road> >::iterator road = roadVector.begin (); road != roadVector.end (); ++road) {
278254 if ((*road)->equals (start, end))
279255 return *road;
280256 }
@@ -306,8 +282,8 @@ int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::m
306282 marked[curr] = true ;
307283 int longest_path = length;
308284 // traverse all the surrounding edges and vertices
309- std::vector<Road* > roadVector = roads[curr];
310- for (std::vector<Road* >::iterator road = roadVector.begin (); road != roadVector.end (); ++road) {
285+ std::vector<shared_ptr< Road> > roadVector = roads[curr];
286+ for (std::vector<shared_ptr< Road> >::iterator road = roadVector.begin (); road != roadVector.end (); ++road) {
311287 int temp_longest_path = length;
312288
313289 // if the owner is correct and the road is unmarked
0 commit comments