Skip to content

Commit 3ccb129

Browse files
committed
Added functions for saving/loading the board
1 parent 53aadd5 commit 3ccb129

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

include/GameBoard.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <vector>
66
#include <memory>
77
#include <utility>
8+
#include <string>
9+
#include <iostream>
10+
#include <fstream>
811

912
#include "Util.h"
1013
#include "GamePiece.h"
@@ -13,11 +16,18 @@ class GameBoard {
1316
private:
1417
std::map<Coordinate, std::unique_ptr<GamePiece>> pieces;
1518
std::vector<std::unique_ptr<const Road>> roads;
19+
20+
int constructBoardFromFile(std::ifstream &file);
21+
int constructFileFromBoard(std::ofstream &file);
22+
1623
public:
1724
GameBoard();
1825
GameBoard(GameBoard&) = delete;
1926
~GameBoard();
2027
GameBoard& operator=(GameBoard&) = delete;
28+
29+
int saveBoardToFile(std::string filename);
30+
int loadBoardFromFile(std::string filename);
2131
};
2232

23-
#endif
33+
#endif

src/GameBoard.cpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,65 @@ GameBoard::GameBoard() {
66

77
GameBoard::~GameBoard() {
88

9-
}
9+
}
10+
11+
int GameBoard::saveBoardToFile(std::string filename){
12+
std::ofstream file;
13+
file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
14+
try {
15+
filename = filename + ".wocs";
16+
file.open(filename.c_str(), std::fstream::out | std::fstream::trunc);
17+
constructFileFromBoard(file);
18+
19+
} catch (std::ofstream::failure e){
20+
std::cerr << "Exception opening/reading/closing file \n";
21+
}
22+
23+
24+
25+
return 0;
26+
}
27+
28+
int GameBoard::loadBoardFromFile(std::string filename){
29+
std::ifstream file;
30+
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
31+
try {
32+
filename = filename + ".wocs";
33+
file.open(filename.c_str(), std::fstream::in);
34+
constructBoardFromFile(file);
35+
36+
file.close();
37+
} catch (std::ifstream::failure e) {
38+
std::cerr << "Exception opening/reading/closing file\n";
39+
}
40+
return 0;
41+
}
42+
43+
int GameBoard::constructBoardFromFile(std::ifstream &file){
44+
//Parse and construct the board from the file
45+
//@ TODO
46+
while (!file.eof()) {
47+
std::cout << file.get();
48+
}
49+
return 0;
50+
}
51+
52+
53+
int GameBoard::constructFileFromBoard(std::ofstream &file){
54+
//Construct the file based on the structure of the board
55+
//@ TODO
56+
file << "Hello World!";
57+
return 0;
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+

0 commit comments

Comments
 (0)