Skip to content

Commit b850150

Browse files
committed
Merged in Paul's and Alex's branches.
2 parents dff358f + 4f083e9 commit b850150

File tree

7 files changed

+175
-60
lines changed

7 files changed

+175
-60
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ EXECUTABLE := warsofcatan
77
ALLFILES := $(wildcard $(SRC_HOME)/*) $(wildcard $(INCL_HOME)/*)
88
export CXX := g++
99
export LD := g++
10-
export CXXFLAGS := -g -I$(INCL_HOME) -std=gnu++0x -Wall
10+
export CXXFLAGS := -g -I$(INCL_HOME) -std=c++0x -Wall
1111
export LDFLAGS := -lSDL2 -lGL -lGLU
1212

1313
.PHONY: all

include/GameBoard.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
class GameBoard {
1616
private:
17-
std::map<Coordinate, std::unique_ptr<GamePiece>> pieces;
17+
std::map<Coordinate, std::unique_ptr<GamePiece>> corners;
18+
std::map<Coordinate, std::unique_ptr<GamePiece>> resources;
1819
std::vector<std::unique_ptr<const Road>> roads;
1920

2021
int constructBoardFromFile(std::ifstream &file);
@@ -28,7 +29,13 @@ class GameBoard {
2829

2930
int saveBoardToFile(std::string filename);
3031
int loadBoardFromFile(std::string filename);
31-
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getPieces() const;
32+
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
33+
34+
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
35+
36+
void PlaceSettlement(Coordinate location, Player& Owner);
37+
38+
void init_resources();
3239
};
3340

34-
#endif
41+
#endif

include/GamePiece.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,52 @@
44
#include "Util.h"
55
#include "Player.h"
66

7+
enum resourceType { WHEAT, SHEEP, STONE, BRICK, WOOD, DESERT };
8+
79
class GameBoard;
810

911
class GamePiece {
1012
private:
1113
GameBoard& board;
1214
public:
13-
GamePiece(GameBoard& board);
15+
GamePiece(GameBoard& board, Coordinate location);
1416
GamePiece(GamePiece&) = delete;
1517
virtual ~GamePiece();
1618
//virtual GamePiece& operator=(GamePiece&) = delete;
1719

1820
Coordinate getCoordinates() const;
1921
GameBoard& getBoard();
2022
const GameBoard& getBoard() const;
23+
24+
Coordinate location;
2125
};
2226

2327
class ResourceTile : public GamePiece {
2428
public:
25-
enum Type { WOOD, SHEEP, ORE, BRICK, GRAIN, DESERT };
26-
private:
27-
Type type;
28-
unsigned short diceValue;
29-
public:
30-
ResourceTile(GameBoard& board, Type type, unsigned short diceValue);
29+
ResourceTile(GameBoard& board);
30+
ResourceTile(GameBoard& board, Coordinate location, resourceType resource, int value);
3131
ResourceTile(ResourceTile&) = delete;
32-
virtual ~ResourceTile();
3332
//virtual ResourceTile& operator=(ResourceTile&) = delete;
34-
35-
Type getType() const;
36-
unsigned short getDiceValue() const;
33+
34+
//dispense resource cards to owning players
35+
void Payout();
36+
37+
resourceType resource;
38+
int value;
39+
40+
virtual ~ResourceTile();
3741
};
3842

3943
class Settlement : public GamePiece {
4044
private:
4145
Player& owner;
4246
public:
43-
Settlement(GameBoard& board, Player& owner);
47+
Settlement(GameBoard& board, Coordinate location, Player& owner);
4448
Settlement(Settlement&) = delete;
4549
virtual ~Settlement();
4650
//virtual Settlement& operator=(Settlement&) = delete;
51+
52+
bool city;
4753
};
4854

4955
class Road {

src/GameBoard.cpp

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
#include "GameBoard.h"
22

3+
34
#include <map>
45
#include <memory>
56

67
using std::map;
78
using std::unique_ptr;
89

10+
#define ADD_RESOURCE(x, y, res, val) (resources[Coordinate(x,y)] = \
11+
std::unique_ptr<GamePiece>(new ResourceTile(*this, Coordinate(x,y), res, val)))
12+
#define DUMMY_BOARD //define to instantiate dummy board for debugging
13+
914
GameBoard::GameBoard() {
10-
pieces.emplace(Coordinate(0, 0), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::WOOD, 2)));
11-
pieces.emplace(Coordinate(1, 1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::SHEEP, 2)));
12-
pieces.emplace(Coordinate(-1, -1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::BRICK, 2)));
13-
pieces.emplace(Coordinate(2, -1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::ORE, 2)));
14-
pieces.emplace(Coordinate(-2, 1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::DESERT, 2)));
15-
pieces.emplace(Coordinate(1, -2), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::GRAIN, 2)));
16-
pieces.emplace(Coordinate(-1, 2), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::BRICK, 2)));
15+
init_resources();
1716
}
1817

1918
GameBoard::~GameBoard() {
@@ -66,6 +65,60 @@ int GameBoard::constructFileFromBoard(std::ofstream &file){
6665
return 0;
6766
}
6867

69-
const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getPieces() const {
70-
return pieces;
68+
const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getResources() const {
69+
return resources;
70+
}
71+
72+
std::vector<Settlement*> GameBoard::GetNeighboringSettlements(Coordinate location) {
73+
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
74+
std::vector<Settlement*> v;
75+
for(unsigned int i = 0; i < 6; i++) {
76+
const Coordinate& diff = adjacentCoordDiffs[i];
77+
Coordinate adjacentPoint(location.first + diff.first, location.second + diff.second);
78+
auto it = resources.find(adjacentPoint);
79+
if(it != resources.end()) {
80+
GamePiece* piece = it->second.get();
81+
if(dynamic_cast<Settlement*>(piece)) {
82+
v.push_back(static_cast<Settlement*>(piece));
83+
}
84+
}
85+
}
86+
return v;
7187
}
88+
89+
/* initialize board with a set of resources. Right now, just creates a static tile arrangement to test
90+
URL: http://images.fanpop.com/images/image_uploads/Differents-Boards-settlers-of-catan-521934_1157_768.jpg*/
91+
92+
void GameBoard::init_resources()
93+
{
94+
95+
96+
#ifdef DUMMY_BOARD
97+
ADD_RESOURCE(0, 1, BRICK, 2);
98+
ADD_RESOURCE(-2, 2, SHEEP, 5);
99+
ADD_RESOURCE(2, 0, WOOD, 6);
100+
ADD_RESOURCE(-3, 3, DESERT, 0);
101+
ADD_RESOURCE(-1, 3, SHEEP, 10);
102+
ADD_RESOURCE(1, 2, WHEAT, 9);
103+
ADD_RESOURCE(3, 1, WHEAT, 3);
104+
ADD_RESOURCE(-4, 6, WOOD, 8);
105+
ADD_RESOURCE(-2, 5, SHEEP, 3);
106+
ADD_RESOURCE(0, 4, STONE, 11);
107+
ADD_RESOURCE(2, 3, STONE, 4);
108+
ADD_RESOURCE(4, 2, SHEEP, 8);
109+
ADD_RESOURCE(-3, 7, BRICK, 4);
110+
ADD_RESOURCE(-1, 6, WHEAT, 6);
111+
ADD_RESOURCE(1, 5, WOOD, 5);
112+
ADD_RESOURCE(3, 4, BRICK, 10);
113+
ADD_RESOURCE(-2, 8, WOOD, 11);
114+
ADD_RESOURCE(0, 7, STONE, 12);
115+
ADD_RESOURCE(2, 6, WHEAT, 9);
116+
#endif
117+
118+
119+
}
120+
121+
void GameBoard::PlaceSettlement(Coordinate location, Player& Owner){
122+
corners[location] = std::unique_ptr<GamePiece>(new Settlement(*this, location, Owner));
123+
}
124+

src/GamePiece.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
#include "GameBoard.h"
44

5-
#include <stdexcept>
6-
7-
using std::runtime_error;
8-
9-
GamePiece::GamePiece(GameBoard& board) : board(board) {
5+
GamePiece::GamePiece(GameBoard& board, Coordinate location) : board(board), location(location) {
106

117
}
128

@@ -15,12 +11,7 @@ GamePiece::~GamePiece() {
1511
}
1612

1713
Coordinate GamePiece::getCoordinates() const {
18-
for(auto& it : board.getPieces()) {
19-
if(it.second.get() == this) {
20-
return it.first;
21-
}
22-
}
23-
throw runtime_error("This GamePiece does not exist on the board it holds a reference to.");
14+
return location;
2415
}
2516

2617
GameBoard& GamePiece::getBoard() {
@@ -31,28 +22,28 @@ const GameBoard& GamePiece::getBoard() const {
3122
return board;
3223
}
3324

34-
ResourceTile::ResourceTile(GameBoard& board, Type type, unsigned short diceValue) : GamePiece(board), type(type), diceValue(diceValue) {
35-
if(type != WOOD && type != SHEEP && type != ORE && type != BRICK && type != GRAIN && type != DESERT) {
36-
throw runtime_error("Invalid resource tile type");
37-
}
38-
if(diceValue < 2 || diceValue > 12) {
39-
throw runtime_error("Invalid dice value");
40-
}
25+
ResourceTile::ResourceTile(GameBoard& board, Coordinate location, resourceType resource, int value) :
26+
GamePiece(board, location), resource(resource), value(value) {
27+
4128
}
4229

4330
ResourceTile::~ResourceTile() {
4431

4532
}
4633

47-
ResourceTile::Type ResourceTile::getType() const {
48-
return type;
49-
}
50-
51-
unsigned short ResourceTile::getDiceValue() const {
52-
return diceValue;
34+
//pay resource cards to owners of this tile
35+
/*
36+
void ResourceTile::Payout() {
37+
std::vector<GamePiece> neighbors = board.GetNeighbors(location);
38+
for (int i = 0; i < neighbors.size; i++) //someone tell me how to traverse a vector less stupidly
39+
{
40+
neighbors[i].owner.addresource(resource, 1 + neighbors[i].city)
41+
}
5342
}
43+
*/
5444

55-
Settlement::Settlement(GameBoard& board, Player& owner) : GamePiece(board), owner(owner) {
45+
Settlement::Settlement(GameBoard& board, Coordinate location, Player& owner) :
46+
GamePiece(board, location), owner(owner), city(0) {
5647

5748
}
5849

src/Renderer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,25 @@ void drawHex(const Coordinate& coord, const ResourceTile& tile) {
7878
static GLuint tileTextures = loadImageAsTexture("resources/tiles.bmp");
7979
glBindTexture(GL_TEXTURE_2D, tileTextures);
8080
typedef std::vector<pair<float, float> > texCoordList;
81-
static const std::map<ResourceTile::Type, texCoordList> resourceTexCoords = {
82-
make_pair(ResourceTile::GRAIN, texCoordList { make_pair(377, 73), make_pair(500, 285),
81+
static const std::map<resourceType, texCoordList> resourceTexCoords = {
82+
make_pair(WHEAT, texCoordList { make_pair(377, 73), make_pair(500, 285),
8383
make_pair(380, 502), make_pair(136, 503), make_pair(10, 288), make_pair(134, 74)}),
84-
make_pair(ResourceTile::SHEEP, texCoordList { make_pair(959, 75), make_pair(1076, 288),
84+
make_pair(SHEEP, texCoordList { make_pair(959, 75), make_pair(1076, 288),
8585
make_pair(955, 503), make_pair(712, 501), make_pair(586, 289), make_pair(708, 73)}),
86-
make_pair(ResourceTile::WOOD, texCoordList { make_pair(1491, 60), make_pair(1618, 269),
86+
make_pair(WOOD, texCoordList { make_pair(1491, 60), make_pair(1618, 269),
8787
make_pair(1479, 490), make_pair(1260, 493), make_pair(1126, 283), make_pair(1246, 65)}),
88-
make_pair(ResourceTile::ORE, texCoordList { make_pair(382, 689), make_pair(506, 898),
88+
make_pair(STONE, texCoordList { make_pair(382, 689), make_pair(506, 898),
8989
make_pair(386, 1118), make_pair(142, 1120), make_pair(17, 905), make_pair(138, 691)}),
90-
make_pair(ResourceTile::BRICK, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
90+
make_pair(BRICK, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
9191
make_pair(1490, 1120), make_pair(1248, 1118), make_pair(1124, 898), make_pair(1250, 688)}),
92-
make_pair(ResourceTile::DESERT, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
92+
make_pair(DESERT, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
9393
make_pair(1490, 1120), make_pair(1248, 1118), make_pair(1124, 898), make_pair(1250, 688)}),
9494
};
9595
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
96-
if(resourceTexCoords.find(tile.getType()) == resourceTexCoords.end()) {
96+
if(resourceTexCoords.find(tile.resource) == resourceTexCoords.end()) {
9797
throw runtime_error("Cannot draw this tile; it is invalid.");
9898
}
99-
const texCoordList& texCoords = resourceTexCoords.at(tile.getType());
99+
const texCoordList& texCoords = resourceTexCoords.find(tile.resource)->second;
100100
glBegin(GL_TRIANGLE_FAN);
101101
texCoordPair(averagePoint(texCoords));
102102
vertexPair(coord);
@@ -122,7 +122,7 @@ void drawHex(const Coordinate& coord, const ResourceTile& tile) {
122122
void renderBoard(const GameBoard& board, const Player& perspective) {
123123
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124124

125-
for(auto& it : board.getPieces()) {
125+
for(auto& it : board.getResources()) {
126126
const Coordinate& coord = it.first;
127127
const GamePiece& piece = *(it.second);
128128
if(dynamic_cast<const ResourceTile*>(&piece)) {

src/board_layout.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
` /\
3+
/ \
4+
| |
5+
| |
6+
| |
7+
\ /
8+
\/
9+
hex cell
10+
NOTE: this isn't how you typically view the board
11+
it's rotated 120 degrees for axial symmetry
12+
13+
14+
^ 7
15+
|y /
16+
| /
17+
| /
18+
|/x
19+
coordinate axes
20+
21+
22+
(0,8)
23+
/\ /\ /\
24+
/ \ / \ / \
25+
| || || |
26+
| || || |
27+
| || || |
28+
\ / \ / \ /
29+
\/ \/ \/
30+
/\ /\ /\ /\
31+
/ \ / \ / \ / \
32+
| || || || |
33+
| || || || |
34+
| || || || |
35+
\ / \ / \ / \ /
36+
\/ \/ \/ \/
37+
/\ /\ /\ /\ /\
38+
/ \ / \ / \ / \ / \
39+
| || || || || |
40+
(-4, 6)| x || || || || x |(4,2)
41+
| || || || || |
42+
\ / \ / \ / \ / \ /
43+
\/ \/ \/ \/ \/
44+
/\ /\ /\ /\
45+
/ \ / \ / \ / \
46+
| || || || |
47+
| || || || |
48+
| || || || |
49+
\ / \ / \ / \ /
50+
\/ \/ \/ \/
51+
/\ /\ /\
52+
/ \ / \ / \
53+
| || || |
54+
| || || |
55+
| || || |
56+
\ / \ / \ /
57+
\/ \/ \/
58+
(0,0)

0 commit comments

Comments
 (0)