diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2015-08-12 23:18:56 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2015-08-12 23:18:56 +0200 |
commit | 1da1c8d213ce710a9cafdd560d0d3d3c7fb74d70 (patch) | |
tree | 3ec371e909af1bda730b6325a40fbb74ac844806 | |
parent | 1049b6acb37c244ef7470b2e7fe4145a616d3df3 (diff) |
Changing how gamestate is interpreted into network
-rw-r--r-- | bot.json | 5 | ||||
-rw-r--r-- | include/game_state.h | 8 | ||||
-rw-r--r-- | src/game_state.cpp | 87 | ||||
-rw-r--r-- | src/spacebot.cpp | 2 | ||||
-rw-r--r-- | test/game_state.cpp | 4 |
5 files changed, 76 insertions, 30 deletions
diff --git a/bot.json b/bot.json new file mode 100644 index 0000000..d70d03c --- /dev/null +++ b/bot.json @@ -0,0 +1,5 @@ +{ + "nickName": "Spacebot", + "author": "Justin Worthe", + "email", "justin.worthe@gmail.com" +} diff --git a/include/game_state.h b/include/game_state.h index 482dd7d..880557e 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -8,6 +8,7 @@ #include <vector> #include <string> #include <istream> +#include <memory> class GameState { @@ -19,7 +20,9 @@ public: const std::vector<EnemyBullet>& bullets() const { return _bullets; } const std::vector<PlayerMissile>& missiles() const { return _missiles; } const std::vector<Shield>& shields() const { return _shields; } - const std::vector<Spaceship>& spaceships() const { return _spaceships; } + + const std::unique_ptr<Spaceship>& playerSpaceship() const { return _playerSpaceship; } + const std::unique_ptr<Spaceship>& enemySpaceship() const { return _enemySpaceship; } std::vector<bool> toBitArray() const; @@ -28,7 +31,8 @@ private: std::vector<EnemyBullet> _bullets; std::vector<PlayerMissile> _missiles; std::vector<Shield> _shields; - std::vector<Spaceship> _spaceships; + std::unique_ptr<Spaceship> _playerSpaceship; + std::unique_ptr<Spaceship> _enemySpaceship; int addEntity(int x, int y, char type); }; diff --git a/src/game_state.cpp b/src/game_state.cpp index e6deae7..a302bfc 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -60,8 +60,10 @@ int GameState::addEntity(int x, int y, char type) _shields.push_back(Shield(x,y)); return 1; case Spaceship::ENEMY_MAP_CHAR: + _enemySpaceship = std::unique_ptr<Spaceship>(new Spaceship(x+1, y)); + return 3; case Spaceship::PLAYER_MAP_CHAR: - _spaceships.push_back(Spaceship(x+1,y)); + _playerSpaceship = std::unique_ptr<Spaceship>(new Spaceship(x+1, y)); return 3; } return 1; @@ -85,51 +87,84 @@ void GameState::logState() { std::cout << "Shield" << shield.coords() << std::endl; } - for (auto spaceship : _spaceships) + if (_playerSpaceship) { - std::cout << "Spaceship" << spaceship.coords() << std::endl; + std::cout << "Player Spaceship" << _playerSpaceship->coords() << std::endl; + } + if (_enemySpaceship) + { + std::cout << "Enemy Spaceship" << _enemySpaceship->coords() << std::endl; } } -std::vector<bool> GameState::toBitArray() const +int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int resultIfRight) { - std::vector<bool> result; - - std::vector<bool> alienNodes((GAME_AREA_LINES-2) * (GAME_WIDTH-2), false); - for (auto alien : _aliens) + int currentRowWidth = bottomWidth; + int currentRowOffset = 0; + for (int i=0; i<y; ++i) { - alienNodes.at((alien.y()-1)*(GAME_WIDTH-2)+(alien.x()-1)) = true; + currentRowOffset += currentRowWidth; + currentRowWidth += 2; } - result.insert(result.end(), alienNodes.begin(), alienNodes.end()); - std::vector<bool> bulletNodes((GAME_AREA_LINES-2) * (GAME_WIDTH-2), false); - for (auto bullet : _bullets) + if (x > currentRowWidth/2) + { + return resultIfRight; + } + else if (x < -currentRowWidth/2) + { + return resultIfLeft; + } + else { - bulletNodes.at((bullet.y()-1)*(GAME_WIDTH-2)+(bullet.x()-1)) = true; + return currentRowOffset + currentRowWidth/2 + x; } - result.insert(result.end(), bulletNodes.begin(), bulletNodes.end()); +} - std::vector<bool> missileNodes((GAME_AREA_LINES-2) * (GAME_WIDTH-2), false); - for (auto missile : _missiles) +int getRectangularOffset(int width, int x, int y) +{ + int currentRowOffset = width*y; + return currentRowOffset + width/2 + x; +} + +std::vector<bool> GameState::toBitArray() const +{ + std::vector<bool> result(171); + + int alienOffset = 0; //Aliens are 0 to 100 + int bulletOffset = 101; //Bullets are 101 to 135 + int shieldOffset = 136; //Shields are 136 to 150 + int missileOffset = 151; //Missile info is 151 to 152 + int positionOffset = 153; //Position is 153 to 168 + int existingBuildingsOffset = 169; //Existing buildings is 169 to 170 + int canBuildOffset = 171; //Can build here is 171 + + int playerX = GAME_WIDTH/2; + int playerY = GAME_AREA_LINES-3; + if (_playerSpaceship) { - missileNodes.at((missile.y()-1)*(GAME_WIDTH-2)+(missile.x()-1)) = true; + playerX = _playerSpaceship->x(); } - result.insert(result.end(), missileNodes.begin(), missileNodes.end()); - std::vector<bool> shieldNodes(2 * (GAME_WIDTH-2), false); - for (auto shield : _shields) + + for (auto alien : _aliens) { - int row = shield.y() > GAME_AREA_LINES/2 ? 1 : 0; - shieldNodes.at(row*(GAME_WIDTH-2)+(shield.x()-1)) = true; + if (alien.y() >= playerY || alien.y() < playerY-9) + { + continue; + } + + result.at(alienOffset + getPyramidOffset(3, alien.x()-playerX, playerY-1-alien.y(), 99, 100)) = true; } - for (auto spaceship : _spaceships) + for (auto bullet : _bullets) { - std::bitset<5> spaceshipBits(spaceship.x()); - for (int i=0; i<spaceshipBits.size(); ++i) + if (bullet.y() >= playerY || bullet.y() < playerY-5 || bullet.x() > playerX+3 || bullet.x() < playerX-3) { - result.push_back(spaceshipBits[i]); + continue; } + + result.at(bulletOffset + getRectangularOffset(7, bullet.x()-playerX, playerY-1-bullet.y())) = true; } return result; diff --git a/src/spacebot.cpp b/src/spacebot.cpp index 418756c..23cce0c 100644 --- a/src/spacebot.cpp +++ b/src/spacebot.cpp @@ -2,6 +2,7 @@ #include "move_string_mapper.h" #include "brain/neural_network.h" #include <fstream> +#include <iostream> Spacebot::Spacebot(std::string outputPath, std::string brainFilename) : _outputFilename(outputPath+"/move.txt"), @@ -19,6 +20,7 @@ void Spacebot::writeNextMove() Move Spacebot::chooseMove() { auto sensorInputs = _gameState.toBitArray(); + std::cout << sensorInputs.size() << std::endl; NeuralNetwork network(std::ifstream(_brainFilename), sensorInputs, diff --git a/test/game_state.cpp b/test/game_state.cpp index 467a665..a9975f2 100644 --- a/test/game_state.cpp +++ b/test/game_state.cpp @@ -82,8 +82,8 @@ SCENARIO("game state is read from istream") THEN("the spaceships are read correctly") { - auto spaceships = state.spaceships(); - REQUIRE(spaceships.size() == 2); + REQUIRE(state.playerSpaceship()->x() == 9); + REQUIRE(state.playerSpaceship()->y() == 22); } } } |