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
|
#include <iostream>
using namespace std;
inline int raycast(char A[][8], int i, int j, int di, int dj) {
do {
i+=di; //change i value by deltaI
j+=dj; //change j value by deltaJ
} while(i+di>=0 && j+dj>=0 && i+di<8 && j+dj<8 && A[i][j]=='.'); //continue until we are off the board or encounter a non empty space
if(A[i][j]=='q' || A[i][j]=='b' || A[i][j]=='r') return(0); //these pieces can attack a White king
if(A[i][j]=='Q' || A[i][j]=='B' || A[i][j]=='R') return(1); //these pieces can attack a Black king
return(2); //Nothing of interest found
}
inline void game(int g, int k) { //outputs who is in check
cout<<"Game #"<<g<<": "<<(k==1 ? "black king is in check.\n" : "white king is in check.\n");
}
inline bool BQR(char A[][8], int k, int ki, int kj, int g) {
int X[3]={-1, 0, 1};
for(int i=0; i<3 ; ++i) {
for(int j=0; j<3; ++j) { //generates all directions from a piece
if(raycast(A, ki, kj, X[i], X[j])==k) { //checks if there is an attacking piece in direction (X[i], X[j])
game(g, k);
return(true);
}
}
}
return(false);
}
inline bool N(char G[][8], int k, int ki, int kj, int g) {
int A[2]={-2,2}, B[2]={-1,1};
char n=(k==0 ? 'n' : 'N'); //Are we looking for a black or white Knight
for(int i=0; i<2; ++i) {
for(int j=0; j<2; ++j) { //Generates all knight configurations from a piece
if(ki+A[i]>=0 && ki+A[i]<8 && kj+B[j]>=0 && kj+B[j]<8 && (G[ki+A[i]][kj+B[j]]==n || G[ki+B[j]][kj+A[i]]==n)) { //if knight of correct colour is found
game(g, k); //king is in check
return(true);
}
}
}
return(false);
}
inline bool P(char A[][8], int k, int ki, int kj, int g) {
if(k==0 && ki-1>=0 && kj-1>=0 && kj+1<8 && (A[ki-1][kj-1]=='p' || A[ki-1][kj+1]=='p')) { //checks if pawn is attacking white king
game(g, k);
return(true);
}
if(k==1 && ki+1<8 && kj-1>=0 && kj+1<8 && (A[ki+1][kj-1]=='P' || A[ki+1][kj+1]=='P')) { //checks if pawn is attacking black king
game(g, k);
return(true);
}
return(false);
}
int main() {
char A[8][8];
for(int g=1; ; ++g) { //holds the game number
int kings[2][2]={{-1,-1},{-1,-1}}; //stores the location of the kings kings[0] is the white king's location
for(int i=0; i<8; ++i) for(int j=0; j<8; ++j) { //input board and locate kings
cin>>A[i][j];
if(A[i][j]=='K') {
kings[0][0]=i;
kings[0][1]=j;
} else if(A[i][j]=='k') {
kings[1][0]=i;
kings[1][1]=j;
}
}
if(kings[0][0]==-1) break; //if there are no kings found then we are done
bool flag=true;
for(int k=0; k<2; ++k) {
if(BQR(A, k, kings[k][0], kings[k][1], g)) {flag=false; break;} //checks if king[k] is being ckeched by a Bishop, Queen or Rook
if(N(A, k, kings[k][0], kings[k][1], g)) {flag=false; break;} //checks if king[k] is being checked by a Knight
if(P(A, k, kings[k][0], kings[k][1], g)){flag=false; break;} //checks if king[k] is being checked by a Pawn
}
if(flag) cout<<"Game #"<<g<<": no king is in check.\n"; //if no king is in check out put this
}
return(0);
}
|