@@ -21,11 +21,10 @@ GameBoard::GameBoard() {
2121}
2222
2323GameBoard::~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
0 commit comments