Skip to content

Commit 2bb5599

Browse files
committed
Added bank trading
1 parent 342e99b commit 2bb5599

File tree

9 files changed

+93
-20
lines changed

9 files changed

+93
-20
lines changed

include/GameController.h

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

7373
bool handlePlayerClick(ScreenCoordinate, Player&);
7474
bool handleTradeOffer(ScreenCoordinate, Player& initiating, std::array<int, 5>, Player& receiving, std::array<int, 5>);
75+
76+
bool handleBankClick(ScreenCoordinate);
7577
};
7678

7779
#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
@@ -202,8 +202,8 @@ std::unique_ptr<ViewElement> makeViewButtonText(Fn fn, std::pair<ScreenCoordinat
202202

203203
class TradingView : public ViewElement {
204204
private:
205-
Player& initiating;
206-
Player& receiving;
205+
std::string initiating;
206+
std::string receiving;
207207

208208
ViewButtonText trade;
209209
ViewButtonText cancel;
@@ -215,7 +215,7 @@ class TradingView : public ViewElement {
215215
protected:
216216
virtual bool clicked(ScreenCoordinate coord);
217217
public:
218-
TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> offer);
218+
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);
219219
virtual ~TradingView();
220220

221221
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
@@ -74,7 +74,8 @@ GameBoard::GameBoard(const vector<std::string>& playerNames) {
7474
}
7575
std::cout << getRobber().first << "\n";
7676
std::cout << getRobber().second << "\n";
77-
77+
78+
maxVictoryPoints = 10;
7879
}
7980

8081
/**
@@ -328,6 +329,7 @@ ResourceTile& GameBoard::getResourceTile(Coordinate location) const
328329
*/
329330
void GameBoard::endTurn()
330331
{
332+
std::cout << currentTurn << std::endl;
331333
if(getCurrentPlayer().getVictoryPoints() >= getMaxVictoryPoints())
332334
std::cout<<"GG Bitches";
333335

src/GameController.cpp

Lines changed: 38 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

@@ -35,6 +33,7 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
3533
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()));
3634
playerTopY -= 0.05;
3735
}
36+
view.addElement(makeViewButtonText(std::bind(&GameController::handleBankClick, this, _1), {{0, 0.8}, {0.1, 0.9}}, font, fontSize, "Bank"));
3837

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

@@ -50,8 +49,9 @@ GameController::GameController(GameBoard& model, GameView& view) : model(model),
5049
view.addElement(makeViewButtonText(std::bind(&GameController::handleOreButtonEvent, this, _1), {{.85, .45}, {.97, .50}}, font, fontSize, "Ore "));
5150
view.addElement(makeViewButtonText(std::bind(&GameController::handleBrickButtonEvent, this, _1), {{.85, .50}, {.97, .55}}, font, fontSize, "Brick "));
5251
view.addElement(makeViewButtonText(std::bind(&GameController::handleWheatButtonEvent, this, _1), {{.85, .55}, {.97, .60}}, font, fontSize, "Wheat "));
53-
54-
52+
53+
view.addElement(100, makeViewButton(std::bind(&GameController::handleBoardEvent, this, _1), {{0, 0}, {1, 1}}));
54+
5555
stateStack.push_back(BASESTATE);
5656
}
5757

@@ -448,7 +448,7 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
448448
return true;
449449
});
450450

451-
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction, initial)));
451+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating.getName(), receiving.getName(), tradeFunction, cancelFunction, initial)));
452452
std::cout << player.getName() << std::endl;
453453
return true;
454454

@@ -476,6 +476,38 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
476476

477477
}
478478

479+
/**
480+
* Handle a player beginning a trade with the bank.
481+
*/
482+
bool GameController::handleBankClick(ScreenCoordinate screenCoord) {
483+
484+
auto priority = -10;
485+
486+
auto tradeFunction = [this, priority](std::array<int, 5> offer, ScreenCoordinate coord) {
487+
std::array<int, 5> splitOffer;
488+
std::array<int, 5> splitDemand;
489+
for(int i = 0; i < 5; i++) {
490+
std::cout << "offer " << i << " " << offer[i] << std::endl;
491+
splitOffer[i] = offer[i] > 0 ? 0 : -offer[i];
492+
splitDemand[i] = offer[i] < 0 ? 0 : offer[i];
493+
}
494+
if(model.getCurrentPlayer().makeBankTrade(splitOffer, splitDemand)) {
495+
view.removeElement(priority);
496+
}
497+
return true;
498+
};
499+
500+
auto cancelFunction = [this, priority](ScreenCoordinate coord) {
501+
view.removeElement(priority);
502+
return true;
503+
};
504+
505+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(model.getCurrentPlayer().getName(), "Bank", tradeFunction, cancelFunction, {0, 0, 0, 0, 0})));
506+
507+
return true;
508+
}
509+
510+
479511
/**
480512
* Handle a trade offer from a player.
481513
* @param coord The coordinate clicked on to initiate the trade.
@@ -506,7 +538,7 @@ bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating
506538
return true;
507539
});
508540

509-
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction, counterOffer)));
541+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating.getName(), receiving.getName(), tradeFunction, cancelFunction, counterOffer)));
510542
}
511543
return true;
512544
}
@@ -584,4 +616,3 @@ bool GameController::handleBrickButtonEvent(ScreenCoordinate coord){
584616
return false;
585617

586618
}
587-

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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bool GameView::acceptInput(SDL_Event& event) {
180180
ScreenCoordinate screen = {(float) event.button.x / getGraphicsConfig()["screen.width"], 1.f - (float) event.button.y / getGraphicsConfig()["screen.height"]};
181181
for(auto& it : viewElements) {
182182
if(it.second->handleClick(screen)) {
183-
//break;
183+
break;
184184
}
185185
}
186186
}
@@ -579,13 +579,12 @@ void DrawingGameVisitor::visit(GameDice& dice) {
579579
glColor3d(1.0, 1.0, 1.0);
580580
static std::map<int, std::pair<float, float>> topLeftOffset;
581581
//construct offset map
582+
582583
for (int i = 1; i < 7; i++) {
583-
584584
//topLeftOffset.emplace(i, make_pair(DiceXCoords[(i-1)%3], DiceYCoords[i/4]));
585585
topLeftOffset.insert(make_pair(i, make_pair(DiceXCoords[(i-1)%3], DiceYCoords[i/4])));
586586
}
587587

588-
589588
drawTexturedRectangle(topLeftOffset.find(dice.getFirst())->second, DIE_SCREEN_SIDE_LENGTH,
590589
lDieScreenLoc, DIE_SIDE_LENGTH);
591590

@@ -689,7 +688,7 @@ void DrawingGameVisitor::visit(DevelopmentCard& card) {
689688
* @param cancel The callback for the "cancel" button
690689
* @param initialOffer The initial offer to display.
691690
*/
692-
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) :
691+
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) :
693692
ViewElement({getGraphicsConfig()["screen.tradingView.bottomLeft"], getGraphicsConfig()["screen.tradingView.topRight"]}),
694693
initiating(initiating),
695694
receiving(receiving),
@@ -748,7 +747,7 @@ void TradingView::render() {
748747
auto font = getGraphicsConfig()["font.path"];
749748
auto fontSize = getGraphicsConfig()["font.size"];
750749

751-
std::string resources[] = {"Wood", "Brick", "Ore", "Wheat", "Wool"};
750+
std::string resources[] = {"Wheat", "Wool", "Ore", "Brick", "Wood"};
752751
for(int i = 0; i < 5; i++) {
753752
auto leftX = getGraphicsConfig()["screen.tradingView.resources.leftX"];
754753
auto rightX = getGraphicsConfig()["screen.tradingView.resources.rightX"];
@@ -758,7 +757,7 @@ void TradingView::render() {
758757
}
759758
auto playersBottomLeft = getGraphicsConfig()["screen.tradingView.players.bottomLeft"];
760759
auto playersTopRight = getGraphicsConfig()["screen.tradingView.players.topRight"];
761-
renderText(font, fontSize, playersBottomLeft, playersTopRight, initiating.getName() + " -> " + receiving.getName());
760+
renderText(font, fontSize, playersBottomLeft, playersTopRight, initiating + " -> " + receiving);
762761

763762
cancel.render();
764763
trade.render();

src/Player.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,38 @@ 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+
std::cout << i << " " << offer[i] << " " << tradeModifiers[i] << std::endl;
643+
credits += offer[i] / tradeModifiers[i];
644+
}
645+
for(int i = 0; i < 5; i++) {
646+
credits -= demand[i];
647+
}
648+
if(credits < 0) {
649+
std::cout << "bank finds this unacceptable" << std::endl;
650+
return false;
651+
}
652+
653+
for(int i = 0; i < 5; i++) {
654+
resources[i] -= offer[i];
655+
resources[i] += demand[i];
656+
}
657+
return true;
658+
}
627659

628660
/**
629661
* picks any one resource at random for robber to steal

0 commit comments

Comments
 (0)