Skip to content

Commit 157c4d0

Browse files
committed
added functionality for cities and settlements
1 parent 6148b44 commit 157c4d0

File tree

13 files changed

+153
-26
lines changed

13 files changed

+153
-26
lines changed

include/City.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ class City : public CornerPiece {
1010
City(GameBoard& board, Coordinate location, Player& owner);
1111
City(City&) = delete;
1212
~City();
13+
City(CornerPiece& sett);
1314
City& operator=(City&) = delete;
1415

1516
virtual void accept(GameVisitor&);
1617
virtual bool operator==(const GamePiece& piece) const;
18+
19+
int getResourceModifier();
20+
int getVictoryPoints();
1721
};
1822

1923
#endif

include/CornerPiece.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class CornerPiece : public GamePiece {
1515

1616
Player& getOwner();
1717
const Player& getOwner() const;
18+
19+
virtual int getResourceModifier();
20+
21+
virtual int getVictoryPoints();
1822
};
1923

2024
#endif

include/GameBoard.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class GameVisitor;
2020

2121
class GameBoard {
2222
private:
23-
std::map<Coordinate, std::unique_ptr<GamePiece>> corners;
24-
std::map<Coordinate, std::unique_ptr<GamePiece>> resources;
23+
std::map<Coordinate, std::unique_ptr<CornerPiece>> corners;
24+
std::map<Coordinate, std::unique_ptr<ResourceTile>> resources;
2525
std::vector<std::unique_ptr<Player>> players;
2626

2727
void addResource(int x, int y, resourceType res, int val);
@@ -49,14 +49,17 @@ class GameBoard {
4949

5050
void save(std::ostream& out);
5151

52-
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
52+
const std::map<Coordinate, std::unique_ptr<ResourceTile>>& getResources() const;
5353
std::shared_ptr<Road> getRoad(Coordinate start, Coordinate end);
5454

5555
int FindLongestRoad(Player & owner);
5656

57-
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
57+
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location) const;
58+
std::vector<CornerPiece*> GetNeighboringCorners(Coordinate location) const;
59+
5860

5961
void PlaceSettlement(Coordinate location, Player& Owner);
62+
void UpgradeSettlement(Coordinate location);
6063
void PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
6164

6265

include/GamePiece.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class GameBoard;
1313
class GamePiece {
1414
private:
1515
GameBoard& board;
16+
1617
public:
1718
GamePiece(GameBoard& board, Coordinate location);
1819
GamePiece(GamePiece&) = delete;
@@ -29,6 +30,7 @@ class GamePiece {
2930
virtual bool operator==(const GamePiece& other) const = 0;
3031

3132
Coordinate getLocation() const;
33+
3234
};
3335

3436
class ResourceTile : public GamePiece {
@@ -39,7 +41,7 @@ class ResourceTile : public GamePiece {
3941
//virtual ResourceTile& operator=(ResourceTile&) = delete;
4042

4143
//dispense resource cards to owning players
42-
void Payout();
44+
void Payout() const;
4345

4446
resourceType resource;
4547
int value;

include/Player.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Player {
8484
void setWheat(int resource);
8585
void setWool(int resource);
8686

87+
void addResource(int resourceType, int delta);
88+
8789
void accept(GameVisitor& visitor);
8890
bool operator==(const Player& player) const;
8991
};

include/Settlement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Settlement : public CornerPiece {
1414

1515
virtual void accept(GameVisitor&);
1616
virtual bool operator==(const GamePiece&) const;
17+
int getResourceModifier();
18+
int getVictoryPoints();
1719
};
1820

1921
#endif

src/City.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "City.h"
2-
2+
#include "Settlement.h"
33
#include "GameVisitor.h"
44

55
City::City(GameBoard& board, Coordinate location, Player& owner) : CornerPiece(board, location, owner) {
@@ -17,3 +17,15 @@ void City::accept(GameVisitor& visitor) {
1717
bool City::operator==(const GamePiece& p) const {
1818
return false;
1919
}
20+
21+
int City::getResourceModifier() {
22+
return 2; //TODO: implement robber check here
23+
}
24+
25+
int City::getVictoryPoints() {
26+
return 2; //TODO: implement robber check here
27+
}
28+
29+
City::City(CornerPiece& sett) : CornerPiece(sett.getBoard(), sett.getLocation(), sett.getOwner()) {
30+
31+
}

src/CornerPiece.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ Player& CornerPiece::getOwner() {
1515
const Player& CornerPiece::getOwner() const {
1616
return owner;
1717
}
18+
19+
20+
int CornerPiece::getVictoryPoints() {
21+
return 0;
22+
}
23+
24+
int CornerPiece::getResourceModifier() {
25+
return 0;
26+
}

src/GameBoard.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "GameVisitor.h"
1111
#include "Serialization.h"
1212
#include "tinyxml2.h"
13+
#include "CornerPiece.h"
14+
#include "City.h"
1315

1416
using std::shared_ptr;
1517
using std::random_shuffle;
@@ -111,12 +113,12 @@ void GameBoard::save(ostream& out) {
111113
out << printer.CStr();
112114
}
113115

114-
const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getResources() const {
116+
const map<Coordinate, unique_ptr<ResourceTile>>& GameBoard::getResources() const {
115117
return resources;
116118
}
117119

118120
std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
119-
Coordinate location) {
121+
Coordinate location) const {
120122
static Coordinate adjacentCoordDiffs[] = { Coordinate(0, 1), Coordinate(1,
121123
0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0),
122124
Coordinate(-1, 1) };
@@ -125,8 +127,8 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
125127
const Coordinate& diff = adjacentCoordDiffs[i];
126128
Coordinate adjacentPoint(location.first + diff.first,
127129
location.second + diff.second);
128-
auto it = resources.find(adjacentPoint);
129-
if (it != resources.end()) {
130+
auto it = corners.find(adjacentPoint);
131+
if (it != corners.end()) {
130132
GamePiece* piece = it->second.get();
131133
if (dynamic_cast<Settlement*>(piece)) {
132134
v.push_back(static_cast<Settlement*>(piece));
@@ -136,6 +138,28 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
136138
return v;
137139
}
138140

141+
std::vector<CornerPiece*> GameBoard::GetNeighboringCorners(
142+
Coordinate location) const{
143+
static Coordinate adjacentCoordDiffs[] = { Coordinate(0, 1), Coordinate(1,
144+
0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0),
145+
Coordinate(-1, 1) };
146+
std::vector<CornerPiece*> v;
147+
for (unsigned int i = 0; i < 6; i++) {
148+
const Coordinate& diff = adjacentCoordDiffs[i];
149+
Coordinate adjacentPoint(location.first + diff.first,
150+
location.second + diff.second);
151+
auto it = resources.find(adjacentPoint);
152+
if (it != resources.end()) {
153+
GamePiece* piece = it->second.get();
154+
if (dynamic_cast<CornerPiece*>(piece)) {
155+
v.push_back(static_cast<CornerPiece*>(piece));
156+
}
157+
}
158+
}
159+
return v;
160+
}
161+
162+
139163
/**
140164
* Checks to make sure the coordinate is within bounds of the board
141165
*/
@@ -195,15 +219,15 @@ bool GameBoard::roadExists(Coordinate start, Coordinate end) {
195219
* Checks to make sure the road being placed at a valid point according to the rules
196220
*/
197221
bool GameBoard::isRoadConnectionPoint(Coordinate start, Coordinate end, Player& Owner){
198-
/** Need to figure out the CornerPiece/GamePiece predicament
199-
CornerPiece * corner = corners[start];
200-
if(corner != NULL){
201-
if (corner->getOwner() == Owner)
222+
223+
//std::unique_ptr<GamePiece> corner = corners[start];
224+
if(corners[start] != NULL){
225+
if (corners[start]->getOwner() == Owner)
202226
return true;
203227
}
204228
return false;
205-
**/
206-
return true;
229+
230+
207231
}
208232

209233
/**
@@ -347,9 +371,15 @@ void GameBoard::init_resources()
347371
}
348372

349373
void GameBoard::PlaceSettlement(Coordinate location, Player& Owner){
350-
corners[location] = std::unique_ptr<GamePiece>(new Settlement(*this, location, Owner));
374+
corners[location] = std::unique_ptr<CornerPiece>(new Settlement(*this, location, Owner));
375+
}
376+
377+
void GameBoard::UpgradeSettlement(Coordinate location){
378+
corners[location] = std::unique_ptr<CornerPiece>(new City(*corners[location])); //TODO test for memory leak
351379
}
352380

381+
382+
353383
void GameBoard::accept(GameVisitor& visitor) {
354384
visitor.visit(*this);
355385
for(auto& it : corners) {
@@ -430,7 +460,7 @@ bool GameBoard::operator==(const GameBoard& other) const {
430460
*/
431461
void GameBoard::addResource(int x, int y, resourceType res, int val)
432462
{
433-
this->resources[Coordinate(x,y)] = std::unique_ptr<GamePiece>(new ResourceTile(*this, Coordinate(x,y), res, val));
463+
this->resources[Coordinate(x,y)] = std::unique_ptr<ResourceTile>(new ResourceTile(*this, Coordinate(x,y), res, val));
434464
}
435465

436466
/*

src/GamePiece.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "GameBoard.h"
44
#include "GameVisitor.h"
55

6-
GamePiece::GamePiece(GameBoard& board, Coordinate location) : board(board), location(location) {
6+
GamePiece::GamePiece(GameBoard& board, Coordinate location) :
7+
board(board), location(location){
78

89
}
910

@@ -58,12 +59,14 @@ bool ResourceTile::operator==(const GamePiece& other) const {
5859
}
5960

6061
//pay resource cards to owners of this tile
61-
/*
62-
void ResourceTile::Payout() {
63-
std::vector<GamePiece> neighbors = board.GetNeighbors(location);
64-
for (int i = 0; i < neighbors.size; i++) //someone tell me how to traverse a vector less stupidly
62+
63+
void ResourceTile::Payout() const{
64+
std::vector<CornerPiece*> neighbors = getBoard().GetNeighboringCorners(location);
65+
std::vector<CornerPiece*>::iterator it = neighbors.begin();
66+
while (it != neighbors.end())
6567
{
66-
neighbors[i].owner.addresource(resource, 1 + neighbors[i].city)
68+
(*it)->getOwner().addResource(resource, (*it)->getResourceModifier());
69+
it++;
6770
}
6871
}
69-
*/
72+

0 commit comments

Comments
 (0)