-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_position.cpp
More file actions
95 lines (86 loc) · 3.92 KB
/
game_position.cpp
File metadata and controls
95 lines (86 loc) · 3.92 KB
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
92
93
94
95
#include "game_position.h"
bool Position::is_correct_cell_id() const {
return file >= 'a' && file <= 'h' && rank >= 1 && rank <= 8;
}
bool Position::is_correct_type() const {
return find(types_of_figure.begin(), types_of_figure.end(), type) != types_of_figure.end();
}
bool GamePosition::is_cell_vacant(Position &position) {
for (Position &i: game_position)
if (i.file == position.file && i.rank == position.rank)
return false;
return true;
}
void GamePosition::reading(const string &file_path) {
string color;
string type;
short int rank;
char file;
try {
ifstream in(file_path);
while (in.is_open()) {
in >> color;
if (not in)
break;
in >> type >> file >> rank;
if (not in)
throw runtime_error("Read error");
transform(color.begin(), color.end(), color.begin(), ::tolower);
transform(type.begin(), type.end(), type.begin(), ::tolower);
string type_of_figure = color.append("_").append(type);
Position position = {file, rank, type_of_figure};
if (!is_cell_vacant(position) || !position.is_correct_cell_id())
throw runtime_error("Incorrect position of the figure");
if (!position.is_correct_type())
throw runtime_error("Incorrect body type");
game_position.push_back(position);
}
if (not in.is_open())
throw runtime_error("File not found");
}
catch (exception &e) {
cerr << e.what();
game_position.clear();
}
}
void GamePosition::visualise(Chessboard &board) {
for (Position &position: game_position) {
if (position.type == "white_pawn") {
auto *pw = new Pawn(board, "pieces.png", true);
board.at(position.file, position.rank).attach_figure(*pw);
} else if (position.type == "black_pawn") {
auto *pw = new Pawn(board, "pieces.png", false);
board.at(position.file, position.rank).attach_figure(*pw);
} else if (position.type == "white_knight") {
auto *kt_w = new Knight(board, "pieces.png", true);
board.at(position.file, position.rank).attach_figure(*kt_w);
} else if (position.type == "black_knight") {
auto *kt_b = new Knight(board, "pieces.png", false);
board.at(position.file, position.rank).attach_figure(*kt_b);
} else if (position.type == "white_king") {
auto *kg_w = new King(board, "pieces.png", true);
board.at(position.file, position.rank).attach_figure(*kg_w);
} else if (position.type == "black_king") {
auto *kg_b = new King(board, "pieces.png", false);
board.at(position.file, position.rank).attach_figure(*kg_b);
} else if (position.type == "white_queen") {
auto *qu_w = new Queen{board, "pieces.png", true};
board.at(position.file, position.rank).attach_figure(*qu_w);
} else if (position.type == "black_queen") {
auto *qu_b = new Queen(board, "pieces.png", false);
board.at(position.file, position.rank).attach_figure(*qu_b);
} else if (position.type == "white_bishop") {
auto *bp_w = new Bishop(board, "pieces.png", true);
board.at(position.file, position.rank).attach_figure(*bp_w);
} else if (position.type == "black_bishop") {
auto *bp_b = new Bishop(board, "pieces.png", false);
board.at(position.file, position.rank).attach_figure(*bp_b);
} else if (position.type == "white_rook") {
auto *rk_w = new Rook(board, "pieces.png", true);
board.at(position.file, position.rank).attach_figure(*rk_w);
} else if (position.type == "black_rook") {
auto *rk_b = new Rook(board, "pieces.png", false);
board.at(position.file, position.rank).attach_figure(*rk_b);
}
}
}