Skip to content

Commit fe5d202

Browse files
committed
Merge pull request #64 from Databean/tradeWithBank
Trade with bank
2 parents 195c37e + 4d74a7d commit fe5d202

File tree

9 files changed

+89
-19
lines changed

9 files changed

+89
-19
lines changed

include/GameController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class GameController {
7575

7676
bool handlePlayerClick(ScreenCoordinate, Player&);
7777
bool handleTradeOffer(ScreenCoordinate, Player& initiating, std::array<int, 5>, Player& receiving, std::array<int, 5>);
78+
79+
bool handleBankClick(ScreenCoordinate);
7880
};
7981

8082
#endif

include/GameDice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class GameDice {
1111
int first;
1212
int second;
1313
public:
14+
GameDice();
15+
1416
int roll();
1517
int getFirst() const;
1618
int getSecond() const;

include/GameView.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ std::unique_ptr<ViewElement> makeViewButtonText(Fn fn, std::pair<ScreenCoordinat
205205

206206
class TradingView : public ViewElement {
207207
private:
208-
Player& initiating;
209-
Player& receiving;
208+
std::string initiating;
209+
std::string receiving;
210210

211211
ViewButtonText trade;
212212
ViewButtonText cancel;
@@ -218,7 +218,7 @@ class TradingView : public ViewElement {
218218
protected:
219219
virtual bool clicked(ScreenCoordinate coord);
220220
public:
221-
TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> offer);
221+
TradingView(const std::string& initiating, const std::string& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> offer);
222222
virtual ~TradingView();
223223

224224
void render();

include/Player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Player {
116116

117117
void setGenralModifier(); //3:1 port
118118

119-
bool offerBankTrade(std::array<int, 5> offer, std::array<int, 5> demand);
119+
bool makeBankTrade(std::array<int, 5> offer, std::array<int, 5> demand);
120120

121121
bool acceptOffer(Player& p, std::array<int, 5> offer, std::array<int, 5> demand);
122122

src/GameBoard.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ GameBoard::GameBoard(const vector<std::string>& playerNames) {
7676
}
7777
std::cout << getRobber().first << "\n";
7878
std::cout << getRobber().second << "\n";
79-
79+
80+
maxVictoryPoints = 10;
8081
}
8182

8283
/**
@@ -334,6 +335,7 @@ ResourceTile& GameBoard::getResourceTile(Coordinate location) const
334335
*/
335336
void GameBoard::endTurn()
336337
{
338+
std::cout << currentTurn << std::endl;
337339
if(getCurrentPlayer().getVictoryPoints() >= getMaxVictoryPoints())
338340
{
339341
//std::cout<<"GG Bitches";

src/GameController.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
GameController::GameController(GameBoard& model, GameView& view) : model(model), view(view) {
1919
using namespace std::placeholders;
2020

21-
view.addElement(makeViewButton(std::bind(&GameController::handleBoardEvent, this, _1), {{0, 0}, {1, 1}}));
22-
2321
auto font = getGraphicsConfig()["font.path"];
2422
auto fontSize = getGraphicsConfig()["font.size"];
2523

@@ -36,6 +34,7 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
3634
view.addElement(makeViewButtonText(std::bind(&GameController::handlePlayerClick, this, _1, std::ref(player)), {{1.0 - width, playerTopY - 0.05}, {1.0, playerTopY}}, font, fontSize, player.getName()));
3735
playerTopY -= 0.05;
3836
}
37+
view.addElement(makeViewButtonText(std::bind(&GameController::handleBankClick, this, _1), {{0, 0.8}, {0.1, 0.9}}, font, fontSize, "Bank"));
3938

4039
view.addElement(makeViewButtonText(std::bind(&GameController::handleCancelButtonEvent, this, _1), {{.92, .96}, {1.0, 1.0}}, font, fontSize, "Cancel"));
4140

@@ -45,7 +44,7 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
4544
view.addElement(makeViewButtonText(std::bind(&GameController::handleYearOfPlentyCardButtonEvent, this, _1), {{0.85, 0.10}, {0.97, 0.15}}, font, fontSize, "Year of Plenty "));
4645
view.addElement(makeViewButtonText(std::bind(&GameController::handleMonopolyCardButtonEvent, this, _1), {{0.85, 0.15}, {0.97, 0.20}}, font, fontSize, "Monopoly "));
4746
view.addElement(makeViewButtonText(std::bind(&GameController::handleVictoryPointCardButtonEvent, this, _1), {{0.85, 0.20}, {0.97, 0.25}}, font, fontSize, "Victory Point "));
48-
47+
4948
view.addElement(makeViewButtonText(std::bind(&GameController::handleWoodButtonEvent, this, _1), {{.85, .30}, {.97, .35}}, font, fontSize, "Wood "));
5049
view.addElement(makeViewButtonText(std::bind(&GameController::handleSheepButtonEvent, this, _1), {{.85, .35}, {.97, .40}}, font, fontSize, "Sheep "));
5150
view.addElement(makeViewButtonText(std::bind(&GameController::handleOreButtonEvent, this, _1), {{.85, .40}, {.97, .45}}, font, fontSize, "Ore "));
@@ -54,7 +53,7 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
5453

5554
view.addElement(makeViewButtonText(std::bind(&GameController::viewCardTotals, this, _1), {{.85, .55}, {.97, .60}}, font, fontSize, "Show Totals"));
5655

57-
56+
view.addElement(100, makeViewButton(std::bind(&GameController::handleBoardEvent, this, _1), {{0, 0}, {1, 1}}));
5857
stateStack.push_back(BASESTATE);
5958
}
6059

@@ -491,7 +490,7 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
491490
return true;
492491
});
493492

494-
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction, initial)));
493+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating.getName(), receiving.getName(), tradeFunction, cancelFunction, initial)));
495494
std::cout << player.getName() << std::endl;
496495
return true;
497496

@@ -519,6 +518,37 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
519518
return true;
520519
}
521520

521+
/**
522+
* Handle a player beginning a trade with the bank.
523+
*/
524+
bool GameController::handleBankClick(ScreenCoordinate screenCoord) {
525+
526+
auto priority = -10;
527+
528+
auto tradeFunction = [this, priority](std::array<int, 5> offer, ScreenCoordinate coord) {
529+
std::array<int, 5> splitOffer;
530+
std::array<int, 5> splitDemand;
531+
for(int i = 0; i < 5; i++) {
532+
splitOffer[i] = offer[i] > 0 ? 0 : -offer[i];
533+
splitDemand[i] = offer[i] < 0 ? 0 : offer[i];
534+
}
535+
if(model.getCurrentPlayer().makeBankTrade(splitOffer, splitDemand)) {
536+
view.removeElement(priority);
537+
}
538+
return true;
539+
};
540+
541+
auto cancelFunction = [this, priority](ScreenCoordinate coord) {
542+
view.removeElement(priority);
543+
return true;
544+
};
545+
std::array<int, 5> initial{{0, 0, 0, 0, 0}};
546+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(model.getCurrentPlayer().getName(), "Bank", tradeFunction, cancelFunction, initial)));
547+
548+
return true;
549+
}
550+
551+
522552
/**
523553
* Handle a trade offer from a player.
524554
* @param coord The coordinate clicked on to initiate the trade.
@@ -549,7 +579,7 @@ bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating
549579
return true;
550580
});
551581

552-
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction, counterOffer)));
582+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating.getName(), receiving.getName(), tradeFunction, cancelFunction, counterOffer)));
553583
}
554584
return true;
555585
}
@@ -627,4 +657,3 @@ bool GameController::handleBrickButtonEvent(ScreenCoordinate coord){
627657
return false;
628658

629659
}
630-

src/GameDice.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55

66
using std::rand;
77

8+
GameDice::GameDice() {
9+
first = 3;
10+
second = 3;
11+
}
12+
813
/**
914
* Re-roll the dice.
1015
* @return The sum of the two dice.
1116
*/
1217
int GameDice::roll() {
13-
first = rand() % 6 + 1;
14-
second = rand() % 6 + 1;
18+
first = (rand() % 6) + 1;
19+
second = (rand() % 6) + 1;
1520

1621
return first + second;
1722
}

src/GameView.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ bool GameView::acceptInput(SDL_Event& event) {
207207
ScreenCoordinate screen = {(float) event.button.x / getGraphicsConfig()["screen.width"], 1.f - (float) event.button.y / getGraphicsConfig()["screen.height"]};
208208
for(auto& it : viewElements) {
209209
if(it.second->handleClick(screen)) {
210-
//break;
210+
break;
211211
}
212212
}
213213
}
@@ -606,13 +606,12 @@ void DrawingGameVisitor::visit(GameDice& dice) {
606606
glColor3d(1.0, 1.0, 1.0);
607607
static std::map<int, std::pair<float, float>> topLeftOffset;
608608
//construct offset map
609+
609610
for (int i = 1; i < 7; i++) {
610-
611611
//topLeftOffset.emplace(i, make_pair(DiceXCoords[(i-1)%3], DiceYCoords[i/4]));
612612
topLeftOffset.insert(make_pair(i, make_pair(DiceXCoords[(i-1)%3], DiceYCoords[i/4])));
613613
}
614614

615-
616615
drawTexturedRectangle(topLeftOffset.find(dice.getFirst())->second, DIE_SCREEN_SIDE_LENGTH,
617616
lDieScreenLoc, DIE_SIDE_LENGTH);
618617

@@ -716,7 +715,7 @@ void DrawingGameVisitor::visit(DevelopmentCard& card) {
716715
* @param cancel The callback for the "cancel" button
717716
* @param initialOffer The initial offer to display.
718717
*/
719-
TradingView::TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> initialOffer) :
718+
TradingView::TradingView(const std::string& initiating, const std::string& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> initialOffer) :
720719
ViewElement({getGraphicsConfig()["screen.tradingView.bottomLeft"], getGraphicsConfig()["screen.tradingView.topRight"]}),
721720
initiating(initiating),
722721
receiving(receiving),
@@ -785,7 +784,7 @@ void TradingView::render() {
785784
}
786785
auto playersBottomLeft = getGraphicsConfig()["screen.tradingView.players.bottomLeft"];
787786
auto playersTopRight = getGraphicsConfig()["screen.tradingView.players.topRight"];
788-
renderText(font, fontSize, playersBottomLeft, playersTopRight, initiating.getName() + " -> " + receiving.getName());
787+
renderText(font, fontSize, playersBottomLeft, playersTopRight, initiating + " -> " + receiving);
789788

790789
cancel.render();
791790
trade.render();

src/Player.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,37 @@ bool Player::acceptOffer(Player& p, std::array<int, 5> offer, std::array<int, 5>
624624
return true;
625625
}
626626

627+
/**
628+
* Make a trade with the bank if the offer is valid.
629+
* @param offer The resources the player is offering.
630+
* @param demand The resources the player wants from the bank.
631+
*/
632+
bool Player::makeBankTrade(std::array<int, 5> offer, std::array<int, 5> demand) {
633+
//Make sure i have the resources i'm offering
634+
for(int i = 0; i < 5; i++) {
635+
if(offer[i] > resources[i]) {
636+
std::cout << "don't have enough " << i << "(" << offer[i] << ">" << resources[i] << ")" << std::endl;
637+
return false;
638+
}
639+
}
640+
int credits = 0;
641+
for(int i = 0; i < 5; i++) {
642+
credits += offer[i] / tradeModifiers[i];
643+
}
644+
for(int i = 0; i < 5; i++) {
645+
credits -= demand[i];
646+
}
647+
if(credits < 0) {
648+
std::cout << "bank finds this unacceptable" << std::endl;
649+
return false;
650+
}
651+
652+
for(int i = 0; i < 5; i++) {
653+
resources[i] -= offer[i];
654+
resources[i] += demand[i];
655+
}
656+
return true;
657+
}
627658

628659
/**
629660
* picks any one resource at random for robber to steal

0 commit comments

Comments
 (0)