Skip to content

Commit 84946ca

Browse files
committed
Merge branch 'master' into tradeWithBank
Conflicts: src/GameController.cpp
2 parents fe003db + ea4937b commit 84946ca

File tree

8 files changed

+158
-38
lines changed

8 files changed

+158
-38
lines changed

include/GameBoard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class GameBoard {
120120
bool canUpgradeSettlement(Coordinate location, const Player& owner) const;
121121
bool buyUpgradeOnSettlement(Coordinate location, Player& owner);
122122

123+
bool canUpgradeToWonder(Coordinate location, const Player& owner) const;
124+
bool buyUpgradeOnWonder(Coordinate location, Player& owner);
125+
123126
//void PlaceSettlement(Coordinate location, Player& Owner);
124127
void PlaceCity(Coordinate location, Player& Owner);
125128
void PlaceWonder(Coordinate location, Player& Owner);

include/GameController.h

Lines changed: 5 additions & 2 deletions
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, BUILDCITY, ROBBER,
15+
enum ControlState {BASESTATE, MODALSTATE, BUILDROAD, BUILDSETTLEMENT, BUILDCITY, BUILDWONDER, ROBBER,
1616
VICTORYPOINT_DEVCARD, BUILDROAD_DEVCARD, KNIGHT_DEVCARD, YEAROFPLENTY_DEVCARD, MONOPOLY_DEVCARD};
1717

1818

@@ -39,6 +39,7 @@ class GameController {
3939
bool handleRoadButtonEvent(ScreenCoordinate);
4040
bool handleSettlementButtonEvent(ScreenCoordinate);
4141
bool handleCityButtonEvent(ScreenCoordinate);
42+
bool handleWonderButtonEvent(ScreenCoordinate);
4243
bool handleRoadCardButtonEvent(ScreenCoordinate);
4344

4445
bool handleBuyDevelopmentCardButtonEvent(ScreenCoordinate);
@@ -57,13 +58,15 @@ class GameController {
5758

5859
bool handleConfirmRoadCard(ScreenCoordinate);
5960
bool handleCancelDialogueEvent(ScreenCoordinate);
61+
62+
bool viewCardTotals(ScreenCoordinate coord);
6063

6164
void pushState(ControlState);
6265
ControlState getState();
6366
ControlState popState();
6467
void storeClick(Coordinate clickCoordinate);
6568
Coordinate getLastClick();
66-
Coordinate getPastClick(int howLongAgo);
69+
Coordinate getPastClick(unsigned int howLongAgo);
6770
void clearClickHistory();
6871
bool hasClickHistory();
6972
int getClickHistorySize();

include/GameView.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ class GameView {
5252
std::vector<ScreenCoordinate> pointsOfInterest;
5353

5454
void highlightPoint(ScreenCoordinate & coord);
55-
void drawCardCount(std::string font, int fontSize);
56-
void drawResourceCount(std::string font, int fontSize);
5755

5856
std::string controlStateText;
5957

6058
GameView(const GameView& o) = delete;
6159
GameView& operator=(const GameView& o) = delete;
60+
6261
public:
6362
GameView(GameBoard&);
6463
~GameView();
@@ -78,6 +77,10 @@ class GameView {
7877
std::unique_ptr<ViewElement> removeElement(int priority);
7978
std::unique_ptr<ViewElement> removeElement(const ViewElement*);
8079
std::unique_ptr<ViewElement> removeElement(const ViewElement&);
80+
81+
void drawCardCount(std::string font, int fontSize);
82+
void drawResourceCount(std::string font, int fontSize);
83+
bool showTotals;
8184
};
8285

8386
/**

src/GameBoard.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,43 @@ bool GameBoard::buyUpgradeOnSettlement(Coordinate location, Player& owner) {
875875
return false;
876876
}
877877

878+
/**
879+
* Whether a settlement/city at a location can be upgraded to a wonder.
880+
*/
881+
bool GameBoard::canUpgradeToWonder(Coordinate location, const Player& owner) const {
882+
auto it = corners.find(location);
883+
if(it == corners.end()) {
884+
std::cout << "there's nothing there" << std::endl;
885+
return false;
886+
}
887+
if(!it->second) {
888+
std::cout << "null ptr there" << std::endl;
889+
return false;
890+
}
891+
if(!(it->second->getOwner() == owner)) {
892+
std::cout << "wrong owner" << std::endl;
893+
return false;
894+
}
895+
if(dynamic_cast<const Settlement*>(it->second.get()) == 0 && dynamic_cast<const City*>(it->second.get()) == 0) {
896+
std::cout << "this isn't a settlement or city" << std::endl;
897+
return false;
898+
}
899+
return true;
900+
}
901+
902+
bool GameBoard::buyUpgradeOnWonder(Coordinate location, Player& owner) {
903+
if(canUpgradeToWonder(location, owner) && owner.canBuyCity()) {
904+
if(!owner.buyWonder()) {
905+
std::cout << "wat" << std::endl;
906+
return false;
907+
}
908+
UpgradeToWonder(location);
909+
return true;
910+
}
911+
std::cout << "failed for some reason" << std::endl;
912+
return false;
913+
}
914+
878915
/**
879916
* Place a city on the board.
880917
* @param location Where to place it on the board.

src/GameController.cpp

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
2424
view.addElement(makeViewButtonText(std::bind(&GameController::handleRoadButtonEvent, this, _1), {{0, 0}, {0.1, 0.10}}, font, fontSize, "Road |"));
2525
view.addElement(makeViewButtonText(std::bind(&GameController::handleCityButtonEvent, this, _1), {{0.10, 0.0}, {0.20, 0.1}}, font, fontSize, "City |"));
2626
view.addElement(makeViewButtonText(std::bind(&GameController::handleSettlementButtonEvent, this, _1), {{0.20, 0.0}, {0.33, 0.1}}, font, fontSize, "Settlement"));
27+
view.addElement(makeViewButtonText(std::bind(&GameController::handleWonderButtonEvent, this, _1), {{0.55, 0.0}, {0.65, 0.1}}, font, fontSize, "|Wonder"));
2728
view.addElement(makeViewButtonText(std::bind(&GameController::nextTurn, this, _1), {{0, 0.3}, {0.1, 0.4}}, font, fontSize, "End Turn"));
2829

2930
auto playerTopY = 0.82;
@@ -43,15 +44,16 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
4344
view.addElement(makeViewButtonText(std::bind(&GameController::handleYearOfPlentyCardButtonEvent, this, _1), {{0.85, 0.10}, {0.97, 0.15}}, font, fontSize, "Year of Plenty "));
4445
view.addElement(makeViewButtonText(std::bind(&GameController::handleMonopolyCardButtonEvent, this, _1), {{0.85, 0.15}, {0.97, 0.20}}, font, fontSize, "Monopoly "));
4546
view.addElement(makeViewButtonText(std::bind(&GameController::handleVictoryPointCardButtonEvent, this, _1), {{0.85, 0.20}, {0.97, 0.25}}, font, fontSize, "Victory Point "));
46-
47-
view.addElement(makeViewButtonText(std::bind(&GameController::handleWoodButtonEvent, this, _1), {{.85, .35}, {.97, .40}}, font, fontSize, "Wood "));
48-
view.addElement(makeViewButtonText(std::bind(&GameController::handleSheepButtonEvent, this, _1), {{.85, .40}, {.97, .45}}, font, fontSize, "Sheep "));
49-
view.addElement(makeViewButtonText(std::bind(&GameController::handleOreButtonEvent, this, _1), {{.85, .45}, {.97, .50}}, font, fontSize, "Ore "));
50-
view.addElement(makeViewButtonText(std::bind(&GameController::handleBrickButtonEvent, this, _1), {{.85, .50}, {.97, .55}}, font, fontSize, "Brick "));
51-
view.addElement(makeViewButtonText(std::bind(&GameController::handleWheatButtonEvent, this, _1), {{.85, .55}, {.97, .60}}, font, fontSize, "Wheat "));
5247

48+
view.addElement(makeViewButtonText(std::bind(&GameController::handleWoodButtonEvent, this, _1), {{.85, .30}, {.97, .35}}, font, fontSize, "Wood "));
49+
view.addElement(makeViewButtonText(std::bind(&GameController::handleSheepButtonEvent, this, _1), {{.85, .35}, {.97, .40}}, font, fontSize, "Sheep "));
50+
view.addElement(makeViewButtonText(std::bind(&GameController::handleOreButtonEvent, this, _1), {{.85, .40}, {.97, .45}}, font, fontSize, "Ore "));
51+
view.addElement(makeViewButtonText(std::bind(&GameController::handleBrickButtonEvent, this, _1), {{.85, .45}, {.97, .50}}, font, fontSize, "Brick "));
52+
view.addElement(makeViewButtonText(std::bind(&GameController::handleWheatButtonEvent, this, _1), {{.85, .50}, {.97, .55}}, font, fontSize, "Wheat "));
53+
54+
view.addElement(makeViewButtonText(std::bind(&GameController::viewCardTotals, this, _1), {{.85, .55}, {.97, .60}}, font, fontSize, "Show Totals"));
55+
5356
view.addElement(100, makeViewButton(std::bind(&GameController::handleBoardEvent, this, _1), {{0, 0}, {1, 1}}));
54-
5557
stateStack.push_back(BASESTATE);
5658
}
5759

@@ -109,7 +111,7 @@ Coordinate GameController::getLastClick(){
109111
* Gets a click from param clicks ago
110112
* @ param an integer
111113
*/
112-
Coordinate GameController::getPastClick(int howLongAgo){
114+
Coordinate GameController::getPastClick(unsigned int howLongAgo){
113115
if (howLongAgo < clickHistory.size()){
114116
return clickHistory[clickHistory.size() - 1 - howLongAgo];
115117
}
@@ -138,20 +140,27 @@ int GameController::getClickHistorySize(){
138140
return clickHistory.size();
139141
}
140142

141-
void printPlayerInfo(const Player& player) {
143+
/**
144+
* Now that the GUI has this information implemented, I am returning before this function prints. To use this function again simply remove the return
145+
*/
146+
void printPlayerInfo(const Player& player)
147+
{
148+
return;
142149
auto color = player.getColor();
143150
std::cout << player.getName() << "'s turn. (" << std::get<0>(color) << ", " << std::get<1>(color) << ", " << std::get<2>(color) <<")" << std::endl;
144151
std::cout << "Wood: " << player.getWood() << ", Brick: " << player.getBrick() << ", Ore: " << player.getOre() << ", Wheat: " << player.getWheat() << ", Wool: " << player.getWool() << std::endl;
145152
}
146153

147154
/**
148-
* calls a function to advance turn, check for victory and roll dice
155+
* calls a function to advance turn, hide resource and development cards, check for victory, and roll dice
149156
*/
150157
bool GameController::nextTurn(ScreenCoordinate) {
151158
if(getState() != BASESTATE){
152159
return false;
153160
}
154-
161+
162+
view.showTotals = false;
163+
155164
model.endTurn();
156165
if (model.getDice().getFirst() + model.getDice().getSecond() == 7)
157166
{
@@ -223,6 +232,11 @@ bool GameController::handleBoardEvent(ScreenCoordinate screenCoord) {
223232
model.buyUpgradeOnSettlement(coord, model.getCurrentPlayer());
224233
handleCancelButtonEvent(screenCoord);
225234
break;
235+
case BUILDWONDER:
236+
std::cout << "attempting to build a wonder" << std::endl;
237+
model.buyUpgradeOnWonder(coord, model.getCurrentPlayer());
238+
handleCancelButtonEvent(screenCoord);
239+
break;
226240
default:
227241
break;
228242
}
@@ -312,6 +326,21 @@ bool GameController::handleCityButtonEvent(ScreenCoordinate coord) {
312326
return true;
313327
}
314328

329+
/**
330+
* Handles a click on the "create wonder" button. Changes the internal state to indicate the user is going to be upgrading settlements/cities on the board.
331+
* @param coord The place the user clicked on screen.
332+
* @return Whether this event was handled by this element. Always true.
333+
*/
334+
bool GameController::handleWonderButtonEvent(ScreenCoordinate coord) {
335+
if(getState() != BASESTATE) {
336+
return false;
337+
}
338+
339+
view.setControlStateText("Click on a settlement/city to upgrade to wonder. (5 of ea rsc)");
340+
pushState(BUILDWONDER);
341+
return true;
342+
}
343+
315344

316345
/**
317346
* 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.
@@ -413,6 +442,19 @@ bool GameController::handleVictoryPointCardButtonEvent(ScreenCoordinate coord){
413442
return true;
414443
}
415444

445+
/**
446+
* Makes the development and resource card totals visible
447+
*/
448+
bool GameController::viewCardTotals(ScreenCoordinate coord)
449+
{
450+
auto font = getGraphicsConfig()["font.path"];
451+
auto fontSize = getGraphicsConfig()["font.size"];
452+
view.showTotals = !view.showTotals;
453+
view.drawCardCount(font, fontSize);
454+
view.drawResourceCount(font, fontSize);
455+
return true;
456+
}
457+
416458

417459
template<int size>
418460
auto negativeArr(std::array<int, size> arr) -> std::array<int, size> {
@@ -473,7 +515,7 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
473515
return handleCancelButtonEvent(coord);
474516
}
475517
}
476-
518+
return true;
477519
}
478520

479521
/**
@@ -521,10 +563,10 @@ bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating
521563
std::array<int, 5> splitOffer;
522564
std::array<int, 5> splitDemand;
523565
for(int i = 0; i < 5; i++) {
524-
splitOffer[i] = counterOffer[i] < 0 ? 0 : -counterOffer[i];
566+
splitOffer[i] = counterOffer[i] > 0 ? 0 : -counterOffer[i];
525567
splitDemand[i] = counterOffer[i] < 0 ? 0 : counterOffer[i];
526568
}
527-
initiating.acceptOffer(receiving, splitOffer, splitDemand);
569+
initiating.acceptOffer(receiving, splitDemand, splitOffer);
528570
} else {
529571
//std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction(std::bind(&GameController::handleTradeOffer, this, _2, std::ref(initiating), _1, std::ref(receiving)));
530572
std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction([this, &initiating, &receiving, counterOffer](std::array<int, 5> offer, ScreenCoordinate coord) {

src/GameView.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ bool ViewElement::handleClick(ScreenCoordinate coord) {
8686
* Constrct a GameView.
8787
* @param model The GameBoard the view is displaying.
8888
*/
89-
GameView::GameView(GameBoard& model) : model(model) {
89+
GameView::GameView(GameBoard& model) : model(model)
90+
{
9091
controlStateText = "Welcome to Wars of Catan";
91-
92+
showTotals = false;
9293
}
9394

9495
/**
@@ -104,7 +105,19 @@ GameView::~GameView() {
104105
* @param font the style of font to use, fontSize the resolution of the font used
105106
* @return void
106107
*/
107-
void GameView::drawCardCount(std::string font, int fontSize){
108+
void GameView::drawCardCount(std::string font, int fontSize)
109+
{
110+
if (showTotals==false)
111+
{
112+
renderText(font, fontSize, {0.97, 0.0}, {1.0, 0.05}, "?"); //Road Building
113+
renderText(font, fontSize, {0.97, 0.05}, {1.0, 0.1}, "?"); //Knight
114+
renderText(font, fontSize, {0.97, 0.1}, {1.0, 0.15}, "?"); //Year of Plenty
115+
renderText(font, fontSize, {0.97, 0.15}, {1.0, 0.2}, "?"); //Monopoly
116+
renderText(font, fontSize, {0.97, 0.2}, {1.0, 0.25}, "?"); //Victory Point
117+
return;
118+
}
119+
120+
108121
renderText(font, fontSize, {0.97, 0.0}, {1.0, 0.05},
109122
toString(model.getCurrentPlayer().getRoadBuildingCards())); //Road Building
110123
renderText(font, fontSize, {0.97, 0.05}, {1.0, 0.1},
@@ -120,16 +133,27 @@ void GameView::drawCardCount(std::string font, int fontSize){
120133
/**
121134
* Draws the count of resources the currentPlayer has
122135
*/
123-
void GameView::drawResourceCount(std::string font, int fontSize){
124-
renderText(font, fontSize, {0.97, 0.35}, {1.0, 0.40},
136+
void GameView::drawResourceCount(std::string font, int fontSize)
137+
{
138+
if(showTotals==false)
139+
{
140+
renderText(font, fontSize, {0.97, 0.30}, {1.0, 0.35}, "?"); //Wood
141+
renderText(font, fontSize, {0.97, 0.35}, {1.0, 0.40}, "?"); //Sheep
142+
renderText(font, fontSize, {0.97, 0.40}, {1.0, 0.45}, "?"); //Ore
143+
renderText(font, fontSize, {0.97, 0.45}, {1.0, 0.50}, "?"); //Brick
144+
renderText(font, fontSize, {0.97, 0.50}, {1.0, 0.55}, "?"); //Wheat
145+
return;
146+
}
147+
148+
renderText(font, fontSize, {0.97, 0.30}, {1.0, 0.35},
125149
toString(model.getCurrentPlayer().getWood())); //Wood
126-
renderText(font, fontSize, {0.97, 0.40}, {1.0, 0.45},
150+
renderText(font, fontSize, {0.97, 0.35}, {1.0, 0.40},
127151
toString(model.getCurrentPlayer().getWool())); //Sheep
128-
renderText(font, fontSize, {0.97, 0.45}, {1.0, 0.50},
152+
renderText(font, fontSize, {0.97, 0.40}, {1.0, 0.45},
129153
toString(model.getCurrentPlayer().getOre())); //Ore
130-
renderText(font, fontSize, {0.97, 0.50}, {1.0, 0.55},
154+
renderText(font, fontSize, {0.97, 0.45}, {1.0, 0.50},
131155
toString(model.getCurrentPlayer().getBrick())); //Brick
132-
renderText(font, fontSize, {0.97, 0.55}, {1.0, 0.60},
156+
renderText(font, fontSize, {0.97, 0.50}, {1.0, 0.55},
133157
toString(model.getCurrentPlayer().getWheat())); //Wheat
134158

135159
}

tests/testRenderer.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
#include <iostream>
2+
#include <vector>
23

34
#include "gtest/gtest.h"
45

56
#include "Renderer.h"
67

8+
using std::vector;
79
using std::make_pair;
810

9-
TEST(RendererTest, coordToScreen) {
11+
class CoordinateConversionTest : public ::testing::TestWithParam<Coordinate> {
12+
13+
};
14+
15+
TEST_P(CoordinateConversionTest, IsReversable) {
16+
ASSERT_EQ(GetParam(), screenToCoord(coordToScreen(GetParam())));
17+
}
18+
19+
vector<Coordinate> makeInputs() {
20+
vector<Coordinate> ret;
1021
for(int x = -5; x < 5; x++) {
11-
for(auto y = -5; y < 5; y++) {
12-
auto original = make_pair(x, y);
13-
auto screen = coordToScreen(original);
14-
auto back = screenToCoord(screen);
15-
ASSERT_EQ(original.first, back.first);
16-
ASSERT_EQ(original.second, back.second);
22+
for(int y = -5; y < 5; y++) {
23+
ret.push_back(Coordinate{x, y});
1724
}
1825
}
26+
return std::move(ret);
1927
}
28+
29+
INSTANTIATE_TEST_CASE_P(LargeSquare, CoordinateConversionTest, ::testing::ValuesIn(makeInputs()));

tests/test_GameBoard.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,9 @@ TEST(GameBoardTest, updateLongestRoadPlayer){
328328
TEST(GameBoardTest, canRobberRob){
329329
GameBoard test_board({"tester1", "tester2"});
330330
Player& test_player1 = test_board.getPlayer(0);
331-
Player& test_player2 = test_board.getPlayer(1);
332-
333-
331+
334332
ASSERT_FALSE(test_board.canRobberRob(test_player1, Coordinate(0,1)));
335-
333+
336334
test_board.PlaceSettlement(Coordinate(0,0), test_player1);
337335
ASSERT_TRUE(test_board.canRobberRob(test_player1, Coordinate(0,1)));
338336
}

0 commit comments

Comments
 (0)