-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathsokoban_input.cpp
49 lines (44 loc) · 1.23 KB
/
sokoban_input.cpp
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
49
#include "stdafx.h"
#include "sokoban_input.h"
SokobanInput::SokobanInput(const FilePath& l, const FilePath& s) : levelsPath(l), statePath(s) {
ifstream input(levelsPath.getPath());
CHECK(input) << "Failed to load sokoban data from " << levelsPath;
}
optional<Table<char>> SokobanInput::readTable(ifstream& input) {
Vec2 size;
input >> size.x >> size.y;
if (!input)
return none;
Table<char> ret(size);
for (int y : ret.getBounds().getYRange()) {
string s;
input >> s;
CHECK(s.size() == ret.getBounds().width());
for (int x : All(s))
ret[x][y] = s[x];
}
return ret;
}
static int getTableNum(const FilePath& path) {
int ret = 0;
ifstream in(path.getPath());
if (in)
in >> ret;
return ret;
}
Table<char> SokobanInput::getNext() {
ifstream input(levelsPath.getPath());
CHECK(input) << "Failed to load sokoban data from " << levelsPath;
vector<Table<char>> rest;
while(1) {
if (auto next = readTable(input))
rest.push_back(*next);
else
break;
}
CHECK(!rest.empty()) << "Failed to load sokoban data from " << levelsPath;
input.close();
int curLevel = getTableNum(statePath);
ofstream(statePath.getPath()) << (curLevel + 1);
return rest[curLevel % rest.size()];
}