Skip to content

Commit 0b64eea

Browse files
committed
Moved road marking into a map parameter to not affect road state
1 parent 751a265 commit 0b64eea

File tree

5 files changed

+10
-51
lines changed

5 files changed

+10
-51
lines changed

include/GameBoard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GameBoard {
3939
int constructFileFromBoard(std::ofstream &file);
4040

4141
void removeRoadEnd(std::shared_ptr<Road> startRoad);
42-
int FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, int length);
42+
int FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, std::map<Road*, bool>& markedRoads, int length);
4343

4444
void createRing(Coordinate topRight, int sideLength, std::vector<resourceType>& resources, std::vector<int>& rolls);
4545
void insertTile(Coordinate location, std::vector<resourceType>& resources, std::vector<int>& rolls);

include/Road.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class Road {
1616

1717
Coordinate start;
1818
Coordinate end;
19-
20-
bool marker;
2119
public:
2220
Road(Coordinate start, Coordinate end, Player& Owner);
2321
Road(Road&) = delete;
@@ -29,10 +27,6 @@ class Road {
2927

3028
bool equals(const Road& otherRoad);
3129
bool equals(const Coordinate& otherStart, const Coordinate& otherEnd);
32-
33-
bool isMarked();
34-
void mark();
35-
void unmark();
3630

3731
Player& getOwner();
3832
Player& getOwner() const;

src/GameBoard.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@ int GameBoard::FindLongestRoad(Player & owner){
361361
for (auto roadVector = roads.begin(); roadVector != roads.end(); ++roadVector){
362362
//find the longest path from v
363363
std::map<Coordinate, bool> marked;
364+
std::map<Road*, bool> markedRoads;
364365
Coordinate start = roadVector->first;
365-
int temp_longest_path = FindLongestRoad_FromPoint(start, owner, marked, 0);
366+
int temp_longest_path = FindLongestRoad_FromPoint(start, owner, marked, markedRoads, 0);
366367

367368
//if that path is longer than the current longest, set to the longest
368369
if (temp_longest_path > longest_path)
@@ -373,7 +374,7 @@ int GameBoard::FindLongestRoad(Player & owner){
373374
}
374375

375376

376-
int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, int length){
377+
int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, std::map<Road*, bool>& markedRoads, int length){
377378
marked[curr] = true;
378379
int longest_path = length;
379380
//traverse all the surrounding edges and vertices
@@ -382,17 +383,17 @@ int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::m
382383
int temp_longest_path = length;
383384

384385
//if the owner is correct and the road is unmarked
385-
if ( !(*road)->isMarked() && (*road)->owner->getName().compare(owner.getName()) == 0){
386+
if ( !markedRoads[road->get()] && (*road)->owner->getName() == owner.getName()){
386387

387388
temp_longest_path++;
388-
(*road)->mark();
389+
markedRoads[road->get()] = true;
389390
//Check if you can traverse to the next vertex and make that step if you can
390391
if(curr != (*road)->getStart() && !marked[(*road)->getStart()]){
391-
temp_longest_path = FindLongestRoad_FromPoint((*road)->getStart(), owner, marked, temp_longest_path);
392+
temp_longest_path = FindLongestRoad_FromPoint((*road)->getStart(), owner, marked, markedRoads, temp_longest_path);
392393
}else if (curr != (*road)->getEnd() && !marked[(*road)->getEnd()]){
393-
temp_longest_path = FindLongestRoad_FromPoint((*road)->getEnd(), owner, marked, temp_longest_path);
394+
temp_longest_path = FindLongestRoad_FromPoint((*road)->getEnd(), owner, marked, markedRoads, temp_longest_path);
394395
}
395-
(*road)->unmark();
396+
markedRoads[road->get()] = false;
396397
}
397398

398399
if(temp_longest_path > longest_path)

src/Road.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ Road::Road(Coordinate start, Coordinate end, Player& Owner) {
1111
this->start = end;
1212
}
1313
owner = &Owner;
14-
15-
marker = false;
16-
14+
1715
//If the input is bad, throw an exception so bad roads won't be built
1816
if(!checkRoad()){
1917
throw -1;
@@ -79,27 +77,6 @@ bool Road::equals(const Coordinate& otherStart, const Coordinate& otherEnd){
7977
return false;
8078
}
8179

82-
/**
83-
* Returns true if the road is marked, false otherwise
84-
*/
85-
bool Road::isMarked(){
86-
return marker;
87-
}
88-
89-
/**
90-
* Marks the road
91-
*/
92-
void Road::mark(){
93-
marker = true;
94-
}
95-
96-
/**
97-
* Unmarks the road
98-
*/
99-
void Road::unmark(){
100-
marker = false;
101-
}
102-
10380
Player& Road::getOwner() {
10481
return *owner;
10582
}

tests/test_Roads.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,6 @@ TEST(road_equals_Coordinate){
7878
CHECK(!test_road_1.equals(start_3, end_3));
7979
}
8080

81-
TEST(road_marker){
82-
Coordinate start(0,0);
83-
Coordinate end(1,0);
84-
Player test_player("tester");
85-
Road test_road(start, end, test_player);
86-
87-
CHECK(test_road.isMarked() == false);
88-
test_road.mark();
89-
CHECK(test_road.isMarked() == true);
90-
test_road.unmark();
91-
CHECK(test_road.isMarked() == false);
92-
}
93-
9481

9582

9683

0 commit comments

Comments
 (0)