@@ -410,8 +410,8 @@ std::vector<CornerPiece*> GameBoard::GetNeighboringCorners(
410410 const Coordinate& diff = adjacentCoordDiffs[i];
411411 Coordinate adjacentPoint (location.first + diff.first ,
412412 location.second + diff.second );
413- auto it = resources .find (adjacentPoint);
414- if (it != resources .end ()) {
413+ auto it = corners .find (adjacentPoint);
414+ if (it != corners .end ()) {
415415 GamePiece* piece = it->second .get ();
416416 if (dynamic_cast <CornerPiece*>(piece)) {
417417 v.push_back (static_cast <CornerPiece*>(piece));
@@ -489,18 +489,26 @@ bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner) const {
489489 * @return If the road can be placed at the locations by the player.
490490 */
491491bool GameBoard::verifyRoadPlacement (Coordinate start, Coordinate end, Player& Owner) const {
492- if (outOfBounds (start) || outOfBounds (end))
492+ if (outOfBounds (start) || outOfBounds (end)) {
493+ std::cout << " out of bounds" << std::endl;
493494 return false ;
494-
495- if (roadExists (start, end))
495+ }
496+
497+ if (roadExists (start, end)) {
498+ std::cout << " road exists" << std::endl;
496499 return false ;
497-
498- if (!isRoadConnectionPoint (start, Owner) && !isRoadConnectionPoint (end, Owner)) // need to XOR
500+ }
501+
502+ if (!isRoadConnectionPoint (start, Owner) && !isRoadConnectionPoint (end, Owner)) { // need to XOR
503+ std::cout << " not a road connection point" << std::endl;
499504 return false ;
500-
501- if (!Road::isValidRoad (start, end))
505+ }
506+
507+ if (!Road::isValidRoad (start, end)) {
508+ std::cout << " not a valid road" << std::endl;
502509 return false ;
503-
510+ }
511+
504512 return true ;
505513}
506514
@@ -548,8 +556,12 @@ Coordinate GameBoard::getRobber() const {
548556 * @returns True if the road was placed, false otherwise
549557 */
550558bool GameBoard::PlaceRoad (Coordinate start, Coordinate end, Player& Owner) {
551- if (!verifyRoadPlacement (start, end, Owner))
559+ if (!verifyRoadPlacement (start, end, Owner)) {
560+ std::cout << " invalid road placement" << std::endl;
552561 return false ;
562+ }
563+
564+ std::cout << " passed verify" << std::endl;
553565
554566 std::shared_ptr<Road> newRoad;
555567 try {
@@ -600,6 +612,7 @@ bool GameBoard::buyRoad(Coordinate start, Coordinate end, Player& Owner){
600612 Owner.buyRoad ();
601613 return true ;
602614 }
615+ std::cout << " failed to buy for some reason" << std::endl;
603616 return false ;
604617}
605618
@@ -743,6 +756,60 @@ void GameBoard::updateLargestArmyPlayer(){
743756
744757}
745758
759+ /* *
760+ * Whether a player can place a settlement at a location.
761+ * @param location The place to put the settlement.
762+ * @param owner The player placing the settlement.
763+ * @return If the location is a valid place to put a settlement.
764+ */
765+ bool GameBoard::canPlaceSettlement (const Coordinate& location, const Player& owner) {
766+ // Don't place this on top of a resource
767+ if (resources.find (location) != resources.end ()) {
768+ std::cout << " can't put settlements on top of resource tiles" << std::endl;
769+ return false ;
770+ }
771+ // Don't place this on top of another settlement
772+ if (corners.find (location) != corners.end ()) {
773+ std::cout << " can't put a settlement on top of another corner piece" << std::endl;
774+ return false ;
775+ }
776+ // Don't place this off the map
777+ if (outOfBounds (location)) {
778+ std::cout << " this is out of bounds" << std::endl;
779+ return false ;
780+ }
781+ // Can't have a settlement next to another settlement.
782+ if (GetNeighboringCorners (location).size () > 0 ) {
783+ std::cout << " there's an adjacent corner piece" << std::endl;
784+ return false ;
785+ }
786+ for (auto road : getRoads (location)) {
787+ if (road->getOwner () == owner) {
788+ // Player has a connecting road
789+ return true ;
790+ }
791+ }
792+ std::cout << " there are no connecting roads" << std::endl;
793+ // Player has no connecting roads
794+ return false ;
795+ }
796+
797+ /* *
798+ * Buy a settlement if possible.
799+ * @param location The location to place the settlement.
800+ * @param owner The player buying the settlement.
801+ * @return If placing the settlement was a success.
802+ */
803+ bool GameBoard::buySettlement (const Coordinate& location, Player& owner) {
804+ if (canPlaceSettlement (location, owner) && owner.canBuySettlement ()) {
805+ if (!owner.buySettlement ()) {
806+ std::cout << " wat" << std::endl;
807+ return false ;
808+ }
809+ PlaceSettlement (location, owner);
810+ }
811+ return false ;
812+ }
746813
747814/* *
748815 * Place a settlement on the board.
0 commit comments