Skip to content

Commit 300ec41

Browse files
committed
Merge branch 'dice_operations' of https://github.com/Databean/warsofcatan into dice_operations
2 parents 5ab7b6b + 7e2a8da commit 300ec41

File tree

14 files changed

+209
-31
lines changed

14 files changed

+209
-31
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: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616
#include "tinyxml2.h"
1717
#include "Road.h"
1818

19+
1920
class GameVisitor;
2021

2122
class GameBoard {
2223
private:
2324
std::map<Coordinate, std::unique_ptr<CornerPiece>> corners;
24-
std::map<Coordinate, std::unique_ptr<GamePiece>> resources;
25+
26+
std::map<Coordinate, std::unique_ptr<ResourceTile>> resources;
27+
28+
2529
std::map<Coordinate, std::vector<std::shared_ptr<Road>>> roads;
30+
2631
std::vector<std::unique_ptr<Player>> players;
32+
Coordinate robber;
33+
2734

2835
void addResource(int x, int y, resourceType res, int val);
2936
bool checkRolls(int* rolls);
@@ -53,16 +60,30 @@ class GameBoard {
5360

5461
void save(std::ostream& out);
5562

56-
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
63+
64+
const std::map<Coordinate, std::unique_ptr<ResourceTile>>& getResources() const;
65+
66+
67+
5768
const std::shared_ptr<Road> getRoad(Coordinate start, Coordinate end) const;
69+
5870

5971
int FindLongestRoad(const Player & owner) const;
6072

61-
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
73+
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location) const;
74+
std::vector<CornerPiece*> GetNeighboringCorners(Coordinate location) const;
75+
6276

63-
bool buyRoad(Coordinate start, Coordinate end, Player& Owner);
6477

6578
void PlaceSettlement(Coordinate location, Player& Owner);
79+
void UpgradeSettlement(Coordinate location);
80+
//void PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
81+
82+
83+
bool buyRoad(Coordinate start, Coordinate end, Player& Owner);
84+
85+
86+
//void PlaceSettlement(Coordinate location, Player& Owner);
6687
void PlaceCity(Coordinate location, Player& Owner);
6788
bool PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
6889

@@ -71,6 +92,10 @@ class GameBoard {
7192
bool operator==(const GameBoard& other) const;
7293

7394
bool testRollChecking(int* rolls);
95+
96+
void moveRobber(Coordinate newRobber);
97+
Coordinate getRobber() const;
98+
7499
};
75100

76101
#endif

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
@@ -86,6 +86,8 @@ class Player {
8686
void addWheat(int resource);
8787
void addWool(int resource);
8888

89+
void addResource(int resourceType, int delta);
90+
8991
void accept(GameVisitor& visitor);
9092
bool operator==(const Player& player) const;
9193
};

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;
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: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "GameVisitor.h"
1212
#include "Serialization.h"
1313
#include "tinyxml2.h"
14+
15+
#include "CornerPiece.h"
16+
1417
#include "City.h"
1518

1619
using std::shared_ptr;
@@ -82,7 +85,7 @@ void GameBoard::insertTile(Coordinate location, vector<resourceType>& resources,
8285

8386
GameBoard::GameBoard(std::vector<std::unique_ptr<Player>>&& players, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations) : players(std::move(players)) {
8487
for(auto& resource : resourceLocations) {
85-
resources[resource.first] = std::unique_ptr<GamePiece>(new ResourceTile(*this, resource.first, resource.second.first, resource.second.second));
88+
resources[resource.first] = std::unique_ptr<ResourceTile>(new ResourceTile(*this, resource.first, resource.second.first, resource.second.second));
8689
}
8790
if(!isValidBoard()) {
8891
throw std::runtime_error("Board is invalid.");
@@ -217,12 +220,12 @@ void GameBoard::save(ostream& out) {
217220
out << printer.CStr();
218221
}
219222

220-
const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getResources() const {
223+
const map<Coordinate, unique_ptr<ResourceTile>>& GameBoard::getResources() const {
221224
return resources;
222225
}
223226

224227
std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
225-
Coordinate location) {
228+
Coordinate location) const {
226229
static Coordinate adjacentCoordDiffs[] = { Coordinate(0, 1), Coordinate(1,
227230
0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0),
228231
Coordinate(-1, 1) };
@@ -231,8 +234,8 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
231234
const Coordinate& diff = adjacentCoordDiffs[i];
232235
Coordinate adjacentPoint(location.first + diff.first,
233236
location.second + diff.second);
234-
auto it = resources.find(adjacentPoint);
235-
if (it != resources.end()) {
237+
auto it = corners.find(adjacentPoint);
238+
if (it != corners.end()) {
236239
GamePiece* piece = it->second.get();
237240
if (dynamic_cast<Settlement*>(piece)) {
238241
v.push_back(static_cast<Settlement*>(piece));
@@ -242,6 +245,28 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
242245
return v;
243246
}
244247

248+
std::vector<CornerPiece*> GameBoard::GetNeighboringCorners(
249+
Coordinate location) const{
250+
static Coordinate adjacentCoordDiffs[] = { Coordinate(0, 1), Coordinate(1,
251+
0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0),
252+
Coordinate(-1, 1) };
253+
std::vector<CornerPiece*> v;
254+
for (unsigned int i = 0; i < 6; i++) {
255+
const Coordinate& diff = adjacentCoordDiffs[i];
256+
Coordinate adjacentPoint(location.first + diff.first,
257+
location.second + diff.second);
258+
auto it = resources.find(adjacentPoint);
259+
if (it != resources.end()) {
260+
GamePiece* piece = it->second.get();
261+
if (dynamic_cast<CornerPiece*>(piece)) {
262+
v.push_back(static_cast<CornerPiece*>(piece));
263+
}
264+
}
265+
}
266+
return v;
267+
}
268+
269+
245270
/**
246271
* Checks to make sure the coordinate is within bounds of the board and not a resource tile.
247272
*/
@@ -267,6 +292,7 @@ bool GameBoard::roadExists(Coordinate start, Coordinate end) const {
267292
/**
268293
* Checks to make sure the road being placed at a valid point according to the rules
269294
*/
295+
270296
bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner) const {
271297
//is there a settlement we can build off of
272298
auto cornerIt = corners.find(point);
@@ -290,6 +316,7 @@ bool GameBoard::isRoadConnectionPoint(Coordinate point, Player& Owner) const {
290316

291317
return false;
292318

319+
293320
}
294321

295322
/**
@@ -309,6 +336,18 @@ bool GameBoard::verifyRoadPlacement(Coordinate start, Coordinate end, Player& Ow
309336
return true;
310337
}
311338

339+
void GameBoard::moveRobber(Coordinate newRobber) {
340+
341+
robber = newRobber;
342+
343+
//force trade
344+
}
345+
346+
Coordinate GameBoard::getRobber() const {
347+
return robber;
348+
349+
}
350+
312351
/**
313352
* Places a road at the specified coordinates that will be owned by the given player
314353
* returns true if the road was placed, false otherwise
@@ -412,44 +451,58 @@ int GameBoard::FindLongestRoad_FromPoint(Coordinate curr, const Player & owner,
412451
return longest_path;
413452
}
414453

454+
455+
415456
void GameBoard::PlaceSettlement(Coordinate location, Player& Owner){
416457
corners[location] = std::unique_ptr<CornerPiece>(new Settlement(*this, location, Owner));
417458
}
418459

419460
void GameBoard::PlaceCity(Coordinate location, Player& Owner){
420461
corners[location] = std::unique_ptr<CornerPiece>(new City(*this, location, Owner));
462+
421463
}
422464

465+
void GameBoard::UpgradeSettlement(Coordinate location){
466+
corners[location] = std::unique_ptr<CornerPiece>(new City(*corners[location])); //TODO test for memory leak
467+
}
468+
469+
470+
423471
void GameBoard::accept(GameVisitor& visitor) {
424472
visitor.visit(*this);
473+
474+
425475
// Drawing needs this to happen in this order. Visitors technically should be order-independent, but this was an easy fix at the moment.
426476
// Keep that in mind when modifying this.
427477
for(auto& it : resources) {
428478
it.second->accept(visitor);
429479
}
430480
for(auto& it : corners) {
431481
it.second->accept(visitor);
482+
432483
}
433484
for(auto& roadCoordVec : roads) {
434485
for(auto& road : roadCoordVec.second) {
435-
road->accept(visitor);
486+
if(road.get()) {
487+
road->accept(visitor);
488+
}
436489
}
437490
}
438491
for(auto& it : players) {
439-
it->accept(visitor);
492+
if(it.get()) {
493+
it->accept(visitor);
494+
}
440495
}
441496
}
442497

443498
bool GameBoard::operator==(const GameBoard& other) const {
444-
if(corners.size() != other.corners.size()) {
445-
return false;
446-
}
447499
for(auto& it : corners) {
448500
auto otherIt = other.corners.find(it.first);
449501
if(otherIt == other.corners.end()) {
450-
return false; // This location isn't in the other array
451-
}
452-
if(!(*(it.second) == *(otherIt->second))) {
502+
if(it.second.get()) {
503+
return false; // This location isn't in the other array
504+
}
505+
} else if(!(*(it.second) == *(otherIt->second))) {
453506
return false;
454507
}
455508
}
@@ -481,12 +534,10 @@ bool GameBoard::operator==(const GameBoard& other) const {
481534
}
482535
}
483536
if(players.size() != other.players.size()) {
484-
std::cout << "sizes differ" << std::endl;
485537
return false;
486538
}
487539
for(unsigned int i = 0; i < players.size(); i++) {
488540
if(!(*(players[i]) == *(other.players[i]))) {
489-
std::cout << "player " << i << " differs" << std::endl;
490541
return false;
491542
}
492543
}
@@ -510,7 +561,7 @@ bool GameBoard::operator==(const GameBoard& other) const {
510561
*/
511562
void GameBoard::addResource(int x, int y, resourceType res, int val)
512563
{
513-
this->resources[Coordinate(x,y)] = std::unique_ptr<GamePiece>(new ResourceTile(*this, Coordinate(x,y), res, val));
564+
this->resources[Coordinate(x,y)] = std::unique_ptr<ResourceTile>(new ResourceTile(*this, Coordinate(x,y), res, val));
514565
}
515566

516567
bool GameBoard::isValidBoard() const {

0 commit comments

Comments
 (0)