Skip to content

Commit ecbdbb7

Browse files
committed
Added button to build cities
1 parent d5d8d30 commit ecbdbb7

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

include/GameBoard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ class GameBoard {
108108
bool canPlaceSettlement(const Coordinate& location, const Player& owner);
109109
bool buySettlement(const Coordinate& location, Player& owner);
110110

111+
bool canUpgradeSettlement(Coordinate location, const Player& owner) const;
112+
bool buyUpgradeOnSettlement(Coordinate location, Player& owner);
113+
111114
//void PlaceSettlement(Coordinate location, Player& Owner);
112115
void PlaceCity(Coordinate location, Player& Owner);
113116
void PlaceWonder(Coordinate location, Player& Owner);

include/GameController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GameView;
1212
class Player;
1313

1414

15-
enum ControlState {BASESTATE, MODALSTATE, BUILDROAD, BUILDSETTLEMENT, ROBBER,
15+
enum ControlState {BASESTATE, MODALSTATE, BUILDROAD, BUILDSETTLEMENT, BUILDCITY, ROBBER,
1616
VICTORYPOINT_DEVCARD, BUILDROAD_DEVCARD, KNIGHT_DEVCARD, YEAROFPLENTY_DEVCARD, MONOPOLY_DEVCARD};
1717

1818

@@ -38,6 +38,7 @@ class GameController {
3838
bool handleBoardEvent(ScreenCoordinate);
3939
bool handleRoadButtonEvent(ScreenCoordinate);
4040
bool handleSettlementButtonEvent(ScreenCoordinate);
41+
bool handleCityButtonEvent(ScreenCoordinate);
4142
bool handleRoadCardButtonEvent(ScreenCoordinate);
4243
bool handleKnightCardButtonEvent(ScreenCoordinate);
4344
bool handleYearOfPlentyCardButtonEvent(ScreenCoordinate);

src/GameBoard.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ bool GameBoard::buySettlement(const Coordinate& location, Player& owner) {
807807
return false;
808808
}
809809
PlaceSettlement(location, owner);
810+
return true;
810811
}
811812
return false;
812813
}
@@ -822,6 +823,43 @@ void GameBoard::PlaceSettlement(Coordinate location, Player& Owner){
822823

823824
}
824825

826+
/**
827+
* Whether a settlement at a location can be upgraded to a city.
828+
*/
829+
bool GameBoard::canUpgradeSettlement(Coordinate location, const Player& owner) const {
830+
auto it = corners.find(location);
831+
if(it == corners.end()) {
832+
std::cout << "there's nothing there" << std::endl;
833+
return false;
834+
}
835+
if(!it->second) {
836+
std::cout << "null ptr there" << std::endl;
837+
return false;
838+
}
839+
if(!(it->second->getOwner() == owner)) {
840+
std::cout << "wrong owner" << std::endl;
841+
return false;
842+
}
843+
if(dynamic_cast<const Settlement*>(it->second.get()) == 0) {
844+
std::cout << "this isn't a settlement" << std::endl;
845+
return false;
846+
}
847+
return true;
848+
}
849+
850+
bool GameBoard::buyUpgradeOnSettlement(Coordinate location, Player& owner) {
851+
if(canUpgradeSettlement(location, owner) && owner.canBuyCity()) {
852+
if(!owner.buyCity()) {
853+
std::cout << "wat" << std::endl;
854+
return false;
855+
}
856+
UpgradeSettlement(location);
857+
return true;
858+
}
859+
std::cout << "failed for some reason" << std::endl;
860+
return false;
861+
}
862+
825863
/**
826864
* Place a city on the board.
827865
* @param location Where to place it on the board.

src/GameController.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
2323
auto font = getGraphicsConfig()["font.path"];
2424
auto fontSize = getGraphicsConfig()["font.size"];
2525

26-
view.addElement(makeViewButtonText(std::bind(&GameController::nextTurn, this, _1), {{0, 0.2}, {0.1, 0.3}}, font, fontSize, "Turn"));
2726
view.addElement(makeViewButtonText(std::bind(&GameController::handleRoadButtonEvent, this, _1), {{0, 0}, {0.1, 0.1}}, font, fontSize, "Road"));
2827
view.addElement(makeViewButtonText(std::bind(&GameController::handleSettlementButtonEvent, this, _1), {{0, 0.1}, {0.1, 0.2}}, font, fontSize, "Stlm"));
28+
view.addElement(makeViewButtonText(std::bind(&GameController::handleCityButtonEvent, this, _1), {{0, 0.2}, {0.1, 0.3}}, font, fontSize, "City"));
29+
view.addElement(makeViewButtonText(std::bind(&GameController::nextTurn, this, _1), {{0, 0.3}, {0.1, 0.4}}, font, fontSize, "Turn"));
2930

3031
auto playerTopY = 0.9;
3132
for(auto i = 0; i < model.getNoOfPlayers(); i++) {
@@ -204,6 +205,11 @@ bool GameController::handleBoardEvent(ScreenCoordinate screenCoord) {
204205
model.buySettlement(coord, model.getCurrentPlayer());
205206
popState();
206207
break;
208+
case BUILDCITY:
209+
std::cout << "attempting to build a city" << std::endl;
210+
model.buyUpgradeOnSettlement(coord, model.getCurrentPlayer());
211+
popState();
212+
break;
207213
default:
208214
break;
209215
}
@@ -238,7 +244,7 @@ bool GameController::handleRoadButtonEvent(ScreenCoordinate coord) {
238244
}
239245

240246
/**
241-
* Handles a click on the "create settlement" button. Changes the internal state to indicate the user is going to be making roads on the board.
247+
* Handles a click on the "create settlement" button. Changes the internal state to indicate the user is going to be making settlements on the board.
242248
* @param coord The place the user clicked on screen.
243249
* @return Whether this event was handled by this element. Always true.
244250
*/
@@ -250,6 +256,18 @@ bool GameController::handleSettlementButtonEvent(ScreenCoordinate coord) {
250256
return true;
251257
}
252258

259+
/**
260+
* Handles a click on the "create city" button. Changes the internal state to indicate the user is going to be upgrading settlements to cities on the board.
261+
* @param coord The place the user clicked on screen.
262+
* @return Whether this event was handled by this element. Always true.
263+
*/
264+
bool GameController::handleCityButtonEvent(ScreenCoordinate coord) {
265+
if(getState() == BASESTATE) {
266+
pushState(BUILDCITY);
267+
}
268+
return true;
269+
}
270+
253271

254272
/**
255273
* Handles a click on the road Building Card button. This changes the control state to indicate the user is going to be building roads on the board.

src/Player.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ bool Player::canBuyCity(){
214214
bool Player::buyCity(){
215215
if(canBuyCity()){
216216
addMultiple(0,0,-3,-2,0);
217+
return true;
217218
}
218219
return false;
219220
}

0 commit comments

Comments
 (0)