diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-25 12:59:47 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-25 12:59:47 +0000 |
commit | b4336dd8f5bcd7370d9f439ea46829d7fce59e76 (patch) | |
tree | f51149312b636317aa72679879360dea26bab63d /src/board_state_parser.cpp | |
parent | b39845e0d36e5806b3f4629b1e5eeca4164572c0 (diff) | |
parent | 4e79ea68d54305e6705a3a8e83a730508b9f1c1b (diff) | |
download | ChessAI-master.tar.gz ChessAI-master.zip |
Diffstat (limited to 'src/board_state_parser.cpp')
-rw-r--r-- | src/board_state_parser.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp new file mode 100644 index 0000000..288a491 --- /dev/null +++ b/src/board_state_parser.cpp @@ -0,0 +1,61 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 15/01/17 + * + * ----------------------------------------------------------------------------- + * + * Implementation of the Board state class + * + */ + +#include "board_state_parser.hpp" +#include "chess_constats.hpp" + +using namespace ymhChessAI; + + +BoardStateParser::BoardStateParser() : boardFile("res/default.board", std::ifstream::in) { +} + +BoardStateParser::BoardStateParser(const std::string& boardFileName) : boardFile(boardFileName.c_str()) { +} + +BoardStateParser::~BoardStateParser() { + boardFile.close(); +} + +void BoardStateParser::populateBoardState() { + std::string currentLine; + + boardStateVector.reserve(ChessConstants::BOARD_SIZE); + + if(!boardFile.is_open()) + throw "Error: Can't open file"; + + while(getline(boardFile, currentLine)) + boardStateVector[stringToInt(currentLine)] = getStateFromLine(currentLine); +} + +unsigned BoardStateParser::stringToInt(const std::string& str) { + unsigned finalInt = 0; + bool foundWhiteSpace = false; + + for(char character : str) + if(character == ' ') + foundWhiteSpace = true; + else if(!foundWhiteSpace && character >= '0' && character <= '9') + finalInt = finalInt * 10 + character - '0'; + + return finalInt; +} + +std::string BoardStateParser::getStateFromLine(const std::string& str) { + std::string state; + + state = str.substr(2, str.length() - 2); + + return state; +} |