Skip to content

Commit 751a265

Browse files
committed
Shortened/simplified GameBoard::(outOfBounds, PlaceRoad, roadExists)
1 parent 5855a37 commit 751a265

File tree

1 file changed

+12
-53
lines changed

1 file changed

+12
-53
lines changed

src/GameBoard.cpp

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -243,48 +243,15 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
243243
}
244244

245245
/**
246-
* Checks to make sure the coordinate is within bounds of the board
246+
* Checks to make sure the coordinate is within bounds of the board and not a resource tile.
247247
*/
248248
bool GameBoard::outOfBounds(const Coordinate& coord) {
249-
/**
250-
* This code is embarrassing, but I couldn't really figure out how to easily check for out of bounds
251-
* I'm sure there is a simple algebraic function that does it, but I went for the hacky way.
252-
*
253-
* Discussed that we can just do a find in the map, and if it's not found then it's out of bounds
254-
*/
255-
256-
257-
258-
switch (coord.second) {
259-
case 0:
260-
return !(coord.first >= 0 && coord.first <= 4);
261-
break;
262-
case 1:
263-
return !(coord.first >= -2 && coord.first <= 5);
264-
break;
265-
case 2:
266-
return !(coord.first >= -3 && coord.first <= 5);
267-
break;
268-
case 3:
269-
return !(coord.first >= -3 && coord.first <= 4);
270-
break;
271-
case 4:
272-
return !(coord.first >= -4 && coord.first <= 4);
273-
break;
274-
case 5:
275-
return !(coord.first >= -4 && coord.first <= 3);
276-
break;
277-
case 6:
278-
return !(coord.first >= -5 && coord.first <= 3);
279-
break;
280-
case 7:
281-
return !(coord.first >= -5 && coord.first <= 2);
282-
break;
283-
case 8:
284-
return !(coord.first >= -4 && coord.first <= 0);
285-
break;
286-
default:
287-
break;
249+
//All valid coordinates are adjacent to resource tiles.
250+
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
251+
for(auto& diff : adjacentCoordDiffs) {
252+
if(resources.find(Coordinate{coord.first + diff.first, coord.second + diff.second}) != resources.end()) {
253+
return false;
254+
}
288255
}
289256
return true;
290257
}
@@ -293,10 +260,7 @@ bool GameBoard::outOfBounds(const Coordinate& coord) {
293260
* Checks to make sure the road doesn't already exist. If it does, then we don't want to add it again
294261
*/
295262
bool GameBoard::roadExists(Coordinate start, Coordinate end) {
296-
std::shared_ptr<Road> isRoad = getRoad(start, end);
297-
if (isRoad == NULL)
298-
return false;
299-
return true;
263+
return bool(getRoad(start, end)); // shared_ptr can convert to bool
300264
}
301265

302266

@@ -356,16 +320,11 @@ bool GameBoard::PlaceRoad(Coordinate start, Coordinate end, Player& Owner) {
356320
//Coordinates did not meet the criteria for a valid road
357321
return false;
358322
}
359-
360-
std::vector<shared_ptr<Road>> roadVector = roads[start];
361-
roadVector.push_back(newRoad);
362-
roads[start] = roadVector;
363-
roadVector = roads[end];
364-
roadVector.push_back(newRoad);
365-
roads[end] = roadVector;
323+
324+
roads[start].push_back(newRoad);
325+
roads[end].push_back(newRoad);
326+
366327
return true;
367-
368-
369328
}
370329

371330
/**

0 commit comments

Comments
 (0)