Skip to content

Commit 8212ee7

Browse files
committed
Merge pull request #6 from Databean/refactorBoardInitialization
Changed resource initialization to be represented by creating concentric...
2 parents 243716f + bbf395e commit 8212ee7

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

include/GameBoard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class GameBoard {
4040

4141
void removeRoadEnd(std::shared_ptr<Road> startRoad);
4242
int FindLongestRoad_FromPoint(Coordinate curr, Player & owner, std::map<Coordinate, bool>& marked, int length);
43-
43+
44+
void createRing(Coordinate topRight, int sideLength, std::vector<resourceType>& resources, std::vector<int>& rolls);
45+
void insertTile(Coordinate location, std::vector<resourceType>& resources, std::vector<int>& rolls);
4446
public:
4547
GameBoard(std::vector<std::unique_ptr<Player>>&& players);
4648
GameBoard(std::vector<std::unique_ptr<Player>>&& players, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations);

src/GameBoard.cpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <algorithm>
77
#include <iostream>
88
#include <stdexcept>
9+
#include <set>
910

1011
#include "GameVisitor.h"
1112
#include "Serialization.h"
@@ -29,37 +30,56 @@ using std::vector;
2930
*/
3031
GameBoard::GameBoard(vector<unique_ptr<Player>>&& players) : players(std::move(players)) {
3132
std::srand(std::time(0));
32-
33-
resourceType resources[] = {BRICK, BRICK, BRICK, STONE, STONE, STONE, WHEAT, WHEAT, WHEAT, WHEAT, WOOD, WOOD, WOOD, WOOD, SHEEP, SHEEP, SHEEP, SHEEP, DESERT};
34-
random_shuffle(&resources[0], &resources[19]);
35-
36-
int rolls[] = {0, 2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12};
37-
38-
int xcoords[] = {-2, 0, 2, -3, -1, 1, 3, -4, -2, 0, 2, 4, -3, -1, 1, 3, -2, 0, 2};
39-
int ycoords[] = { 2, 1, 0, 4, 3, 2, 1, 6, 5, 4, 3, 2, 7, 6, 5, 4, 8, 7, 6};
33+
34+
const static vector<resourceType> boardResources {BRICK, BRICK, BRICK, STONE, STONE, STONE, WHEAT, WHEAT, WHEAT, WHEAT, WOOD, WOOD, WOOD, WOOD, SHEEP, SHEEP, SHEEP, SHEEP};
35+
const static vector<int> boardRolls = {0, 2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12};
4036

4137
bool valid = false;
4238

43-
while(!valid) {
39+
const static Coordinate center {0, 4};
40+
41+
while(!valid) {
4442
this->resources.clear();
45-
random_shuffle(&rolls[0], &rolls[18]);
46-
int resourceCount = 0;
47-
for (int i = 0; i<19; i++)
48-
{
49-
if (rolls[i] == 0)
50-
{
51-
addResource(xcoords[i], ycoords[i], resources[18], 0);
52-
}
53-
else
54-
{
55-
addResource(xcoords[i], ycoords[i], resources[resourceCount], rolls[i]);
56-
resourceCount++;
57-
}
43+
44+
vector<resourceType> resources = boardResources;
45+
random_shuffle(resources.begin(), resources.end());
46+
47+
vector<int> rolls = boardRolls;
48+
random_shuffle(rolls.begin(), rolls.end());
49+
50+
insertTile(center, resources, rolls);
51+
for(int i = 1; i < 3; i++) {
52+
createRing({center.first + i, center.second + i}, i, resources, rolls);
5853
}
5954
valid = isValidBoard();
6055
}
6156
}
6257

58+
void GameBoard::createRing(Coordinate topRight, int sideLength, vector<resourceType>& resources, vector<int>& rolls) {
59+
//static const Coordinate adjacentTileOffsets[] = {Coordinate(1, -2), Coordinate(2, -1), Coordinate(-1, -1), Coordinate(-2, 1), Coordinate(2, -1), Coordinate(1, 1)};
60+
static const Coordinate adjacentTileOffsets[] = {Coordinate{1, -2}, Coordinate{-1, -1}, Coordinate{-2, 1}, Coordinate{-1, 2}, Coordinate{1, 1}, Coordinate{2, -1}};
61+
62+
Coordinate coord{topRight};
63+
for(const Coordinate& offset : adjacentTileOffsets) {
64+
for(int i = 0; i < sideLength; i++) {
65+
insertTile(coord, resources, rolls);
66+
coord.first += offset.first;
67+
coord.second += offset.second;
68+
}
69+
}
70+
}
71+
72+
void GameBoard::insertTile(Coordinate location, vector<resourceType>& resources, vector<int>& rolls) {
73+
if(rolls.back() == 0) {
74+
addResource(location.first, location.second, DESERT, rolls.back());
75+
rolls.pop_back();
76+
} else {
77+
addResource(location.first, location.second, resources.back(), rolls.back());
78+
resources.pop_back();
79+
rolls.pop_back();
80+
}
81+
}
82+
6383
GameBoard::GameBoard(std::vector<std::unique_ptr<Player>>&& players, const std::map<Coordinate, std::pair<resourceType, int>>& resourceLocations) : players(std::move(players)) {
6484
for(auto& resource : resourceLocations) {
6585
resources[resource.first] = std::unique_ptr<GamePiece>(new ResourceTile(*this, resource.first, resource.second.first, resource.second.second));

0 commit comments

Comments
 (0)