From a197a92893cff416b63da359db8c8059c7f333bf Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Wed, 3 Jun 2015 22:11:59 +0200 Subject: Added remaining objects for game state --- src/game_state.cpp | 35 ++++++++++++++++++++++++++++------- src/spacebot.cpp | 6 +++--- 2 files changed, 31 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/game_state.cpp b/src/game_state.cpp index 48a9e42..340bcd3 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -1,27 +1,48 @@ #include "game_state.h" #include +#include +#include const int OPENING_LINES = 6; const int GAME_AREA_LINES = 25; -GameState::GameState(std::istream& file) +GameState::GameState(std::string mapFilename) { + std::ifstream mapFile(mapFilename); for (int i=0; i::max(), '\n'); + mapFile.ignore(std::numeric_limits::max(), '\n'); } - int x = -1; int y = 0; - char nextChar = ' '; - while (char nextChar = file.get()) + + for (int x=-1; y <= GAME_AREA_LINES; ++x) { - ++x; + char nextChar = mapFile.get(); + if (nextChar == EOF) + { + break; + } + switch (nextChar) { - case Alien.MAP_CHAR: + case Alien::MAP_CHAR: aliens.push_back(Alien(x,y)); break; + case EnemyBullet::ALIEN_MAP_CHAR: + case EnemyBullet::ENEMY_MISSILE_MAP_CHAR: + bullets.push_back(EnemyBullet(x,y)); + break; + case PlayerMissile::MAP_CHAR: + missiles.push_back(PlayerMissile(x,y)); + break; + case Shield::MAP_CHAR: + shields.push_back(Shield(x,y)); + break; + case Spaceship::ENEMY_MAP_CHAR: + case Spaceship::PLAYER_MAP_CHAR: + spaceships.push_back(Spaceship(x,y)); + break; case '\n': ++y; x = -1; diff --git a/src/spacebot.cpp b/src/spacebot.cpp index 7460fe3..a6c4953 100644 --- a/src/spacebot.cpp +++ b/src/spacebot.cpp @@ -4,8 +4,8 @@ #include Spacebot::Spacebot(std::string outputPath) - : outputPath(std::move(outputPath)), - gameState(std::ifstream(outputPath+"/map.txt")) + : outputFilename(outputPath+"/move.txt"), + gameState(outputPath+"/map.txt") { } @@ -27,7 +27,7 @@ Move Spacebot::chooseMove() void Spacebot::writeMove(const Move& move) { - std::ofstream resultStream(outputPath+"/move.txt"); + std::ofstream resultStream(outputFilename); switch (move) { case Move::NOTHING: -- cgit v1.2.3