summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/enemy_bullet.h2
-rw-r--r--include/game_state.h4
-rw-r--r--include/player_missile.h1
-rw-r--r--include/shield.h1
-rw-r--r--include/spacebot.h2
-rw-r--r--include/spaceship.h2
-rw-r--r--src/game_state.cpp35
-rw-r--r--src/spacebot.cpp6
8 files changed, 40 insertions, 13 deletions
diff --git a/include/enemy_bullet.h b/include/enemy_bullet.h
index f08d988..db3fcf9 100644
--- a/include/enemy_bullet.h
+++ b/include/enemy_bullet.h
@@ -3,6 +3,8 @@
class EnemyBullet {
public:
EnemyBullet(int x, int y);
+ const static char ALIEN_MAP_CHAR = '|';
+ const static char ENEMY_MISSILE_MAP_CHAR = 'i';
private:
int x;
int y;
diff --git a/include/game_state.h b/include/game_state.h
index aa49fc8..defe691 100644
--- a/include/game_state.h
+++ b/include/game_state.h
@@ -6,12 +6,12 @@
#include "shield.h"
#include "spaceship.h"
#include <vector>
-#include <iostream>
+#include <string>
class GameState
{
public:
- GameState(std::istream& mapFile);
+ GameState(std::string mapFilename);
private:
std::vector<Alien> aliens;
std::vector<EnemyBullet> bullets;
diff --git a/include/player_missile.h b/include/player_missile.h
index 22c60ec..6f304df 100644
--- a/include/player_missile.h
+++ b/include/player_missile.h
@@ -4,6 +4,7 @@ class PlayerMissile
{
public:
PlayerMissile(int x, int y);
+ const static char MAP_CHAR = '!';
private:
int x;
int y;
diff --git a/include/shield.h b/include/shield.h
index b113bdd..bd64e3e 100644
--- a/include/shield.h
+++ b/include/shield.h
@@ -3,6 +3,7 @@
class Shield {
public:
Shield(int x, int y);
+ const static char MAP_CHAR = '-';
private:
int x;
int y;
diff --git a/include/spacebot.h b/include/spacebot.h
index b152197..9b89383 100644
--- a/include/spacebot.h
+++ b/include/spacebot.h
@@ -9,7 +9,7 @@ public:
Spacebot(std::string outputPath);
void writeNextMove();
private:
- std::string outputPath;
+ std::string outputFilename;
GameState gameState;
void writeMove(const Move& move);
Move chooseMove();
diff --git a/include/spaceship.h b/include/spaceship.h
index e4ee37b..5e6afd0 100644
--- a/include/spaceship.h
+++ b/include/spaceship.h
@@ -3,6 +3,8 @@
class Spaceship {
public:
Spaceship(int x, int y);
+ const static char ENEMY_MAP_CHAR = 'V';
+ const static char PLAYER_MAP_CHAR = 'A';
private:
int x;
int y;
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 <iostream>
+#include <fstream>
+#include <limits>
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<OPENING_LINES; ++i)
{
- file.ignore(numeric_limits<streamsize>::max(), '\n');
+ mapFile.ignore(std::numeric_limits<std::streamsize>::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 <string>
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: