-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateManager.cpp
More file actions
48 lines (39 loc) · 912 Bytes
/
StateManager.cpp
File metadata and controls
48 lines (39 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "StateManager.h"
#include <fstream>
#include <iostream>
#include <cstring>
#include <sstream>
/*
Save the game to a text file.
*/
void StateManager::saveGame(std::string saveFile){
std::ofstream sf;
sf.open(saveFile);
sf << "clock:" << clock;
sf.close();
}
/*
Load the game from a text file.
*/
void StateManager::loadGame(std::string saveFile){
std::ifstream sf;
std::string line;
std::string token1;
std::string token2;
sf.open(saveFile);
//if the file exists, open it:
if (sf.is_open()){
//Go line by line through the file:
while ( getline (sf,line)){
//Delimit each line and interpret the results
std::istringstream iss(line);
std::getline(iss, token1, ':');
std::getline(iss, token2, ':');
//Go through each possible field and update the game accordingly.
if(token1.compare("clock")==0){
clock = (int) stoi(token2);
}
}
}
sf.close();
}