-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10196.cpp
191 lines (180 loc) · 3.73 KB
/
P10196.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
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <stdio.h>
#define HIT_NONE 0
#define HIT_WHITE 1
#define HIT_BLACK 2
/*
black at top
WHITE at bottom
*/
class Board {
char board[64];
int state;
bool kingAt(bool white, int x, int y) {
if(x < 0 || y < 0 || x >= 8 || y >= 8)
return false;
if(white)
return board[8*y+x] == 'K';
return board[8*y+x] == 'k';
}
void tryHitKing(bool white, int x, int y) {
if(state == HIT_NONE && kingAt(white, x, y)) {
if(white) {
state = HIT_WHITE;
std::cout << "white king is in check." << std::endl;
}
else {
state = HIT_BLACK;
std::cout << "black king is in check." << std::endl;
}
}
}
// Bonde
void pawn(bool white, int x, int y) {
tryHitKing(!white, x-1, white ? y-1 : y+1);
tryHitKing(!white, x+1, white ? y-1 : y+1);
}
// Taarn
void rook(bool white, int x, int y) {
// Up:
for(int xx = x-1; xx >= 0; --xx) {
tryHitKing(!white, xx, y);
if(board[8*y+xx] != '.')
break;
}
// Down:
for(int xx = x+1; xx < 8; ++xx) {
tryHitKing(!white, xx, y);
if(board[8*y+xx] != '.')
break;
}
// Left:
for(int yy = y-1; yy >= 0; --yy) {
tryHitKing(!white, x, yy);
if(board[8*yy+x] != '.')
break;
}
// Right:
for(int yy = y+1; yy < 8; ++yy) {
tryHitKing(!white, x, yy);
if(board[8*yy+x] != '.')
break;
}
}
// Loeber
void bishop(bool white, int x, int y) {
for(int i = 1; true; ++i) {
int xx = x-i;
int yy = y-i;
if(xx < 0 || yy < 0 || xx >= 8 || yy >= 8)
break;
tryHitKing(!white, xx, yy);
if(board[8*yy+xx] != '.')
break;
}
for(int i = 1; true; ++i) {
int xx = x-i;
int yy = y+i;
if(xx < 0 || yy < 0 || xx >= 8 || yy >= 8)
break;
tryHitKing(!white, xx, yy);
if(board[8*yy+xx] != '.')
break;
}
for(int i = 1; true; ++i) {
int xx = x+i;
int yy = y-i;
if(xx < 0 || yy < 0 || xx >= 8 || yy >= 8)
break;
tryHitKing(!white, xx, yy);
if(board[8*yy+xx] != '.')
break;
}
for(int i = 1; true; ++i) {
int xx = x+i;
int yy = y+i;
if(xx < 0 || yy < 0 || xx >= 8 || yy >= 8)
break;
tryHitKing(!white, xx, yy);
if(board[8*yy+xx] != '.')
break;
}
}
// Dronning
void queen(bool white, int x, int y) {
bishop(white, x, y);
rook(white, x, y);
}
// Springer
void knight(bool white, int x, int y) {
for(int xx = -2; xx <= 2; ++xx) {
for(int yy = -2; yy <= 2; ++yy) {
if(xx*xx*yy*yy != 4)
continue;
tryHitKing(!white, x+xx, y+yy);
}
}
}
public:
bool read() {
state = HIT_NONE;
bool anyPieces = false;
char line[19];
// Scan for next line with content:
do {
gets(line);
}
while(line[0] != '.' && !isalpha(line[0]));
for(int i = 0; i < 8; ++i) {
if(i != 0)
gets(line);
for(int j = 0; j < 8; ++j) {
board[8*i+j] = line[j];
if(line[j] != '.')
anyPieces = true;
}
}
return anyPieces;
}
void check() {
for(int x = 0; x < 8 && state == HIT_NONE; ++x) {
for(int y = 0; y < 8 && state == HIT_NONE; ++y) {
char piece = board[8*y+x];
switch(piece) {
case '.':
break;
case 'p':
case 'P':
pawn(piece == 'P', x, y);
break;
case 'r':
case 'R':
rook(piece == 'R', x, y);
break;
case 'b':
case 'B':
bishop(piece == 'B', x, y);
break;
case 'q':
case 'Q':
queen(piece == 'Q', x, y);
break;
case 'n':
case 'N':
knight(piece == 'N', x, y);
break;
}
}
}
if(state == HIT_NONE)
std::cout << "no king is in check." << std::endl;
}
};
int main() {
Board b;
for(int cas = 1; b.read(); ++cas) {
std::cout << "Game #" << cas << ": ";
b.check();
}
return 0;
}