Skip to content

Instantly share code, notes, and snippets.

@klmr
Last active August 29, 2015 14:06
Show Gist options
  • Save klmr/0816a6e64a6e8cc846b7 to your computer and use it in GitHub Desktop.
Save klmr/0816a6e64a6e8cc846b7 to your computer and use it in GitHub Desktop.
Rewrite of the main function of http://codereview.stackexchange.com/q/62493/308
Tile LoadFromFile(std::string filename) {
std::ifstream ifs{filename};
std::string line;
if (not getline(ifs, line))
throw "foo";
std::string name{line};
int numBoxes{};
int numTargets{};
Point playerStart{};
bool playerStartSet{};
std::vector<Tile> tiles(TileCount, Tile::Free);
int y{};
while (getline(ifs, line)) {
if (y > Level::MaxRows)
throw "foo";
int x{};
for (char c : line) {
if (x > Level::MaxCols)
throw "foo";
if (! validTile(c)) // TODO: implement `validTile`.
throw "foo";
auto tile = static_cast<Tile>(c);
switch (tile) {
case Tile::Free:
break;
case Tile::Box:
++numBoxes;
break;
case Tile::BoxOnTarget:
++numBoxes;
++numTargets;
break;
case Tile::Target:
++numTargets;
break;
case Tile::Wall:
break;
case Tile::PlayerStart:
if (playerStartSet)
throw "foo";
playerStartSet = true;
playerStart = Point{x, y};
tile = Tile::Free;
break;
}
tiles[indexof(x, y)] = tile;
++x;
}
++y;
}
if (numBoxes != numTargets)
throw "foo";
if (not playerStart)
throw "foo";
return Level{tiles, name, playerStart};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment