summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2015-06-03 23:03:40 +0200
committerJustin Worthe <justin.worthe@gmail.com>2015-06-03 23:03:40 +0200
commit140b51f2bcd9c0aaa5b536d85702064a93530188 (patch)
tree5635713a49790726e2f99853f2feb38cd7bea578 /src
parenta197a92893cff416b63da359db8c8059c7f333bf (diff)
Moved mapping move to string to its own file
Diffstat (limited to 'src')
-rw-r--r--src/move_string_mapper.cpp21
-rw-r--r--src/spacebot.cpp33
2 files changed, 24 insertions, 30 deletions
diff --git a/src/move_string_mapper.cpp b/src/move_string_mapper.cpp
new file mode 100644
index 0000000..37d5d7e
--- /dev/null
+++ b/src/move_string_mapper.cpp
@@ -0,0 +1,21 @@
+#include "move_string_mapper.h"
+
+#include "move.h"
+#include <map>
+#include <string>
+
+MoveStringMapper::MoveStringMapper()
+{
+ moveMap[Move::NOTHING] = "Nothing";
+ moveMap[Move::MOVE_LEFT] = "MoveLeft";
+ moveMap[Move::MOVE_RIGHT] = "MoveRight";
+ moveMap[Move::SHOOT] = "Shoot";
+ moveMap[Move::BUILD_ALIEN_FACTORY] = "BuildAlienFactory";
+ moveMap[Move::BUILD_MISSILE_CONTROLLER] = "BuildMissileController";
+ moveMap[Move::BUILD_SHIELD] = "BuildShield";
+}
+
+std::string MoveStringMapper::toString(const Move &move)
+{
+ return moveMap[move];
+}
diff --git a/src/spacebot.cpp b/src/spacebot.cpp
index a6c4953..f30bb34 100644
--- a/src/spacebot.cpp
+++ b/src/spacebot.cpp
@@ -1,4 +1,5 @@
#include "spacebot.h"
+#include "move_string_mapper.h"
#include <random>
#include <fstream>
#include <string>
@@ -21,42 +22,14 @@ Move Spacebot::chooseMove()
int max = static_cast<int>(Move::BUILD_SHIELD);
std::random_device rd;
std::mt19937 gen(rd());
- std::uniform_int_distribution<> dis(min, max);
+ std::uniform_int_distribution<int> dis(min, max);
return static_cast<Move>(dis(gen));
}
void Spacebot::writeMove(const Move& move)
{
std::ofstream resultStream(outputFilename);
- switch (move)
- {
- case Move::NOTHING:
- resultStream << "Nothing";
- break;
- case Move::MOVE_LEFT:
- resultStream << "MoveLeft";
- break;
- case Move::MOVE_RIGHT:
- resultStream << "MoveRight";
- break;
- case Move::SHOOT:
- resultStream << "Shoot";
- break;
- case Move::BUILD_ALIEN_FACTORY:
- resultStream << "BuildAlienFactory";
- break;
- case Move::BUILD_MISSILE_CONTROLLER:
- resultStream << "BuildMissileController";
- break;
- case Move::BUILD_SHIELD:
- resultStream << "BuildShield";
- break;
- default:
- resultStream << "MoveLeft";
- }
-
- resultStream << std::endl;
- resultStream.flush();
+ resultStream << MoveStringMapper().toString(move) << std::endl;
return;
}