|
2 | 2 |
|
3 | 3 | #include <map> |
4 | 4 | #include <memory> |
5 | | - |
6 | | -using std::map; |
7 | | -using std::unique_ptr; |
8 | | - |
9 | 5 | #include <ctime> |
10 | 6 | #include <algorithm> |
| 7 | +#include <iostream> |
11 | 8 |
|
12 | 9 | #include "GameVisitor.h" |
| 10 | +#include "Serialization.h" |
| 11 | +#include "tinyxml2.h" |
13 | 12 |
|
14 | 13 | #define ADD_RESOURCE(x, y, res, val) (this->resources[Coordinate(x,y)] = \ |
15 | 14 | std::unique_ptr<GamePiece>(new ResourceTile(*this, Coordinate(x,y), res, val))) |
16 | 15 | #define DUMMY_BOARD //define to instantiate dummy board for debugging |
17 | 16 |
|
18 | 17 | using std::random_shuffle; |
19 | 18 | using std::time; |
| 19 | +using std::string; |
| 20 | +using std::map; |
| 21 | +using std::unique_ptr; |
| 22 | +using std::istream; |
| 23 | +using std::ostream; |
20 | 24 |
|
21 | 25 | GameBoard::GameBoard() { |
22 | 26 | init_resources(); |
23 | 27 | } |
24 | 28 |
|
25 | | -GameBoard::~GameBoard() { |
| 29 | +GameBoard::GameBoard(istream& in) { |
| 30 | + std::string gameXML; |
| 31 | + std::getline(in, gameXML, '\0'); //Read until the null character (end of file) and put in the string |
26 | 32 |
|
27 | | -} |
28 | | - |
29 | | - |
30 | | -int GameBoard::save_Board(std::string filename){ |
31 | | - std::ofstream file; |
32 | | - file.exceptions(std::ofstream::failbit | std::ofstream::badbit); |
33 | | - try { |
34 | | - filename = filename + ".wocs"; |
35 | | - file.open(filename.c_str()); |
36 | | - constructFileFromBoard(file); |
37 | | - file.close(); |
38 | | - return 0; |
39 | | - } catch (std::ofstream::failure e) { |
40 | | - std::cerr << "Exception opening/closing/writing file: " << e.what(); |
41 | | - } |
42 | | - return -1; |
43 | | -} |
44 | | - |
45 | | -int GameBoard::load_Board(std::string filename){ |
46 | | - std::ifstream file; |
47 | | - try { |
48 | | - filename = filename + ".wocs"; |
49 | | - file.open(filename.c_str()); |
50 | | - constructBoardFromFile(file); |
51 | | - file.close(); |
52 | | - return 0; |
53 | | - } catch (std::ifstream::failure e) { |
54 | | - std::cerr << "Exception opening/closing/reading file: " << e.what(); |
55 | | - } |
56 | | - return -1; |
57 | | -} |
58 | | - |
59 | | -int GameBoard::constructBoardFromFile(std::ifstream &file){ |
60 | | - //Parse and construct the board from the file |
61 | | - //@ TODO |
62 | | - std::string line; |
63 | | - if (file.is_open()) { |
64 | | - while (getline(file, line)) { |
65 | | - std::cout << line << '\n'; |
| 33 | + tinyxml2::XMLDocument doc; |
| 34 | + doc.Parse(gameXML.c_str()); |
| 35 | + |
| 36 | + auto hexTiles = doc.RootElement()->FirstChildElement("tiles"); |
| 37 | + |
| 38 | + for(tinyxml2::XMLElement* tileElement = hexTiles->FirstChildElement(); tileElement; tileElement = tileElement->NextSiblingElement()) { |
| 39 | + static const map<std::string, resourceType> textToType = { |
| 40 | + std::make_pair("wheat", WHEAT), |
| 41 | + std::make_pair("sheep", SHEEP), |
| 42 | + std::make_pair("stone", STONE), |
| 43 | + std::make_pair("brick", BRICK), |
| 44 | + std::make_pair("wood", WOOD), |
| 45 | + std::make_pair("desert", DESERT), |
| 46 | + }; |
| 47 | + std::string typeString = tileElement->FirstChildElement("type")->FirstChild()->Value(); |
| 48 | + auto it = textToType.find(typeString); |
| 49 | + if(it == textToType.end()) { |
| 50 | + throw std::runtime_error("Invalid type string"); |
66 | 51 | } |
| 52 | + resourceType type = it->second; |
| 53 | + |
| 54 | + int diceValue = fromString<int>(tileElement->FirstChildElement("value")->FirstChild()->Value()); |
| 55 | + |
| 56 | + Coordinate coord = xmlElementToCoord(*(tileElement->FirstChildElement("coordinate"))); |
| 57 | + |
| 58 | + resources[coord] = unique_ptr<ResourceTile>(new ResourceTile(*this, coord, type, diceValue)); |
67 | 59 | } |
68 | | - return 0; |
69 | 60 | } |
70 | 61 |
|
| 62 | +GameBoard::~GameBoard() { |
| 63 | + |
| 64 | +} |
71 | 65 |
|
72 | | -int GameBoard::constructFileFromBoard(std::ofstream &file){ |
73 | | - //Construct the file based on the structure of the board |
74 | | - //@ TODO |
75 | | - file << "Hello World!"; |
76 | | - return 0; |
| 66 | +void GameBoard::save(ostream& out) { |
| 67 | + XMLVisitor saver; |
| 68 | + accept(saver); |
| 69 | + tinyxml2::XMLPrinter printer; |
| 70 | + saver.getXMLDoc().Print(&printer); |
| 71 | + out << printer.CStr(); |
77 | 72 | } |
78 | 73 |
|
79 | 74 | const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getResources() const { |
|
0 commit comments