1111#include " GameVisitor.h"
1212#include " Serialization.h"
1313#include " tinyxml2.h"
14+
15+ #include " CornerPiece.h"
16+
1417#include " City.h"
1518
1619using std::shared_ptr;
@@ -82,7 +85,7 @@ void GameBoard::insertTile(Coordinate location, vector<resourceType>& resources,
8285
8386GameBoard::GameBoard (std::vector<std::unique_ptr<Player>>&& players, const std::map<Coordinate, std::pair<resourceType, int >>& resourceLocations) : players(std::move(players)) {
8487 for (auto & resource : resourceLocations) {
85- resources[resource.first ] = std::unique_ptr<GamePiece >(new ResourceTile (*this , resource.first , resource.second .first , resource.second .second ));
88+ resources[resource.first ] = std::unique_ptr<ResourceTile >(new ResourceTile (*this , resource.first , resource.second .first , resource.second .second ));
8689 }
8790 if (!isValidBoard ()) {
8891 throw std::runtime_error (" Board is invalid." );
@@ -217,12 +220,12 @@ void GameBoard::save(ostream& out) {
217220 out << printer.CStr ();
218221}
219222
220- const map<Coordinate, unique_ptr<GamePiece >>& GameBoard::getResources () const {
223+ const map<Coordinate, unique_ptr<ResourceTile >>& GameBoard::getResources () const {
221224 return resources;
222225}
223226
224227std::vector<Settlement*> GameBoard::GetNeighboringSettlements (
225- Coordinate location) {
228+ Coordinate location) const {
226229 static Coordinate adjacentCoordDiffs[] = { Coordinate (0 , 1 ), Coordinate (1 ,
227230 0 ), Coordinate (1 , -1 ), Coordinate (0 , -1 ), Coordinate (-1 , 0 ),
228231 Coordinate (-1 , 1 ) };
@@ -231,8 +234,8 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
231234 const Coordinate& diff = adjacentCoordDiffs[i];
232235 Coordinate adjacentPoint (location.first + diff.first ,
233236 location.second + diff.second );
234- auto it = resources .find (adjacentPoint);
235- if (it != resources .end ()) {
237+ auto it = corners .find (adjacentPoint);
238+ if (it != corners .end ()) {
236239 GamePiece* piece = it->second .get ();
237240 if (dynamic_cast <Settlement*>(piece)) {
238241 v.push_back (static_cast <Settlement*>(piece));
@@ -242,6 +245,28 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
242245 return v;
243246}
244247
248+ std::vector<CornerPiece*> GameBoard::GetNeighboringCorners (
249+ Coordinate location) const {
250+ static Coordinate adjacentCoordDiffs[] = { Coordinate (0 , 1 ), Coordinate (1 ,
251+ 0 ), Coordinate (1 , -1 ), Coordinate (0 , -1 ), Coordinate (-1 , 0 ),
252+ Coordinate (-1 , 1 ) };
253+ std::vector<CornerPiece*> v;
254+ for (unsigned int i = 0 ; i < 6 ; i++) {
255+ const Coordinate& diff = adjacentCoordDiffs[i];
256+ Coordinate adjacentPoint (location.first + diff.first ,
257+ location.second + diff.second );
258+ auto it = resources.find (adjacentPoint);
259+ if (it != resources.end ()) {
260+ GamePiece* piece = it->second .get ();
261+ if (dynamic_cast <CornerPiece*>(piece)) {
262+ v.push_back (static_cast <CornerPiece*>(piece));
263+ }
264+ }
265+ }
266+ return v;
267+ }
268+
269+
245270/* *
246271 * Checks to make sure the coordinate is within bounds of the board and not a resource tile.
247272 */
@@ -267,6 +292,7 @@ bool GameBoard::roadExists(Coordinate start, Coordinate end) const {
267292/* *
268293 * Checks to make sure the road being placed at a valid point according to the rules
269294 */
295+
270296bool GameBoard::isRoadConnectionPoint (Coordinate point, Player& Owner) const {
271297 // is there a settlement we can build off of
272298 auto cornerIt = corners.find (point);
@@ -290,6 +316,7 @@ bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner) const {
290316
291317 return false ;
292318
319+
293320}
294321
295322/* *
@@ -309,6 +336,18 @@ bool GameBoard::verifyRoadPlacement(Coordinate start, Coordinate end, Player& Ow
309336 return true ;
310337}
311338
339+ void GameBoard::moveRobber (Coordinate newRobber) {
340+
341+ robber = newRobber;
342+
343+ // force trade
344+ }
345+
346+ Coordinate GameBoard::getRobber () const {
347+ return robber;
348+
349+ }
350+
312351/* *
313352 * Places a road at the specified coordinates that will be owned by the given player
314353 * returns true if the road was placed, false otherwise
@@ -412,44 +451,58 @@ int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, const Player & owner,
412451 return longest_path;
413452}
414453
454+
455+
415456void GameBoard::PlaceSettlement (Coordinate location, Player& Owner){
416457 corners[location] = std::unique_ptr<CornerPiece>(new Settlement (*this , location, Owner));
417458}
418459
419460void GameBoard::PlaceCity (Coordinate location, Player& Owner){
420461 corners[location] = std::unique_ptr<CornerPiece>(new City (*this , location, Owner));
462+
421463}
422464
465+ void GameBoard::UpgradeSettlement (Coordinate location){
466+ corners[location] = std::unique_ptr<CornerPiece>(new City (*corners[location])); // TODO test for memory leak
467+ }
468+
469+
470+
423471void GameBoard::accept (GameVisitor& visitor) {
424472 visitor.visit (*this );
473+
474+
425475 // Drawing needs this to happen in this order. Visitors technically should be order-independent, but this was an easy fix at the moment.
426476 // Keep that in mind when modifying this.
427477 for (auto & it : resources) {
428478 it.second ->accept (visitor);
429479 }
430480 for (auto & it : corners) {
431481 it.second ->accept (visitor);
482+
432483 }
433484 for (auto & roadCoordVec : roads) {
434485 for (auto & road : roadCoordVec.second ) {
435- road->accept (visitor);
486+ if (road.get ()) {
487+ road->accept (visitor);
488+ }
436489 }
437490 }
438491 for (auto & it : players) {
439- it->accept (visitor);
492+ if (it.get ()) {
493+ it->accept (visitor);
494+ }
440495 }
441496}
442497
443498bool GameBoard::operator ==(const GameBoard& other) const {
444- if (corners.size () != other.corners .size ()) {
445- return false ;
446- }
447499 for (auto & it : corners) {
448500 auto otherIt = other.corners .find (it.first );
449501 if (otherIt == other.corners .end ()) {
450- return false ; // This location isn't in the other array
451- }
452- if (!(*(it.second ) == *(otherIt->second ))) {
502+ if (it.second .get ()) {
503+ return false ; // This location isn't in the other array
504+ }
505+ } else if (!(*(it.second ) == *(otherIt->second ))) {
453506 return false ;
454507 }
455508 }
@@ -481,12 +534,10 @@ bool GameBoard::operator==(const GameBoard& other) const {
481534 }
482535 }
483536 if (players.size () != other.players .size ()) {
484- std::cout << " sizes differ" << std::endl;
485537 return false ;
486538 }
487539 for (unsigned int i = 0 ; i < players.size (); i++) {
488540 if (!(*(players[i]) == *(other.players [i]))) {
489- std::cout << " player " << i << " differs" << std::endl;
490541 return false ;
491542 }
492543 }
@@ -510,7 +561,7 @@ bool GameBoard::operator==(const GameBoard& other) const {
510561 */
511562void GameBoard::addResource (int x, int y, resourceType res, int val)
512563{
513- this ->resources [Coordinate (x,y)] = std::unique_ptr<GamePiece >(new ResourceTile (*this , Coordinate (x,y), res, val));
564+ this ->resources [Coordinate (x,y)] = std::unique_ptr<ResourceTile >(new ResourceTile (*this , Coordinate (x,y), res, val));
514565}
515566
516567bool GameBoard::isValidBoard () const {
0 commit comments