Skip to content

Commit b49effd

Browse files
committed
Made some road functions const
1 parent 0b64eea commit b49effd

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

include/GameBoard.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ class GameBoard {
3030

3131
bool isValidBoard() const;
3232

33-
bool verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner);
34-
bool outOfBounds(const Coordinate& coord);
35-
bool roadExists(Coordinate start, Coordinate end);
36-
bool isRoadConnectionPoint(Coordinate point, Player& Owner);
33+
bool verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner) const;
34+
bool outOfBounds(const Coordinate& coord) const;
35+
bool roadExists(Coordinate start, Coordinate end) const;
36+
bool isRoadConnectionPoint(Coordinate point, Player& Owner) const;
3737

3838
int constructBoardFromFile(std::ifstream &file);
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, std::map<Road*, bool>& markedRoads, int length);
42+
int FindLongestRoad_FromPoint(Coordinate curr, const Player & owner, std::map<Coordinate, bool>& marked, std::map<Road*, bool>& markedRoads, int length) const;
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);
@@ -54,9 +54,9 @@ class GameBoard {
5454
void save(std::ostream& out);
5555

5656
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
57-
std::shared_ptr<Road> getRoad(Coordinate start, Coordinate end);
57+
const std::shared_ptr<Road> getRoad(Coordinate start, Coordinate end) const;
5858

59-
int FindLongestRoad(Player & owner);
59+
int FindLongestRoad(const Player & owner) const;
6060

6161
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
6262

src/GameBoard.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
245245
/**
246246
* Checks to make sure the coordinate is within bounds of the board and not a resource tile.
247247
*/
248-
bool GameBoard::outOfBounds(const Coordinate& coord) {
248+
bool GameBoard::outOfBounds(const Coordinate& coord) const {
249249
//All valid coordinates are adjacent to resource tiles.
250250
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
251251
for(auto& diff : adjacentCoordDiffs) {
@@ -259,29 +259,33 @@ bool GameBoard::outOfBounds(const Coordinate& coord) {
259259
/**
260260
* Checks to make sure the road doesn't already exist. If it does, then we don't want to add it again
261261
*/
262-
bool GameBoard::roadExists(Coordinate start, Coordinate end) {
262+
bool GameBoard::roadExists(Coordinate start, Coordinate end) const {
263263
return bool(getRoad(start, end)); // shared_ptr can convert to bool
264264
}
265265

266266

267267
/**
268268
* Checks to make sure the road being placed at a valid point according to the rules
269269
*/
270-
bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner){
270+
bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner) const {
271271
//is there a settlement we can build off of
272-
if(corners.count(point) > 0){
273-
CornerPiece * corner = corners[point].get();
272+
auto cornerIt = corners.find(point);
273+
if(cornerIt != corners.end()){
274+
const CornerPiece * corner = cornerIt->second.get();
274275
if(corner != NULL){
275276
if (corner->getOwner() == Owner)
276277
return true;
277278
}
278279
}
279-
280+
280281
//is there a road we can build off of
281-
std::vector<shared_ptr<Road>> roadVector = roads[point];
282-
for (std::vector<shared_ptr<Road>>::iterator road = roadVector.begin(); road != roadVector.end(); ++road) {
283-
if ((*road)->getOwner() == Owner)
284-
return true;
282+
auto roadIt = roads.find(point);
283+
if(roadIt != roads.end()) {
284+
const std::vector<shared_ptr<Road>>& roadVector = roadIt->second;
285+
for (auto road = roadVector.begin(); road != roadVector.end(); ++road) {
286+
if ((*road)->getOwner() == Owner)
287+
return true;
288+
}
285289
}
286290

287291
return false;
@@ -292,7 +296,7 @@ bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner){
292296
* Runs a series of checks to make sure the road can be placed
293297
* new Roads must be in bounds, unique, and connected to an existing road or settlement
294298
*/
295-
bool GameBoard::verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner) {
299+
bool GameBoard::verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner) const {
296300
if (outOfBounds(start) || outOfBounds(end))
297301
return false;
298302

@@ -343,19 +347,21 @@ bool GameBoard::buyRoad(Coordinate start, Coordinate end, Player& Owner){
343347
/**
344348
* returns a pointer to the road located at the specified coordinates. Will return NULL if the road is not found
345349
*/
346-
std::shared_ptr<Road> GameBoard::getRoad(Coordinate start, Coordinate end){
347-
std::vector<shared_ptr<Road>> roadVector = roads[start];
348-
for (std::vector<shared_ptr<Road>>::iterator road = roadVector.begin(); road != roadVector.end(); ++road) {
349-
if ((*road)->equals(start, end))
350-
return *road;
350+
const std::shared_ptr<Road> GameBoard::getRoad(Coordinate start, Coordinate end) const {
351+
auto roadVecIt = roads.find(start);
352+
if(roadVecIt != roads.end()) {
353+
for (auto road = roadVecIt->second.begin(); road != roadVecIt->second.end(); road++) {
354+
if ((*road)->equals(start, end))
355+
return *road;
356+
}
351357
}
352358
return NULL;
353359
}
354360

355361
/**
356362
* Parent function for the find longest road traversal. Note that longest path is NP-Hard, so there is no simple algorithm for this.
357363
*/
358-
int GameBoard::FindLongestRoad(Player & owner){
364+
int GameBoard::FindLongestRoad(const Player & owner) const {
359365
int longest_path = 0;
360366
//for each road vertex v on the board
361367
for (auto roadVector = roads.begin(); roadVector != roads.end(); ++roadVector){
@@ -374,12 +380,14 @@ int GameBoard::FindLongestRoad(Player & owner){
374380
}
375381

376382

377-
int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, std::map<Road*, bool>& markedRoads, int length){
383+
int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, const Player & owner, std::map<Coordinate, bool>& marked, std::map<Road*, bool>& markedRoads, int length) const {
378384
marked[curr] = true;
379385
int longest_path = length;
380386
//traverse all the surrounding edges and vertices
381-
std::vector<shared_ptr<Road>> roadVector = roads[curr];
382-
for (std::vector<shared_ptr<Road>>::iterator road = roadVector.begin(); road != roadVector.end(); ++road) {
387+
auto roadVectorIt = roads.find(curr);
388+
if(roadVectorIt != roads.end()) {
389+
auto& roadVector = roadVectorIt->second;
390+
for (auto road = roadVector.begin(); road != roadVector.end(); ++road) {
383391
int temp_longest_path = length;
384392

385393
//if the owner is correct and the road is unmarked
@@ -398,6 +406,7 @@ int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::m
398406

399407
if(temp_longest_path > longest_path)
400408
longest_path = temp_longest_path;
409+
}
401410
}
402411
marked[curr] = false;
403412
return longest_path;

0 commit comments

Comments
 (0)