-
Notifications
You must be signed in to change notification settings - Fork 0
/
figure.cpp
181 lines (170 loc) · 7.06 KB
/
figure.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "figure.h"
list<Field *> Figure::excludeImpossibleMovements(list<Field *> possibleMovements, Board* board, Coords myPos)
{
//cout << " B\n";
Coords pinningFigure = Coords(FieldsCoordinates::A, -1);
Coords myKingPos = board->getKingPosition(getTeam());
// Test dla ataku prostego
if(myPos.first == myKingPos.first){
// Czyli są na 1 linii w pionie
if(myPos.second > myKingPos.second){
for(int i = myPos.second+1; i < 8; i++){
Figure* tested = board->getFigureAt(myPos.first, i);
if(tested == nullptr) continue;
else if(tested->getTeam() == getTeam()) break;
else{
auto ftek = tested->getFieldsToEnemyKing(Coords(myPos.first, i), board, false, true);
if(board->contains(ftek, board->getField(myPos))){
pinningFigure = Coords(myPos.first, i);
}
break;
}
}
} else if(myPos.second < myKingPos.second){
for(int i = myPos.second-1; i > -1; i--){
Figure* tested = board->getFigureAt(myPos.first, i);
if(tested == nullptr) continue;
else if(tested->getTeam() == getTeam()) break;
else{
auto ftek = tested->getFieldsToEnemyKing(Coords(myPos.first, i), board, false, true);
if(board->contains(ftek, board->getField(myPos))){
pinningFigure = Coords(myPos.first, i);
}
break;
}
}
}
} else if(myPos.second == myKingPos.second){
// Czyli są na 1 linii w poziomie
if(myPos.first > myKingPos.first){
for(int i = myPos.first+1; i < 8; i++){
Figure* tested = board->getFigureAt(FieldsCoordinates(i), myPos.second);
if(tested == nullptr) continue;
else if(tested->getTeam() == getTeam()) break;
else{
auto ftek = tested->getFieldsToEnemyKing(Coords(FieldsCoordinates(i), myPos.second), board, false, true);
if(board->contains(ftek, board->getField(myPos))){
pinningFigure = Coords(FieldsCoordinates(i), myPos.second);
}
break;
}
}
} else if(myPos.first < myKingPos.first){
for(int i = myPos.first-1; i > -1; i--){
Figure* tested = board->getFigureAt(FieldsCoordinates(i), myPos.second);
if(tested == nullptr) continue;
else if(tested->getTeam() == getTeam()) break;
else{
auto ftek = tested->getFieldsToEnemyKing(Coords(FieldsCoordinates(i), myPos.second), board, false, true);
if(board->contains(ftek, board->getField(myPos))){
pinningFigure = Coords(FieldsCoordinates(i), myPos.second);
}
break;
}
}
}
} else if(abs(static_cast<int>(myPos.first) - static_cast<int>(myKingPos.first)) == abs(myPos.second - myKingPos.second)){ // Test dla ataku skośnego
//
int x = myPos.first - myKingPos.first;
int y = myPos.second - myKingPos.second;
int a = 0;
if(x*y > 0) a = 1;
else if(x*y < 0) a = -1;
int b = myPos.second - (a*myPos.first);
if(x > 0){
for(int i = myPos.first+1; i<8 && (-1 < (a*i)+b && (a*i)+b < 8); i++){
Coords testPoint(FieldsCoordinates(i), (a*i)+b);
Figure* tested = board->getFigureAt(testPoint);
if(tested == nullptr) continue;
else if(tested->getTeam() == getTeam()) break;
else{
auto ftek = tested->getFieldsToEnemyKing(testPoint, board, false, true);
if(board->contains(ftek, board->getField(myPos))){
pinningFigure = testPoint;
}
break;
}
}
} else if(x < 0){
for(int i = myPos.first-1; i>-1 && (-1 < (a*i)+b && (a*i)+b < 8); i--){
Coords testPoint(FieldsCoordinates(i), (a*i)+b);
Figure* tested = board->getFigureAt(testPoint);
if(tested == nullptr) continue;
else if(tested->getTeam() == getTeam()) break;
else{
auto ftek = tested->getFieldsToEnemyKing(testPoint, board, false, true);
if(board->contains(ftek, board->getField(myPos))){
pinningFigure = testPoint;
}
break;
}
}
}
// x y - szukamy y=x+b dla x -> 8
// x -y - szukamy y=-x+b dla x -> 8
// -x -y - szukamy y=x+b dla x -> 1
// -x y - szukamy y=-x+b dla x -> 1
}
if(pinningFigure.second != -1){
auto pinningLine = board->getFigureAt(pinningFigure)->getFieldsToEnemyKing(pinningFigure, board, false, true);
possibleMovements = commonPart(possibleMovements, pinningLine);
}
// Po tym etapie wiemy, jakie mamy ruchy będąc przypiętymi lub nie
if(board->isKingChecked(getTeam())){
Coords checkingFigurePosition = board->getFirstAttackerPosition(getTeam()==T_WHITE?T_BLACK:T_WHITE);
if(board->isPossibleToCoverKing(getTeam())){
auto lineToOwnKing = board->getFigureAt(checkingFigurePosition)->getFieldsToEnemyKing(checkingFigurePosition, board, true);
// Porównanie lineToOwnKing z possibleMovements
//DEBUG
list<Coords> ltok;
for(auto e : lineToOwnKing){
ltok.push_back(board->getFieldCoords(e));
}
list<Coords> pm;
for(auto e : possibleMovements){
pm.push_back(board->getFieldCoords(e));
}
//DEBUG end
possibleMovements = commonPart(lineToOwnKing, possibleMovements);
} else {
auto checkingFigureField = board->getField(checkingFigurePosition);
bool found = false;
for(auto possField : possibleMovements){
if(possField == checkingFigureField) {
found = true;
break;
}
}
possibleMovements.clear();
if(found) possibleMovements.push_back(checkingFigureField);
}
}
//cout << " eeB\n";
return possibleMovements;
}
Figure::Figure(){}
Figure::Figure(Team t){
this->t = t;
}
Figure::~Figure(){}
list<Field *> Figure::commonPart(const list<Field *> a, const list<Field *> b)
{
list<Field*> ret;
for(Field* i : a){
for(Field* j : b){
if(i == j) ret.push_back(i);
}
}
return ret;
}
void Figure::onMoveEvent(){}
Team Figure::getTeam() const{
return this->t;
}
string Figure::getSymbol() const{
return "_";
}
string Figure::show() const{
if(getTeam() == Team::T_BLACK) return "C" + getSymbol();
return "B" + getSymbol();
}