Skip to content

Commit 40a3f9e

Browse files
committed
Added ResourceTile serialization and serialization framework
1 parent 76e73d9 commit 40a3f9e

File tree

9 files changed

+90
-77
lines changed

9 files changed

+90
-77
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ $(EXECUTABLE): $(ALLFILES)
2121
tests:
2222
cd UnitTest++ && $(MAKE) libUnitTest++.a
2323
cd tests && $(MAKE)
24+
25+
.PHONY: clean
26+
clean:
27+
rm -f $(EXECUTABLE)
28+
rm -f obj/*.o

include/GameBoard.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ class GameBoard {
2424
std::map<Coordinate, std::unique_ptr<GamePiece>> resources;
2525
std::vector<std::unique_ptr<Road>> roads;
2626

27-
int constructBoardFromFile(std::ifstream &file);
28-
int constructFileFromBoard(std::ofstream &file);
2927
public:
3028
GameBoard();
29+
GameBoard(std::istream& in);
3130
GameBoard(GameBoard&) = delete;
3231
~GameBoard();
3332
GameBoard& operator=(GameBoard&) = delete;
3433

35-
int save_Board(std::string filename);
36-
int load_Board(std::string filename);
34+
void save(std::ostream& out);
35+
3736
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getResources() const;
3837

3938
std::vector<Settlement*> GetNeighboringSettlements(Coordinate location);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef XML_VISITOR_H
2-
#define XML_VISITOR_H
1+
#ifndef SERIALIZATION_H
2+
#define SERIALIZATION_H
33

44
#include "GameVisitor.h"
55
#include "Util.h"
@@ -25,4 +25,6 @@ class XMLVisitor : public GameVisitor {
2525
const tinyxml2::XMLDocument& getXMLDoc() const;
2626
};
2727

28+
Coordinate xmlElementToCoord(const tinyxml2::XMLElement& element);
29+
2830
#endif

include/Util.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22
#define UTIL_H
33

44
#include <utility>
5+
#include <sstream>
56

67
typedef std::pair<int, int> Coordinate;
78

9+
template<class T>
10+
T fromString(const std::string& s) {
11+
std::stringstream stream(s);
12+
T ret;
13+
stream >> ret;
14+
return ret;
15+
}
16+
17+
template<class T>
18+
std::string toString(T t) {
19+
std::stringstream stream;
20+
stream << t;
21+
return stream.str();
22+
}
23+
824
#endif

src/GameBoard.cpp

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,73 @@
22

33
#include <map>
44
#include <memory>
5-
6-
using std::map;
7-
using std::unique_ptr;
8-
95
#include <ctime>
106
#include <algorithm>
7+
#include <iostream>
118

129
#include "GameVisitor.h"
10+
#include "Serialization.h"
11+
#include "tinyxml2.h"
1312

1413
#define ADD_RESOURCE(x, y, res, val) (this->resources[Coordinate(x,y)] = \
1514
std::unique_ptr<GamePiece>(new ResourceTile(*this, Coordinate(x,y), res, val)))
1615
#define DUMMY_BOARD //define to instantiate dummy board for debugging
1716

1817
using std::random_shuffle;
1918
using std::time;
19+
using std::string;
20+
using std::map;
21+
using std::unique_ptr;
22+
using std::istream;
23+
using std::ostream;
2024

2125
GameBoard::GameBoard() {
2226
init_resources();
2327
}
2428

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
2632

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");
6651
}
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));
6759
}
68-
return 0;
6960
}
7061

62+
GameBoard::~GameBoard() {
63+
64+
}
7165

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();
7772
}
7873

7974
const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getResources() const {
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "XMLVisitor.h"
1+
#include "Serialization.h"
22

33
#include <map>
44
#include <utility>
@@ -89,6 +89,10 @@ void XMLVisitor::visit(ResourceTile& tile) {
8989

9090
newTileElement->InsertEndChild(coordinateElement(tile.getCoordinates()));
9191

92+
XMLElement* tileValueElement = xmldoc.NewElement("value");
93+
tileValueElement->InsertEndChild(xmldoc.NewText(toString(tile.getDiceValue()).c_str()));
94+
newTileElement->InsertEndChild(tileValueElement);
95+
9296
tilesElement->InsertEndChild(newTileElement);
9397
}
9498

@@ -103,3 +107,7 @@ const tinyxml2::XMLDocument& XMLVisitor::getXMLDoc() const {
103107
return xmldoc;
104108
}
105109

110+
Coordinate xmlElementToCoord(const XMLElement& elem) {
111+
return Coordinate(elem.IntAttribute("u"), elem.IntAttribute("v"));
112+
}
113+

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ OBJECTS_NO_MAIN := $(subst $(OBJ_HOME)/main.o,,$(OBJECTS))
77
run: $(TEST_FILE)
88
./$(TEST_FILE)
99

10-
$(TEST_FILE): ../$(EXECUTABLE) $(TEST_LINK_FILES)
10+
$(TEST_FILE): ../$(EXECUTABLE) $(TEST_LINK_FILES) $(wildcard *.cpp)
1111
$(CXX) $(CXXFLAGS) -I$(TEST_INCLUDE) $(wildcard *.cpp) -o $@ $(TEST_LINK_FILES) $(OBJECTS_NO_MAIN) $(LDFLAGS)
1212

1313
.PHONY: clean

tests/testSerialization.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
#include <UnitTest++.h>
22

3-
#include "XMLVisitor.h"
3+
#include "Serialization.h"
44
#include "GameBoard.h"
55
#include "Player.h"
66

77
#include "tinyxml2.h"
88

99
#include <iostream>
10+
#include <sstream>
11+
12+
using std::stringstream;
1013

1114
TEST(xmlPrint) {
1215
GameBoard testBoard;
1316

14-
XMLVisitor vs;
15-
testBoard.accept(vs);
17+
stringstream stream;
18+
testBoard.save(stream);
1619

17-
tinyxml2::XMLPrinter printer;
18-
vs.getXMLDoc().Print( &printer );
20+
GameBoard copyBoard(stream);
1921

20-
std::cout << printer.CStr() << std::endl;
2122
}

tests/test_GameBoard.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,3 @@
77

88
#include "GameBoard.h"
99
#include "UnitTest++.h"
10-
11-
12-
TEST(saveFile_simple){
13-
GameBoard * test_board = new GameBoard;
14-
CHECK(test_board->save_Board("test_1") == 0);
15-
delete(test_board);
16-
}
17-
18-
TEST(loadFile_simple){
19-
GameBoard * test_board = new GameBoard;
20-
CHECK(test_board->load_Board("test_1") == 0);
21-
delete(test_board);
22-
}

0 commit comments

Comments
 (0)