blob: 1f8f2b89ad9f296848d19d42bfed07bf0c06152d (
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
|
#include "spacebot.h"
#include "move_string_mapper.h"
#include "brain/neural_network.h"
#include <fstream>
Spacebot::Spacebot(std::string outputPath)
: _outputFilename(outputPath+"/move.txt"),
_networkConfigFilename("brain.nn"),
_gameState(std::ifstream(outputPath+"/map.txt"))
{
}
void Spacebot::writeNextMove()
{
Move move = chooseMove();
writeMove(move);
}
Move Spacebot::chooseMove()
{
auto sensorInputs = _gameState.toBitArray();
NeuralNetwork network(std::ifstream(_networkConfigFilename),
sensorInputs.size(),
static_cast<int>(Move::BUILD_SHIELD));
for (int i=0; i<sensorInputs.size(); ++i)
{
network.setInput(i, sensorInputs[i] ? 1 : 0);
}
int moveInt = network.findMaxOutputIndex();
return static_cast<Move>(moveInt);
}
void Spacebot::writeMove(const Move& move)
{
std::ofstream resultStream(_outputFilename);
resultStream << MoveStringMapper().toString(move) << std::endl;
return;
}
|