Skip to content

Commit 8281136

Browse files
Kyle GrageKyle Grage
authored andcommitted
Added method to add/subtract multiple resources at once and refactored
to have functions use this
1 parent 29b0a0c commit 8281136

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

include/Player.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Player {
105105
void addOre(int resource);
106106
void addWheat(int resource);
107107
void addWool(int resource);
108+
void addMultiple(int wood, int brick, int ore, int wheat, int wool);
108109

109110
int getResource(int resourceType) const; //
110111
void addResource(int resourceType, int delta);

src/Player.cpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ bool Player::canBuyRoad(){
108108
*/
109109
bool Player::buyRoad(){
110110
if(canBuyRoad()){
111-
addWood(-1);
112-
addBrick(-1);
111+
addMultiple(-1,-1,0,0,0);
113112
return true;
114113
}
115114
//insufficient funds
@@ -130,10 +129,7 @@ bool Player::canBuySettlement(){
130129
*/
131130
bool Player::buySettlement(){
132131
if(canBuySettlement()){
133-
addWood(-1);
134-
addBrick(-1);
135-
addWheat(-1);
136-
addWool(-1);
132+
addMultiple(-1,-1,0,-1,-1);
137133
return true;
138134
}
139135
return false;
@@ -153,8 +149,7 @@ bool Player::canBuyCity(){
153149
*/
154150
bool Player::buyCity(){
155151
if(canBuyCity()){
156-
addWheat(-2);
157-
addOre(-3);
152+
addMultiple(0,0,-3,-2,0);
158153
}
159154
return false;
160155
}
@@ -173,11 +168,7 @@ bool Player::canBuyWonder(){
173168
*/
174169
bool Player::buyWonder(){
175170
if(canBuySettlement()){
176-
addWood(-5);
177-
addBrick(-5);
178-
addWheat(-5);
179-
addWool(-5);
180-
addOre(-5);
171+
addMultiple(-5,-5,-5,-5,-5);
181172
return true;
182173
}
183174
return false;
@@ -544,7 +535,10 @@ int Player::getWool() const
544535
}
545536

546537

547-
538+
/**
539+
* Adds (or subtracts) the amount of wood a player has
540+
* @param resource, the number to add (negative to subtract)
541+
*/
548542
void Player::addWood(int resource)
549543
{
550544
if(resources[WOOD_INDEX] < (0-resource))
@@ -553,6 +547,10 @@ void Player::addWood(int resource)
553547
resources[WOOD_INDEX] += resource;
554548
}
555549

550+
/**
551+
* Adds (or subtracts) the amount of brick a player has
552+
* @param resource, the number to add (negative to subtract)
553+
*/
556554
void Player::addBrick(int resource)
557555
{
558556
if(resources[BRICK_INDEX] < (0-resource))
@@ -561,6 +559,10 @@ void Player::addBrick(int resource)
561559
resources[BRICK_INDEX] += resource;
562560
}
563561

562+
/**
563+
* Adds (or subtracts) the amount of ore a player has
564+
* @param resource, the number to add (negative to subtract)
565+
*/
564566
void Player::addOre(int resource)
565567
{
566568
if(resources[ORE_INDEX] < (0-resource))
@@ -569,6 +571,10 @@ void Player::addOre(int resource)
569571
resources[ORE_INDEX] += resource;
570572
}
571573

574+
/**
575+
* Adds (or subtracts) the amount of wheat a player has
576+
* @param resource, the number to add (negative to subtract)
577+
*/
572578
void Player::addWheat(int resource)
573579
{
574580
if(resources[WHEAT_INDEX] < (0-resource))
@@ -577,6 +583,10 @@ void Player::addWheat(int resource)
577583
resources[WHEAT_INDEX] += resource;
578584
}
579585

586+
/**
587+
* Adds (or subtracts) the amount of wool a player has
588+
* @param resource, the number to add (negative to subtract)
589+
*/
580590
void Player::addWool(int resource)
581591
{
582592
if(resources[WOOL_INDEX] < (0-resource))
@@ -585,6 +595,20 @@ void Player::addWool(int resource)
585595
resources[WOOL_INDEX] += resource;
586596
}
587597

598+
/**
599+
* Adds (or subtracts) the amount of resources a player has
600+
* Param order: wood, brick, ore, wheat, wool
601+
* @param [resource], the number to add (negative to subtract)
602+
*
603+
*/
604+
void Player::addMultiple(int wood, int brick, int ore, int wheat, int wool){
605+
addWood(wood);
606+
addBrick(brick);
607+
addOre(ore);
608+
addWheat(wheat);
609+
addWool(wool);
610+
}
611+
588612
/**
589613
* Get a player's name.
590614
* @return The player's name.
@@ -601,7 +625,10 @@ std::string Player::getName() const
601625
* @param delta The change in the resource.
602626
*/
603627
void Player::addResource(int resourceType, int delta) {
604-
resources[resourceType] += delta;
628+
if(resources[resourceType] < (0-delta))
629+
resources[resourceType] = 0;
630+
else
631+
resources[resourceType] += delta;
605632

606633
}
607634

0 commit comments

Comments
 (0)