aboutsummaryrefslogtreecommitdiffstats
path: root/src/board_state_parser.cpp
blob: 288a49169083634fecd7b84c62052a1de3e583f7 (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
49
50
51
52
53
54
55
56
57
58
59
60
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;
}