Skip to content

Commit d5d8d30

Browse files
committed
Fixed resource payout. Made road and settlement buttons require and deduct resources
1 parent 79f2f8d commit d5d8d30

File tree

7 files changed

+100
-27
lines changed

7 files changed

+100
-27
lines changed

include/GameBoard.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ class GameBoard {
101101
void PlaceSettlement(Coordinate location, Player& Owner);
102102
void UpgradeSettlement(Coordinate location);
103103
void UpgradeToWonder(Coordinate location);
104-
105-
104+
106105
bool verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner) const;
107106
bool buyRoad(Coordinate start, Coordinate end, Player& Owner);
108-
107+
108+
bool canPlaceSettlement(const Coordinate& location, const Player& owner);
109+
bool buySettlement(const Coordinate& location, Player& owner);
110+
109111
//void PlaceSettlement(Coordinate location, Player& Owner);
110112
void PlaceCity(Coordinate location, Player& Owner);
111113
void PlaceWonder(Coordinate location, Player& Owner);

include/Player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Player {
7171
void setLongestRoad(bool);
7272
void setLongestRoadSize(int);
7373

74-
std::tuple<float, float, float> getColor();
74+
std::tuple<float, float, float> getColor() const;
7575

7676
int getVictoryPointsWithoutCards();
7777
int getVictoryPointCards();

src/GameBoard.cpp

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
491491
bool 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
*/
550558
bool 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.

src/GameController.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,18 @@ int GameController::getClickHistorySize(){
131131
return clickHistory.size();
132132
}
133133

134-
135-
136-
134+
void printPlayerInfo(const Player& player) {
135+
auto color = player.getColor();
136+
std::cout << player.getName() << "'s turn. (" << std::get<0>(color) << ", " << std::get<1>(color) << ", " << std::get<2>(color) <<")" << std::endl;
137+
std::cout << "Wood: " << player.getWood() << ", Brick: " << player.getBrick() << ", Ore: " << player.getOre() << ", Wheat: " << player.getWheat() << ", Wool: " << player.getWool() << std::endl;
138+
}
137139

138140
/**
139141
* calls a function to advance turn, check for victory and roll dice
140142
*/
141143
bool GameController::nextTurn(ScreenCoordinate) {
142144
model.endTurn();
145+
printPlayerInfo(model.getCurrentPlayer());
143146
return true;
144147
}
145148

@@ -150,14 +153,15 @@ bool GameController::nextTurn(ScreenCoordinate) {
150153
* @return Whether this event was handled by this element. Always true.
151154
*/
152155
bool GameController::handleBoardEvent(ScreenCoordinate screenCoord) {
156+
printPlayerInfo(model.getCurrentPlayer());
153157
auto coord = screenToCoord(screenCoord);
154-
158+
155159
switch (getState()){
156160
case BUILDROAD:
157161
if(!hasClickHistory()) {
158162
storeClick(coord);
159163
} else {
160-
if (model.PlaceRoad(getLastClick(), coord, *model.getPlayers()[0]));
164+
if (model.buyRoad(getLastClick(), coord, model.getCurrentPlayer()));
161165
{
162166
popState();
163167
}
@@ -196,8 +200,8 @@ bool GameController::handleBoardEvent(ScreenCoordinate screenCoord) {
196200
popState();
197201
break;
198202
case BUILDSETTLEMENT:
199-
std::cout << "BUILDSETTLEMENT\n";
200-
model.PlaceSettlement(coord, *model.getPlayers()[0]);
203+
std::cout << "attempting to buy a settlement" << std::endl;
204+
model.buySettlement(coord, model.getCurrentPlayer());
201205
popState();
202206
break;
203207
default:

src/Player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Player::~Player() {
111111

112112
}
113113

114-
std::tuple<float, float, float> Player::getColor(){
114+
std::tuple<float, float, float> Player::getColor() const {
115115
return color;
116116
}
117117

src/Renderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pair<float, float> coordToScreen(const Coordinate& coord) {
145145
using std::sin;
146146
using std::cos;
147147
// TODO not magic numbers
148-
static const float xscale = 9.f / 16.f / 7.f;
148+
static const float xscale = 9.f / 16.f / 6.f;
149149
static const float yscale = 0.1f;
150150
static const float angle = M_PI / 3.f;
151151
float x = .25f + (xscale * coord.first) + ((yscale * coord.second) * cos(angle));
@@ -159,7 +159,7 @@ pair<float, float> coordToScreen(const Coordinate& coord) {
159159
* @return The game coordinate.
160160
*/
161161
Coordinate screenToCoord(const pair<float, float>& screen) {
162-
static const float xscale = 9.f / 16.f / 7.f;
162+
static const float xscale = 9.f / 16.f / 6.f;
163163
static const float yscale = 0.1f;
164164
static const float angle = M_PI / 3.f;
165165
Coordinate ret;

tests/test_GameBoard.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ TEST(payout_simple) {
175175
test_board.getResourceTile(Coordinate(0,1)).Payout();
176176

177177

178-
CHECK(!(test_player.getWheat() || test_player.getWood() ||
179-
test_player.getOre() || test_player.getBrick() || test_player.getWool()));
178+
CHECK(test_player.getWheat() || test_player.getWood() ||
179+
test_player.getOre() || test_player.getBrick() || test_player.getWool());
180180
}
181181

182182
TEST(upgrade_simple){

0 commit comments

Comments
 (0)