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 */
3031GameBoard::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+
6383GameBoard::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