-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
92 lines (76 loc) · 2.07 KB
/
Parser.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Parser.h"
Parser::Parser(Board* board) {
this->board = board;
}
bool Parser::ParseCommand() {
std::string line;
int c;
while ((c = getchar()) != EOF) {
if (c == '\n') break;
line += (char)c;
}
command = line;
return c != EOF;
}
void Parser::ParseInfo() {
int c;
std::string num;
while (isspace(c = getchar()));
while (c != ' ') {
num += c;
c = getchar();
}
board->boardInfo.dimension = std::stoi(num);
std::cin >> board->boardInfo.pawnsToCapture;
std::cin >> board->boardInfo.pawns[WHITE];
std::cin >> board->boardInfo.pawns[BLACK];
std::cin.get();
std::cin >> board->boardInfo.reserve[WHITE];
std::cin >> board->boardInfo.reserve[BLACK];
std::cin >> board->boardInfo.activePlayer;
std::cin.get();
board->boardExpectedValues.pawnsInTotal[WHITE] = board->boardInfo.pawns[WHITE];
board->boardExpectedValues.pawnsInTotal[BLACK] = board->boardInfo.pawns[BLACK];
int upDowner = 1;
for (int i = 0; i != -1; i += upDowner) {
board->boardExpectedValues.rowWidth.push_back(board->boardInfo.dimension + i);
if (i == board->boardInfo.dimension - 1) upDowner = -1;
}
}
bool Parser::ParseBoard() {
int spacing = board->boardInfo.dimension;
std::vector<std::vector<char>>& bboard = board->board;
for (int i = 0; i < 2 * board->boardInfo.dimension + 1; i++) {
bboard.push_back({});
std::string line;
int c;
if (i == 0 || i == 2 * board->boardInfo.dimension) {
for (int _ = 0; _ < board->boardInfo.dimension + 1; _++)
bboard[bboard.size() - 1].push_back('+');
}
else {
while ((c = getchar()) != '\n' && c != EOF)
line += (char)c;
if (c == EOF) return false;
bboard[i].push_back('+');
for (size_t j = 0; j < line.length(); j++) {
if (line[j] == ' ') continue;
bboard[i].push_back(line[j]);
}
bboard[i].push_back('+');
}
if (spacing > 0)
for (int _ = 0; _ < spacing; _++)
bboard[i].push_back(NULL);
else if (spacing < 0)
for (int _ = 0; _ > spacing; _--)
bboard[i].insert(bboard[i].begin(), NULL);
spacing--;
}
return true;
}
void Parser::ParseGame() {
board->Clear();
ParseInfo();
ParseBoard();
}