summaryrefslogtreecommitdiff
path: root/2015-spacebot/src/spacebot.cpp
blob: 17d20b525123b8f10d14661b00ca9d4471a83b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "spacebot.h"
#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"),
      _brainFilename(brainFilename),
      _gameState(std::ifstream(outputPath+"/map.txt"))
{
}

void Spacebot::writeNextMove()
{
    Move move = chooseMove();
    writeMove(move);
}

Move Spacebot::chooseMove()
{
    auto sensorInputs = _gameState.toBitArray();

    if (!sensorInputs.at(51) || !sensorInputs.at(53))
    {
        return Move::BUILD_SHIELD;
    }
    else if (sensorInputs.at(55))
    {
        return Move::SHOOT;
    }
    else if (sensorInputs.at(60) && !sensorInputs.at(59))
    {
        return Move::BUILD_MISSILE_CONTROLLER;
    }
    else
    {
        return Move::NOTHING;
    }
}

void Spacebot::writeMove(const Move& move)
{
    std::ofstream resultStream(_outputFilename);
    resultStream << MoveStringMapper().toString(move) << std::endl;
    return;
}