Skip to content

Commit 25377d7

Browse files
committed
Merge branch 'master' into serialization
2 parents ffe1014 + d4404d0 commit 25377d7

File tree

3 files changed

+22
-47
lines changed

3 files changed

+22
-47
lines changed

include/GameBoard.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class GameBoard {
2727
void addResource(int x, int y, resourceType res, int val);
2828
bool checkRolls(int* rolls);
2929

30-
std::map<Coordinate, std::vector<Road*>> roads;
30+
std::map<Coordinate, std::vector<std::shared_ptr<Road>>> roads;
3131

3232
bool verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner);
3333
bool outOfBounds(const Coordinate& coord);
@@ -37,8 +37,7 @@ class GameBoard {
3737
int constructBoardFromFile(std::ifstream &file);
3838
int constructFileFromBoard(std::ofstream &file);
3939

40-
void freeRoads();
41-
void removeRoadEnd(Road * startRoad);
40+
void removeRoadEnd(std::shared_ptr<Road> startRoad);
4241
int FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, int length);
4342

4443
public:
@@ -51,7 +50,7 @@ class GameBoard {
5150
void save(std::ostream& out);
5251

5352
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
54-
Road * getRoad(Coordinate start, Coordinate end);
53+
std::shared_ptr<Road> getRoad(Coordinate start, Coordinate end);
5554

5655
int FindLongestRoad(Player & owner);
5756

src/GameBoard.cpp

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Serialization.h"
1212
#include "tinyxml2.h"
1313

14+
using std::shared_ptr;
1415
using std::random_shuffle;
1516
using std::time;
1617
using 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

8990
GameBoard::~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
*/
211187
bool 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

tests/test_GameBoard.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TEST(place_road_good){
3838
GameBoard * test_board = new GameBoard(std::move(players));
3939

4040
test_board->PlaceRoad(start, end, test_player);
41-
Road * test_road = test_board->getRoad(start, end);
41+
std::shared_ptr<Road> test_road = test_board->getRoad(start, end);
4242
if (test_road == NULL)
4343
CHECK(false);
4444
else
@@ -58,7 +58,7 @@ TEST(place_road_badroad){
5858
Coordinate end(0,2);
5959

6060
test_board->PlaceRoad(start, end, test_player);
61-
Road * test_road = test_board->getRoad(start, end);
61+
std::shared_ptr<Road> test_road = test_board->getRoad(start, end);
6262

6363
CHECK(test_road == NULL);
6464

@@ -76,7 +76,7 @@ TEST(place_road_outofbounds){
7676
Coordinate end(-1,0);
7777

7878
test_board->PlaceRoad(start, end, test_player);
79-
Road * test_road = test_board->getRoad(start, end);
79+
std::shared_ptr<Road> test_road = test_board->getRoad(start, end);
8080

8181
CHECK(test_road == NULL);
8282

@@ -94,7 +94,7 @@ TEST(place_road_roadexists){
9494
Coordinate end(0,1);
9595

9696
test_board->PlaceRoad(start, end, test_player);
97-
Road * test_road = test_board->getRoad(start, end);
97+
std::shared_ptr<Road> test_road = test_board->getRoad(start, end);
9898
if (test_road == NULL)
9999
CHECK(false);
100100
else

0 commit comments

Comments
 (0)