diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | include/alien.h | 1 | ||||
-rw-r--r-- | include/game_state.h | 2 | ||||
-rw-r--r-- | src/game_state.cpp | 31 | ||||
-rw-r--r-- | src/spacebot.cpp | 2 |
5 files changed, 35 insertions, 2 deletions
@@ -7,3 +7,4 @@ cppbot *.sdf *.suo Debug/ +Makefile diff --git a/include/alien.h b/include/alien.h index 476990b..2f7d596 100644 --- a/include/alien.h +++ b/include/alien.h @@ -3,6 +3,7 @@ class Alien { public: Alien(int x, int y); + const static char MAP_CHAR = 'x'; private: int x; int y; diff --git a/include/game_state.h b/include/game_state.h index abb2efa..aa49fc8 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -11,7 +11,7 @@ class GameState { public: - GameState(const std::istream& mapFile){} + GameState(std::istream& mapFile); private: std::vector<Alien> aliens; std::vector<EnemyBullet> bullets; diff --git a/src/game_state.cpp b/src/game_state.cpp new file mode 100644 index 0000000..48a9e42 --- /dev/null +++ b/src/game_state.cpp @@ -0,0 +1,31 @@ +#include "game_state.h" +#include <iostream> + +const int OPENING_LINES = 6; +const int GAME_AREA_LINES = 25; + +GameState::GameState(std::istream& file) +{ + for (int i=0; i<OPENING_LINES; ++i) + { + file.ignore(numeric_limits<streamsize>::max(), '\n'); + } + + int x = -1; + int y = 0; + char nextChar = ' '; + while (char nextChar = file.get()) + { + ++x; + switch (nextChar) + { + case Alien.MAP_CHAR: + aliens.push_back(Alien(x,y)); + break; + case '\n': + ++y; + x = -1; + break; + } + } +} diff --git a/src/spacebot.cpp b/src/spacebot.cpp index b297eb3..7460fe3 100644 --- a/src/spacebot.cpp +++ b/src/spacebot.cpp @@ -5,7 +5,7 @@ Spacebot::Spacebot(std::string outputPath) : outputPath(std::move(outputPath)), - gameState(std::ifstream(outputPath+"/map.txt", std::ifstream::in)) + gameState(std::ifstream(outputPath+"/map.txt")) { } |