Skip to content

Commit f2d46eb

Browse files
committed
Changed some Road methods to const, changed owner pointer to reference
1 parent b49effd commit f2d46eb

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

include/Road.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ class GameVisitor;
1212

1313
class Road {
1414
private:
15-
bool checkRoad();
16-
15+
Player& owner;
1716
Coordinate start;
1817
Coordinate end;
18+
19+
bool checkRoad();
1920
public:
2021
Road(Coordinate start, Coordinate end, Player& Owner);
2122
Road(Road&) = delete;
@@ -29,9 +30,7 @@ class Road {
2930
bool equals(const Coordinate& otherStart, const Coordinate& otherEnd);
3031

3132
Player& getOwner();
32-
Player& getOwner() const;
33-
34-
Player* owner;
33+
const Player& getOwner() const;
3534

3635
virtual void accept(GameVisitor& visitor);
3736
bool operator==(const Road&) const;

src/GameBoard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, const Player & owner,
391391
int temp_longest_path = length;
392392

393393
//if the owner is correct and the road is unmarked
394-
if ( !markedRoads[road->get()] && (*road)->owner->getName() == owner.getName()){
394+
if ( !markedRoads[road->get()] && (*road)->getOwner().getName() == owner.getName()){
395395

396396
temp_longest_path++;
397397
markedRoads[road->get()] = true;

src/Road.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
#include "GameVisitor.h"
44

5-
Road::Road(Coordinate start, Coordinate end, Player& Owner) {
5+
Road::Road(Coordinate start, Coordinate end, Player& Owner) : owner(Owner) {
66
if(start < end) {
77
this->start = start;
88
this->end = end;
99
} else {
1010
this->end = start;
1111
this->start = end;
1212
}
13-
owner = &Owner;
1413

1514
//If the input is bad, throw an exception so bad roads won't be built
1615
if(!checkRoad()){
1716
throw -1;
1817
}
19-
20-
2118
}
2219

2320
/**
@@ -42,7 +39,7 @@ void Road::accept(GameVisitor& visitor) {
4239
bool Road::operator==(const Road& other) const {
4340
return getStart() == other.getStart() &&
4441
getEnd() == other.getEnd() &&
45-
owner->getName() == other.owner->getName();
42+
owner.getName() == other.owner.getName();
4643
}
4744

4845
/**
@@ -78,9 +75,9 @@ bool Road::equals(const Coordinate& otherStart, const Coordinate& otherEnd){
7875
}
7976

8077
Player& Road::getOwner() {
81-
return *owner;
78+
return owner;
8279
}
8380

84-
Player& Road::getOwner() const {
85-
return *owner;
81+
const Player& Road::getOwner() const {
82+
return owner;
8683
}

0 commit comments

Comments
 (0)