summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/game_state.h16
-rw-r--r--src/game_state.cpp25
-rw-r--r--src/spacebot.cpp3
-rw-r--r--test/game_state.cpp59
4 files changed, 82 insertions, 21 deletions
diff --git a/include/game_state.h b/include/game_state.h
index 1b35fc6..7ccef54 100644
--- a/include/game_state.h
+++ b/include/game_state.h
@@ -7,18 +7,22 @@
#include "spaceship.h"
#include <vector>
#include <string>
+#include <istream>
class GameState
{
public:
- GameState(std::string mapFilename);
+ GameState(std::istream &&mapFile);
void logState();
+
+ const std::vector<Alien>& aliens() const { return _aliens; }
+
private:
- std::vector<Alien> aliens;
- std::vector<EnemyBullet> bullets;
- std::vector<PlayerMissile> missiles;
- std::vector<Shield> shields;
- std::vector<Spaceship> spaceships;
+ std::vector<Alien> _aliens;
+ std::vector<EnemyBullet> _bullets;
+ std::vector<PlayerMissile> _missiles;
+ std::vector<Shield> _shields;
+ std::vector<Spaceship> _spaceships;
int addEntity(int x, int y, char type);
};
diff --git a/src/game_state.cpp b/src/game_state.cpp
index 59eb8c1..d99ca12 100644
--- a/src/game_state.cpp
+++ b/src/game_state.cpp
@@ -6,7 +6,7 @@
const int OPENING_LINES = 6;
const int GAME_AREA_LINES = 25;
-void moveToNextChar(int &x, int &y, int &width, char &nextChar, std::ifstream &mapFile)
+void moveToNextChar(int &x, int &y, int &width, char &nextChar, std::istream &mapFile)
{
if (nextChar == '\n')
{
@@ -24,9 +24,8 @@ void moveToNextChar(int &x, int &y, int &width, char &nextChar, std::ifstream &m
nextChar = mapFile.get();
}
-GameState::GameState(std::string mapFilename)
+GameState::GameState(std::istream &&mapFile)
{
- std::ifstream mapFile(mapFilename);
for (int i=0; i<OPENING_LINES; ++i)
{
mapFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@@ -46,21 +45,21 @@ int GameState::addEntity(int x, int y, char type)
switch (type)
{
case Alien::MAP_CHAR:
- aliens.push_back(Alien(x,y));
+ _aliens.push_back(Alien(x,y));
return 1;
case EnemyBullet::ALIEN_MAP_CHAR:
case EnemyBullet::ENEMY_MISSILE_MAP_CHAR:
- bullets.push_back(EnemyBullet(x,y));
+ _bullets.push_back(EnemyBullet(x,y));
return 1;
case PlayerMissile::MAP_CHAR:
- missiles.push_back(PlayerMissile(x,y));
+ _missiles.push_back(PlayerMissile(x,y));
return 1;
case Shield::MAP_CHAR:
- shields.push_back(Shield(x,y));
+ _shields.push_back(Shield(x,y));
return 1;
case Spaceship::ENEMY_MAP_CHAR:
case Spaceship::PLAYER_MAP_CHAR:
- spaceships.push_back(Spaceship(x+1,y));
+ _spaceships.push_back(Spaceship(x+1,y));
return 3;
}
return 1;
@@ -68,23 +67,23 @@ int GameState::addEntity(int x, int y, char type)
void GameState::logState()
{
- for (auto alien : aliens)
+ for (auto alien : _aliens)
{
std::cout << "Alien " << alien.coords() << std::endl;
}
- for (auto bullet : bullets)
+ for (auto bullet : _bullets)
{
std::cout << "Enemy Bullet" << bullet.coords() << std::endl;
}
- for (auto missile : missiles)
+ for (auto missile : _missiles)
{
std::cout << "Player Missile" << missile.coords() << std::endl;
}
- for (auto shield : shields)
+ for (auto shield : _shields)
{
std::cout << "Shield" << shield.coords() << std::endl;
}
- for (auto spaceship : spaceships)
+ for (auto spaceship : _spaceships)
{
std::cout << "Spaceship" << spaceship.coords() << std::endl;
}
diff --git a/src/spacebot.cpp b/src/spacebot.cpp
index f30bb34..5f87df9 100644
--- a/src/spacebot.cpp
+++ b/src/spacebot.cpp
@@ -2,11 +2,10 @@
#include "move_string_mapper.h"
#include <random>
#include <fstream>
-#include <string>
Spacebot::Spacebot(std::string outputPath)
: outputFilename(outputPath+"/move.txt"),
- gameState(outputPath+"/map.txt")
+ gameState(std::ifstream(outputPath+"/map.txt"))
{
}
diff --git a/test/game_state.cpp b/test/game_state.cpp
new file mode 100644
index 0000000..0887b05
--- /dev/null
+++ b/test/game_state.cpp
@@ -0,0 +1,59 @@
+#include "catch.hpp"
+#include <sstream>
+
+#include "game_state.h"
+
+SCENARIO("game state is read from istream")
+{
+ GIVEN("a valid map file")
+ {
+ std::stringstream file;
+ file << "###################" << std::endl;
+ file << "# Node Sample Bot #" << std::endl;
+ file << "# Round: 1 #" << std::endl;
+ file << "# Kills: 0 #" << std::endl;
+ file << "# Lives: 2 #" << std::endl;
+ file << "# Missiles: 0/1 #" << std::endl;
+ file << "###################" << std::endl;
+ file << "# #" << std::endl;
+ file << "# VVV #" << std::endl;
+ file << "# --- --- #" << std::endl;
+ file << "# --- --- #" << std::endl;
+ file << "# --- --- #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# x x x #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# x x x #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# #" << std::endl;
+ file << "# --- --- #" << std::endl;
+ file << "# --- --- #" << std::endl;
+ file << "# --- --- #" << std::endl;
+ file << "# AAA #" << std::endl;
+ file << "# #" << std::endl;
+ file << "###################" << std::endl;
+ file << "# Missiles: 0/1 #" << std::endl;
+ file << "# Lives: 2 #" << std::endl;
+ file << "# Kills: 0 #" << std::endl;
+ file << "# Round: 1 #" << std::endl;
+ file << "# Node Sample Bot #" << std::endl;
+ file << "###################" << std::endl;
+
+ WHEN ("the game state is initilized")
+ {
+ GameState state(std::move(file));
+ THEN("the map is read correctly")
+ {
+ auto aliens = state.aliens();
+ REQUIRE(aliens.size() == 6);
+ }
+ }
+ }
+}