Skip to content

Commit 3449b67

Browse files
committed
Actually does trades now
1 parent 04ae11e commit 3449b67

File tree

3 files changed

+24
-62
lines changed

3 files changed

+24
-62
lines changed

include/Player.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <vector>
1414
#include <string>
1515
#include <memory>
16+
#include <array>
1617

1718
#include "tinyxml2.h"
1819

@@ -45,7 +46,7 @@ class Player {
4546
int tradeModifiers[5];
4647

4748

48-
void tradeWithBank(int offer[], int demand[]);
49+
void tradeWithBank(std::array<int, 5> offer, std::array<int, 5> demand);
4950

5051
public:
5152

@@ -78,11 +79,9 @@ class Player {
7879

7980
void setGenralModifier(); //3:1 port
8081

81-
bool offerBankTrade(int offer[], int demand[]);
82+
bool offerBankTrade(std::array<int, 5> offer, std::array<int, 5> demand);
8283

83-
bool offerTrade(Player* p, int offer[], int demand[]);
84-
bool recieveOffer(Player* p, int offer[], int demand[]);
85-
bool acceptOffer(Player* p, int offer[], int demand[]);
84+
bool acceptOffer(Player& p, std::array<int, 5> offer, std::array<int, 5> demand);
8685

8786
int getRandomResource();
8887

src/GameController.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating
132132
auto priority = -10;
133133
if(offer == negativeArr<5>(counterOffer)) {
134134
view.removeElement(priority);
135-
//TODO: perform trade
135+
std::array<int, 5> splitOffer;
136+
std::array<int, 5> splitDemand;
137+
for(int i = 0; i < 5; i++) {
138+
splitOffer[i] = counterOffer[i] < 0 ? 0 : -counterOffer[i];
139+
splitDemand[i] = counterOffer[i] < 0 ? 0 : counterOffer[i];
140+
}
141+
initiating.acceptOffer(receiving, splitOffer, splitDemand);
136142
} else {
137143
//std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction(std::bind(&GameController::handleTradeOffer, this, _2, std::ref(initiating), _1, std::ref(receiving)));
138144
std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction([this, &initiating, &receiving, counterOffer](std::array<int, 5> offer, ScreenCoordinate coord) {

src/Player.cpp

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void Player::setGenralModifier()
258258
*@param offer An array representing your offer to the bank
259259
*@param demand An array representing your demand
260260
*/
261-
void Player::tradeWithBank(int offer[], int demand[])
261+
void Player::tradeWithBank(std::array<int, 5> offer, std::array<int, 5> demand)
262262
{
263263
for(int i=0; i<5; i++)
264264
{
@@ -273,12 +273,12 @@ void Player::tradeWithBank(int offer[], int demand[])
273273
* @param demand An array representing your demand
274274
* @return true if bank accepted and trade was successful.
275275
*/
276-
bool Player::offerBankTrade(int offer[], int demand[])
276+
bool Player::offerBankTrade(std::array<int, 5> offer, std::array<int, 5> demand)
277277
{
278-
if(!checkResources(offer))
278+
if(!checkResources(offer.data()))
279279
return false;
280280

281-
int offerToBank[5];
281+
std::array<int, 5> offerToBank;
282282

283283
for(int i=0; i<5; i++)
284284
{
@@ -292,66 +292,23 @@ bool Player::offerBankTrade(int offer[], int demand[])
292292
}
293293

294294

295-
/**
296-
* Offer a trade to another player with an offer and a demand.
297-
* @param p The other player that is receiving the trade.
298-
* @param offer The resources this player is offering to the other player.
299-
* @param demand The resources that this player wants in return from the other player.
300-
* @return If the trade succeeded.
301-
*/
302-
bool Player::offerTrade(Player* p, int offer[], int demand[])
303-
{
304-
if(sizeof offer/sizeof(int) != 5 || sizeof demand/sizeof(int) != 5)
305-
return false; //Invalid Trade
306-
307-
if(!this->checkResources(offer))
308-
return false; //YOu dont have enough to offer this
309-
310-
return p->recieveOffer(this, offer, demand);
311-
}
312-
313-
314-
/**
315-
* Receive a trade offer from another player.
316-
* @param p The player offering the trade.
317-
* @param offer The resources the other player is giving.
318-
* @param demand The resources the other player wants in return.
319-
* @return If the trade succeeded.
320-
*/
321-
bool Player::recieveOffer(Player* p, int offer[], int demand[])
322-
{
323-
if( !this->checkResources(demand) )
324-
return false;
325-
326-
bool input = true;
327-
328-
//TODO:Display Offer to User and wait for input
329-
330-
if(input)
331-
{
332-
this->acceptOffer(p, offer, demand);
333-
return true;
334-
}
335-
else
336-
return false;
337-
338-
}
339-
340-
341295
/**
342296
* Accept the trade offer from another player.
343297
* @param p The player offering the trade.
344298
* @param offer The resources the other player is offering.
345299
* @param demand The resources the other player wants in return.
346300
*/
347301

348-
bool Player::acceptOffer(Player* p, int offer[], int demand[])
302+
bool Player::acceptOffer(Player& p, std::array<int, 5> offer, std::array<int, 5> demand)
349303
{
350-
p->addWood(demand[WOOD_INDEX] - offer[WOOD_INDEX]);
351-
p->addBrick(demand[BRICK_INDEX] - offer[BRICK_INDEX]);
352-
p->addOre(demand[ORE_INDEX] - offer[ORE_INDEX]);
353-
p->addWheat(demand[WHEAT_INDEX] - offer[WHEAT_INDEX]);
354-
p->addWool(demand[WOOL_INDEX] - offer[WOOL_INDEX]);
304+
if(!checkResources(offer.data()) || !p.checkResources(demand.data())) {
305+
return false;
306+
}
307+
p.addWood(demand[WOOD_INDEX] - offer[WOOD_INDEX]);
308+
p.addBrick(demand[BRICK_INDEX] - offer[BRICK_INDEX]);
309+
p.addOre(demand[ORE_INDEX] - offer[ORE_INDEX]);
310+
p.addWheat(demand[WHEAT_INDEX] - offer[WHEAT_INDEX]);
311+
p.addWool(demand[WOOL_INDEX] - offer[WOOL_INDEX]);
355312

356313
this->addWood(offer[WOOD_INDEX] - demand[WOOD_INDEX]);
357314
this->addBrick(offer[BRICK_INDEX] - demand[BRICK_INDEX]);

0 commit comments

Comments
 (0)