Skip to content

Commit 4f083e9

Browse files
committed
Changed GetNeighbors to GetNeighboringSettlements (I believe that was the intended functionality) and implemented it.
1 parent 399f599 commit 4f083e9

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

include/GameBoard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GameBoard {
2323
GameBoard(GameBoard&) = delete;
2424
~GameBoard();
2525
GameBoard& operator=(GameBoard&) = delete;
26-
std::vector<GamePiece> GetNeighbors(Coordinate location);
26+
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
2727

2828
void PlaceSettlement(Coordinate location, Player& Owner);
2929

src/GameBoard.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,24 @@ GameBoard::GameBoard() {
1414
GameBoard::~GameBoard() {
1515

1616
}
17-
/*
18-
std::vector<GamePiece> GameBoard::GetNeighbors(Coordinate location){
19-
std::vector<GamePiece> v;
20-
for (int i = -1; i < 1; i++)
21-
{
22-
Settlement* sett = dynamic_cast<Settlement*>(&(corners[Coordinate(location.first + i, location.second)]));
23-
if (sett != 0)
24-
{
25-
v.insert(sett)
26-
}
27-
Settlement sett = dynamic_cast<Settlement*>(corners[Coordinate(location.first + i, location.second - i)]);
28-
if (sett != 0)
29-
{
30-
v.insert(sett) }
31-
Settlement sett = dynamic_cast<Settlement*>(corners[Coordinate(location.first, location.second - i)]);
32-
if (sett != 0)
33-
{
34-
v.insert(sett)
17+
18+
std::vector<Settlement*> GameBoard::GetNeighboringSettlements(Coordinate location) {
19+
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
20+
std::vector<Settlement*> v;
21+
for(unsigned int i = 0; i < 6; i++) {
22+
const Coordinate& diff = adjacentCoordDiffs[i];
23+
Coordinate adjacentPoint(location.first + diff.first, location.second + diff.second);
24+
auto it = resources.find(adjacentPoint);
25+
if(it != resources.end()) {
26+
GamePiece* piece = it->second.get();
27+
if(dynamic_cast<Settlement*>(piece)) {
28+
v.push_back(static_cast<Settlement*>(piece));
29+
}
3530
}
3631
}
32+
return v;
3733
}
3834

39-
&/
40-
4135
/* initialize board with a set of resources. Right now, just creates a static tile arrangement to test
4236
URL: http://images.fanpop.com/images/image_uploads/Differents-Boards-settlers-of-catan-521934_1157_768.jpg*/
4337

0 commit comments

Comments
 (0)