Skip to content

Commit c6ef3b6

Browse files
committed
Began working on placing Roads. Code is still untested. Roads are added
through interacting with the GameBoard
1 parent a76c85b commit c6ef3b6

File tree

8 files changed

+218
-54
lines changed

8 files changed

+218
-54
lines changed

include/GameBoard.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ class GameBoard {
2020
private:
2121
std::map<Coordinate, std::unique_ptr<GamePiece>> corners;
2222
std::map<Coordinate, std::unique_ptr<GamePiece>> resources;
23-
std::vector<std::unique_ptr<const Road>> roads;
23+
24+
std::map<Coordinate, std::vector<Road*>> roads;
2425

26+
bool verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner);
27+
bool outOfBounds(const Coordinate& coord);
28+
bool roadExists(Coordinate start, Coordinate end);
29+
bool isRoadConnectionPoint(Coordinate start, Coordinate end, Player& Owner);
30+
2531
int constructBoardFromFile(std::ifstream &file);
2632
int constructFileFromBoard(std::ofstream &file);
2733
public:
@@ -33,10 +39,13 @@ class GameBoard {
3339
int save_Board(std::string filename);
3440
int load_Board(std::string filename);
3541
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
42+
const Road& getRoad(int start, int end);
3643

3744
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
3845

3946
void PlaceSettlement(Coordinate location, Player& Owner);
47+
void PlaceRoad(Coordinate start, Coordinate end, Player& Owner);
48+
4049

4150
void init_resources();
4251
};

include/Player.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class Player {
6060
void setWheat(int resource);
6161
void setWool(int resource);
6262

63-
6463
};
6564

6665
#endif

include/Road.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@
22
#define ROAD_H
33

44
#include "Util.h"
5+
#include "Player.h"
6+
#include <vector>
7+
#include <cmath>
58

69
class Road {
710
private:
8-
Coordinate start;
9-
Coordinate end;
11+
bool checkRoad();
12+
1013
public:
11-
Road(Coordinate start, Coordinate end);
14+
Road(Coordinate start, Coordinate end, Player& Owner);
1215
Road(Road&) = delete;
1316
~Road();
1417
Road& operator=(Road&) = delete;
18+
19+
//Made these public because I couldn't get getters to work, maybe someone else knows how. Paul
20+
Coordinate start;
21+
Coordinate end;
22+
23+
Coordinate getStart();
24+
25+
bool equals(const Road& otherRoad);
26+
bool equals(const Coordinate& otherStart, const Coordinate& otherEnd);
27+
28+
Player* owner;
29+
30+
1531
};
1632

1733
#endif

src/GameBoard.cpp

Lines changed: 139 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ GameBoard::GameBoard() {
2121
}
2222

2323
GameBoard::~GameBoard() {
24-
25-
}
2624

25+
}
2726

28-
int GameBoard::save_Board(std::string filename){
27+
int GameBoard::save_Board(std::string filename) {
2928
std::ofstream file;
3029
file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
3130
try {
@@ -40,7 +39,7 @@ int GameBoard::save_Board(std::string filename){
4039
return -1;
4140
}
4241

43-
int GameBoard::load_Board(std::string filename){
42+
int GameBoard::load_Board(std::string filename) {
4443
std::ifstream file;
4544
try {
4645
filename = filename + ".wocs";
@@ -54,7 +53,7 @@ int GameBoard::load_Board(std::string filename){
5453
return -1;
5554
}
5655

57-
int GameBoard::constructBoardFromFile(std::ifstream &file){
56+
int GameBoard::constructBoardFromFile(std::ifstream &file) {
5857
//Parse and construct the board from the file
5958
//@ TODO
6059
std::string line;
@@ -66,8 +65,7 @@ int GameBoard::constructBoardFromFile(std::ifstream &file){
6665
return 0;
6766
}
6867

69-
70-
int GameBoard::constructFileFromBoard(std::ofstream &file){
68+
int GameBoard::constructFileFromBoard(std::ofstream &file) {
7169
//Construct the file based on the structure of the board
7270
//@ TODO
7371
file << "Hello World!";
@@ -78,16 +76,20 @@ const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getResources() const {
7876
return resources;
7977
}
8078

81-
std::vector<Settlement*> GameBoard::GetNeighboringSettlements(Coordinate location) {
82-
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
79+
std::vector<Settlement*> GameBoard::GetNeighboringSettlements(
80+
Coordinate location) {
81+
static Coordinate adjacentCoordDiffs[] = { Coordinate(0, 1), Coordinate(1,
82+
0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0),
83+
Coordinate(-1, 1) };
8384
std::vector<Settlement*> v;
84-
for(unsigned int i = 0; i < 6; i++) {
85+
for (unsigned int i = 0; i < 6; i++) {
8586
const Coordinate& diff = adjacentCoordDiffs[i];
86-
Coordinate adjacentPoint(location.first + diff.first, location.second + diff.second);
87+
Coordinate adjacentPoint(location.first + diff.first,
88+
location.second + diff.second);
8789
auto it = resources.find(adjacentPoint);
88-
if(it != resources.end()) {
90+
if (it != resources.end()) {
8991
GamePiece* piece = it->second.get();
90-
if(dynamic_cast<Settlement*>(piece)) {
92+
if (dynamic_cast<Settlement*>(piece)) {
9193
v.push_back(static_cast<Settlement*>(piece));
9294
}
9395
}
@@ -96,41 +98,133 @@ std::vector<Settlement*> GameBoard::GetNeighboringSettlements(Coordinate locatio
9698
}
9799

98100
/* initialize board with a set of resources. Currently only the standard configuration (no custom shapes or expansion packs) is implemented. Board tiles and roll numbers are randomized.
99-
@todo Change the dummy board to the actual board
101+
@todo Change the dummy board to the actual board
100102
*/
101103

102-
void GameBoard::init_resources()
103-
{
104-
std::srand(std::time(0));
105-
106-
resourceType resources[] = {BRICK, BRICK, BRICK, STONE, STONE, STONE, WHEAT, WHEAT, WHEAT, WHEAT, WOOD, WOOD, WOOD, WOOD, SHEEP, SHEEP, SHEEP, SHEEP, DESERT};
107-
random_shuffle(&resources[0], &resources[19]);
108-
109-
int rolls[] = {2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11};
110-
random_shuffle(&rolls[0], &rolls[18]);
111-
112-
int xcoords[] = {0, -2, 2, -3, -1, 1, 3, -4, -2, 0, 2, 4, -3, -1, 1, 3, -2, 0, 2};
113-
int ycoords[] = {1, 2, 0, 4, 3, 2, 1, 6, 5, 4, 3, 2, 7, 6, 5, 4, 8, 7, 6};
114-
115-
116-
#ifdef DUMMY_BOARD
117-
int rollCount = 0;
118-
for (int i = 0; i<19; i++)
119-
{
120-
if (resources[i]==DESERT)
121-
{
122-
ADD_RESOURCE(xcoords[i], ycoords[i], resources[i], 0);
123-
}
124-
else
125-
{
126-
ADD_RESOURCE(xcoords[i], ycoords[i], resources[i], rolls[rollCount]);
127-
rollCount++;
128-
}
129-
}
130-
#endif
104+
void GameBoard::init_resources() {
105+
std::srand(std::time(0));
106+
107+
resourceType resources[] = { BRICK, BRICK, BRICK, STONE, STONE, STONE,
108+
WHEAT, WHEAT, WHEAT, WHEAT, WOOD, WOOD, WOOD, WOOD, SHEEP, SHEEP,
109+
SHEEP, SHEEP, DESERT };
110+
random_shuffle(&resources[0], &resources[19]);
111+
112+
int rolls[] = { 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11 };
113+
random_shuffle(&rolls[0], &rolls[18]);
114+
115+
int xcoords[] = { 0, -2, 2, -3, -1, 1, 3, -4, -2, 0, 2, 4, -3, -1, 1, 3, -2,
116+
0, 2 };
117+
int ycoords[] = { 1, 2, 0, 4, 3, 2, 1, 6, 5, 4, 3, 2, 7, 6, 5, 4, 8, 7, 6 };
118+
119+
#ifdef DUMMY_BOARD
120+
int rollCount = 0;
121+
for (int i = 0; i < 19; i++) {
122+
if (resources[i] == DESERT) {
123+
ADD_RESOURCE(xcoords[i], ycoords[i], resources[i], 0);
124+
} else {
125+
ADD_RESOURCE(xcoords[i], ycoords[i], resources[i],
126+
rolls[rollCount]);
127+
rollCount++;
128+
}
129+
}
130+
#endif
131+
}
132+
133+
void GameBoard::PlaceSettlement(Coordinate location, Player& Owner) {
134+
corners[location] = std::unique_ptr < GamePiece
135+
> (new Settlement(*this, location, Owner));
131136
}
132137

133-
void GameBoard::PlaceSettlement(Coordinate location, Player& Owner){
134-
corners[location] = std::unique_ptr<GamePiece>(new Settlement(*this, location, Owner));
138+
/**
139+
* Checks to make sure the coordinate is within bounds of the board
140+
*/
141+
bool GameBoard::outOfBounds(const Coordinate& coord) {
142+
/**
143+
* This code is embarrassing, but I couldn't really figure out how to easily check for out of bounds
144+
* I'm sure there is a simple algebraic function that does it, but I went for the hacky way.
145+
*/
146+
switch (coord.second) {
147+
case 0:
148+
return (coord.first >= 0 && coord.first <= 4);
149+
break;
150+
case 1:
151+
return (coord.first >= -2 && coord.first <= 5);
152+
break;
153+
case 2:
154+
return (coord.first >= -3 && coord.first <= 5);
155+
break;
156+
case 3:
157+
return (coord.first >= -3 && coord.first <= 4);
158+
break;
159+
case 4:
160+
return (coord.first >= -4 && coord.first <= 4);
161+
break;
162+
case 5:
163+
return (coord.first >= -4 && coord.first <= 3);
164+
break;
165+
case 6:
166+
return (coord.first >= -5 && coord.first <= 3);
167+
break;
168+
case 7:
169+
return (coord.first >= -5 && coord.first <= 2);
170+
break;
171+
case 8:
172+
return (coord.first >= -4 && coord.first <= 0);
173+
break;
174+
default:
175+
break;
176+
}
177+
return false;
178+
}
179+
180+
bool GameBoard::roadExists(Coordinate start, Coordinate end) {
181+
std::vector<Road*> roadVector = roads[start];
182+
for (std::vector<Road*>::iterator road = roadVector.begin();
183+
road != roadVector.end(); ++road) {
184+
if ((*road)->equals(start, end))
185+
return true;
186+
}
187+
return false;
188+
}
189+
190+
bool GameBoard::isRoadConnectionPoint(Coordinate start, Coordinate end, Player& Owner){
191+
/** Need to figure out the CornerPiece/GamePiece predicament
192+
CornerPiece * corner = corners[start];
193+
if(corner != NULL){
194+
if (corner->getOwner() == Owner)
195+
return true;
196+
}
197+
return false;
198+
**/
199+
return true;
200+
}
201+
202+
bool GameBoard::verifyRoadPlacement(Coordinate start, Coordinate end, Player& Owner) {
203+
if (outOfBounds(start) || outOfBounds(end))
204+
return false;
205+
206+
if (roadExists(start, end))
207+
return false;
208+
209+
if (!isRoadConnectionPoint(start, end, Owner))
210+
return false;
211+
return true;
212+
}
213+
214+
void GameBoard::PlaceRoad(Coordinate start, Coordinate end, Player& Owner) {
215+
verifyRoadPlacement(start, end, Owner);
216+
Road * newRoad;
217+
try {
218+
newRoad = new Road(start, end, Owner);
219+
} catch (int n) {
220+
//Coordinates did not meet the criteria for a valid road
221+
return;
222+
}
223+
224+
std::vector<Road*> roadVector = roads[start];
225+
roadVector.push_back(newRoad);
226+
227+
roadVector = roads[end];
228+
roadVector.push_back(newRoad);
135229
}
136230

src/Player.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ void Player::playCard(DevelopmentCard *card)
6363

6464

6565

66-
6766
int Player::getWood()
6867
{
6968
return resources[0];

src/Road.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
#include "Road.h"
22

3-
Road::Road(Coordinate start, Coordinate end) {
3+
Road::Road(Coordinate start, Coordinate end, Player& Owner) {
44
if(start < end) {
55
this->start = start;
66
this->end = end;
77
} else {
88
this->end = start;
99
this->start = end;
1010
}
11+
owner = &Owner;
12+
13+
//If the input is bad, throw an exception so bad roads won't be built
14+
if(!checkRoad())
15+
throw -1;
16+
17+
}
18+
19+
/**
20+
* Valid roads must start in one point and end in another point a distance of 1 away.
21+
*/
22+
bool Road::checkRoad(){
23+
24+
int dist = std::abs(start.first - end.first) + std::abs(start.second - end.second);
25+
return dist == 1;
1126
}
1227

1328
Road::~Road() {
1429

15-
}
30+
}
31+
32+
Coordinate Road::getStart(){
33+
return start;
34+
}
35+
36+
bool Road::equals(const Road& otherRoad){
37+
Coordinate otherstart = otherRoad.start;
38+
Coordinate otherend = otherRoad.end;
39+
return equals(otherstart, otherend);
40+
}
41+
42+
bool Road::equals(const Coordinate& otherStart, const Coordinate& otherEnd){
43+
if((otherStart == start && otherEnd == end) || (otherStart == end && otherEnd == start))
44+
return true;
45+
return false;
46+
}
47+

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int argc, char *argv[]) {
4343
SDL_Window* displayWindow;
4444
SDL_Renderer* displayRenderer;
4545
SDL_RendererInfo displayRendererInfo;
46-
SDL_CreateWindowAndRenderer(900, 900, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
46+
SDL_CreateWindowAndRenderer(900, 800, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
4747
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
4848
/*TODO: Check that we have OpenGL */
4949
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||

tests/test_Roads.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* test_Roads.cpp
3+
*
4+
* Created on: Mar 1, 2014
5+
* Author: The Pickle
6+
*/
7+
#include "UnitTest++.h"
8+
#include "Road.h"
9+
#include "Util.h"
10+
11+
TEST(road_constructor_1){
12+
Coordinate start = Coordinate(0,0);
13+
Coordinate end = Coordinate(0,1);
14+
}
15+

0 commit comments

Comments
 (0)