From the problem's input statement:midra wrote:I have no idea of what the program must output if there is a white king adyacent to a black king....who hack?
Ciao!!!...there won't be a configuration where both kings are in check.
Claudio
Moderator: Board moderators
From the problem's input statement:midra wrote:I have no idea of what the program must output if there is a white king adyacent to a black king....who hack?
Ciao!!!...there won't be a configuration where both kings are in check.
Code: Select all
board[row-1][col+2]==piece || board[row-1][col+2]==piece
Code: Select all
board[row-1][col-2]==piece
Code: Select all
#include <iostream>
using namespace std;
#include <string>
int main(){
int wlocx, wlocy, blocx, blocy;
bool wcheck;
wcheck = false;
bool bcheck;
bcheck = false;
bool done;
done = false;
string test[8];
int i, j;
long long game;
game = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++)
{
cin >> test[i][j];
while (test[i][j] == ' ') {
cin >> test[i][j];
}
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (test[i][j] != '.')
done = true;
}
}
while (done == true) {
game++;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++) {
if (test[j][i] == 'k') {
blocx = j;
blocy = i;
}
if (test[j][i] == 'K') {
wlocx = j;
wlocy = i;
}
}
}
for (i = wlocx; i < 8; i++) {
if (test[i][wlocy] == 'P'
|| test[i][wlocy] == 'R'
|| test[i][wlocy] == 'Q'
|| test[i][wlocy] == 'N'
|| test[i][wlocy] == 'B'
|| test[i][wlocy] == 'b'
|| test[i][wlocy] == 'n'
|| test[i][wlocy] == 'p'
|| test[i][wlocy] == 'k') {
i = 10;
}
else if (test[i][wlocy] == 'r') {
wcheck = true;
i = 10;
}
else if (test[i][wlocy] == 'q') {
wcheck = true;
i = 10;
}
}
for (i = wlocx; i >= 0; i--) {
if (test[i][wlocy] == 'P'
|| test[i][wlocy] == 'R'
|| test[i][wlocy] == 'Q'
|| test[i][wlocy] == 'N'
|| test[i][wlocy] == 'B'
|| test[i][wlocy] == 'b'
|| test[i][wlocy] == 'n'
|| test[i][wlocy] == 'p'
|| test[i][wlocy] == 'k') {
i = -1;
}
else if (test[i][wlocy] == 'r') {
wcheck = true;
i = -1;
}
else if (test[i][wlocy] == 'q') {
wcheck = true;
i = -1;
}
}
i = wlocx; j = wlocy;
while (i < 8 && j < 8) {
if (test[i][j] == 'P'
|| test[i][j] == 'R'
|| test[i][j] == 'Q'
|| test[i][j] == 'N'
|| test[i][j] == 'B'
|| test[i][j] == 'r'
|| test[i][j] == 'n'
|| test[i][j] == 'k'
|| test[i][j] == 'p') {
j = 10;
}
else if (test[i][j] == 'b') {
wcheck = true;
j = 10;
}
else if (test[i][j] == 'q') {
wcheck = true;
j = 10;
}
i++;
j++;
}
i = wlocx; j = wlocy;
while (i < 8 && j >= 0) {
if (test[i][j] == 'P'
|| test[i][j] == 'R'
|| test[i][j] == 'Q'
|| test[i][j] == 'N'
|| test[i][j] == 'B'
|| test[i][j] == 'r'
|| test[i][j] == 'n'
|| test[i][j] == 'k'
|| test[i][j] == 'p') {
j = -1;
}
else if (test[i][j] == 'b') {
wcheck = true;
j = -1;
}
else if (test[i][j] == 'q') {
wcheck = true;
j = -1;
}
i++;
j--;
}
i = wlocx;
j = wlocy;
while (i >= 0 && j >= 0) {
if (test[i][j] == 'P'
|| test[i][j] == 'R'
|| test[i][j] == 'Q'
|| test[i][j] == 'N'
|| test[i][j] == 'B'
|| test[i][j] == 'n'
|| test[i][j] == 'k'
|| test[i][j] == 'r'
|| test[i][j] == 'p') {
i = -1;
}
else if (test[i][j] == 'b') {
wcheck = true;
i = -1;
}
else if (test[i][j] == 'q') {
wcheck = true;
i = -1;
}
i--;
j--;
}
i = wlocx; j = wlocy;
while (i >= 0 && j < 8) {
if (test[i][j] == 'P'
|| test[i][j] == 'R'
|| test[i][j] == 'Q'
|| test[i][j] == 'N'
|| test[i][j] == 'B'
|| test[i][j] == 'n'
|| test[i][j] == 'k'
|| test[i][j] == 'r'
|| test[i][j] == 'p') {
i = -1;
}
else if (test[i][j] == 'b') {
wcheck = true;
i = -1;
}
else if (test[i][j] == 'q') {
wcheck = true;
i = -1;
}
i--;
j++;
}
i = wlocx; j = wlocy;
if (i > 0 && j > 0) {
if (test[i-1][j-1] == 'p')
wcheck = true;
}
if (i > 0 && j < 7) {
if (test[i-1][j+1] == 'p')
wcheck = true;
}
if (i < 6 && j < 7)
if (test[i+2][j+1] == 'n')
wcheck = true;
if (i < 6 && j > 0)
if (test[i+2][j-1] == 'n')
wcheck = true;
if (i > 1 && j > 0)
if (test[i-2][j-1] == 'n')
wcheck = true;
if (i > 1 && j < 7)
if (test[i-2][j+1] == 'n')
wcheck = true;
if (i < 7 && j < 6)
if (test[i+1][j+2] == 'n')
wcheck = true;
if (i < 7 && j > 1)
if (test[i+1][j-2] == 'n')
wcheck = true;
if (i > 0 && j > 1)
if (test[i-1][j-2] == 'n')
wcheck = true;
if (i > 0 && j < 6)
if (test[i-1][j+2] == 'n')
wcheck = true;
for (i = blocx; i < 8; i++) {
if (test[i][blocy] == 'p'
|| test[i][blocy] == 'r'
|| test[i][blocy] == 'q'
|| test[i][blocy] == 'n'
|| test[i][blocy] == 'b'
|| test[i][blocy] == 'N'
|| test[i][blocy] == 'P'
|| test[i][blocy] == 'B'
|| test[i][blocy] == 'K') {
i = 10;
}
else if (test[i][blocy] == 'R') {
bcheck = true;
i = 10;
}
else if (test[i][blocy] == 'Q') {
bcheck = true;
i = 10;
}
}
for (i = blocx; i >= 0; i--) {
if (test[i][blocy] == 'p'
|| test[i][blocy] == 'r'
|| test[i][blocy] == 'q'
|| test[i][blocy] == 'n'
|| test[i][blocy] == 'b'
|| test[i][blocy] == 'N'
|| test[i][blocy] == 'P'
|| test[i][blocy] == 'B'
|| test[i][blocy] == 'K') {
i = -1;
}
else if (test[i][blocy] == 'R') {
bcheck = true;
i = -1;
}
else if (test[i][blocy] == 'Q') {
bcheck = true;
i = -1;
}
}
i = blocx; j = blocy;
while (i < 8 && j < 8) {
if (test[i][j] == 'p'
|| test[i][j] == 'r'
|| test[i][j] == 'q'
|| test[i][j] == 'n'
|| test[i][j] == 'b'
|| test[i][j] == 'N'
|| test[i][j] == 'K'
|| test[i][j] == 'R'
|| test[i][j] == 'P') {
j = 10;
}
else if (test[i][j] == 'B') {
bcheck = true;
j = 10;
}
else if (test[i][j] == 'Q') {
bcheck = true;
j = 10;
}
i++;
j++;
}
i = blocx; j = blocy;
while (i < 8 && j >= 0) {
if (test[i][j] == 'p'
|| test[i][j] == 'r'
|| test[i][j] == 'q'
|| test[i][j] == 'n'
|| test[i][j] == 'b'
|| test[i][j] == 'N'
|| test[i][j] == 'K'
|| test[i][j] == 'R'
|| test[i][j] == 'P') {
j = -1;
}
else if (test[i][j] == 'B') {
bcheck = true;
j = -1;
}
else if (test[i][j] == 'Q') {
bcheck = true;
j = -1;
}
i++;
j--;
}
i = blocx; j = blocy;
while (i >= 0 && j >= 0) {
if (test[i][j] == 'p'
|| test[i][j] == 'r'
|| test[i][j] == 'q'
|| test[i][j] == 'b'
|| test[i][j] == 'b'
|| test[i][j] == 'N'
|| test[i][j] == 'K'
|| test[i][j] == 'R'
|| test[i][j] == 'P') {
i = -1;
}
else if (test[i][j] == 'B') {
bcheck = true;
i = -1;
}
else if (test[i][j] == 'Q') {
bcheck = true;
i = -1;
}
i--;
j--;
}
i = blocx; j = blocy;
while (i >= 0 && j < 8) {
if (test[i][j] == 'p'
|| test[i][j] == 'r'
|| test[i][j] == 'q'
|| test[i][j] == 'n'
|| test[i][j] == 'b'
|| test[i][j] == 'N'
|| test[i][j] == 'K'
|| test[i][j] == 'R'
|| test[i][j] == 'P') {
i = -1;
}
else if (test[i][j] == 'B') {
bcheck = true;
i = -1;
}
else if (test[i][j] == 'Q') {
bcheck = true;
i = -1;
}
i--;
j++;
}
i = blocx; j = blocy;
if (i < 7 && j < 7) {
if (test[i+1][j+1] == 'P')
bcheck = true;
}
if (i < 7 && j > 0) {
if (test[i+1][j-1] == 'P')
bcheck = true;
}
if (i < 6 && j < 7)
if (test[i+2][j+1] == 'N')
bcheck = true;
if (i < 6 && j > 0)
if (test[i+2][j-1] == 'N')
bcheck = true;
if (i > 1 && j > 0)
if (test[i-2][j-1] == 'N')
bcheck = true;
if (i > 1 && j < 7)
if (test[i-2][j+1] == 'N')
bcheck = true;
if (i < 7 && j < 6)
if (test[i+1][j+2] == 'N')
bcheck = true;
if (i < 7 && j > 1)
if (test[i+1][j-2] == 'N')
bcheck = true;
if (i > 0 && j > 1)
if (test[i-1][j-2] == 'N')
bcheck = true;
if (i > 0 && j < 6)
if (test[i-1][j+2] == 'N')
bcheck = true;
if (wcheck == true) {
cout << "Game #" << game << ": white king is in check." << endl;
}
else if (bcheck == true) {
cout << "Game #" << game << ": black king is in check." << endl;
}
else {
cout << "Game #" << game << ": no king is in check." << endl;
}
done = false;
wcheck = false;
bcheck = false;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++)
{
cin >> test[i][j];
while (test[i][j] == ' ') {
cin >> test[i][j];
}
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j< 8; j++) {
if (test[i][j] != '.')
done = true;
}
}
i = 0;
j = 0;
wlocy = -1;
wlocx = -1;
blocy = -1;
blocx = -1;
}
return 0;
}
Code: Select all
includes and the defines;
using namespace std;
string polje[8];
bool is_end() {
FOR( i, 0, 8 ) FOR( j, 0, 8 ) if( polje[i][j] != '.' ) return false;
return true;
}
bool bpawn( int x, int y ) {
if( x == 7 ) return false;
if( polje[x+1][y-1] == 'K' ) return true;
if( polje[x+1][y+1] == 'K' ) return true;
return false;
}
bool wpawn( int x, int y ) {
if( x == 0 ) return false;
if( polje[x-1][y-1] == 'k' ) return true;
if( polje[x-1][y+1] == 'k' ) return true;
return false;
}
bool rook( int x, int y, char k ) {
FOR( i, 0, 8 ) if( polje[x][i] == k ) return true;
FOR( i, 0, 8 ) if( polje[i][y] == k ) return true;
return false;
}
bool bishop( int x, int y, char k ) {
bool ret = false;
int nx = x, ny = y;
while( nx != 8 && ny != 8 ) {
if( polje[nx][ny] == k ) ret = true;
nx++; ny++;
}
nx = x; ny = y;
while( nx >= 0 && ny != 8 ) {
if( polje[nx][ny] == k ) ret = true;
nx--; ny++;
}
nx = x; ny = y;
while( nx >= 0 && ny >= 0 ) {
if( polje[nx][ny] == k ) ret = true;
nx--; ny--;
}
nx = x; ny = y;
while( nx != 8 && ny >= 0 ) {
if( polje[nx][ny] == k ) ret = true;
nx++; ny--;
}
return ret;
}
bool queen( int x, int y, char k ) {
bool ret = false;
if( bishop( x, y, k ) ) ret = true;
if( rook( x, y, k ) ) ret = true;
return ret;
}
bool knight( int x, int y, char k ) {
bool ret = false;
int dx[] = { 1, 1, -1, -1, 2, 2, -2, -2 };
int dy[] = { 2, -2, 2, -2, 1, -1, 1, -1 };
FOR( i, 0, 8 ) {
int nx = x + dx[i], ny = y + dy[i];
if( nx < 0 || nx >= 8 || ny < 0 || ny >= 8 ) continue;
if( polje[nx][ny] == k ) ret = true;
}
return ret;
}
int main() {
int n = 0;
while( 1 ) {
n++;
FOR( i, 0, 8 ) cin >> polje[i];
if( is_end() ) break;
bool black = false, white = false;
FOR( i, 0, 8 )
FOR( j, 0, 8 )
switch( polje[i][j] ) {
case 'p' : if( bpawn( i, j ) ) white = true; break;
case 'P' : if( wpawn( i, j ) ) black = true; break;
case 'r' : if( rook( i, j, 'K' ) ) white = true; break;
case 'R' : if( rook( i, j, 'k' ) ) black = true; break;
case 'b' : if( bishop( i, j, 'K' ) ) white = true; break;
case 'B' : if( bishop( i, j, 'k' ) ) black = true; break;
case 'q' : if( queen( i, j, 'K' ) ) white = true; break;
case 'Q' : if( queen( i, j, 'k' ) ) black = true; break;
case 'n' : if( knight( i, j, 'K' ) ) white = true; break;
case 'N' : if( knight( i, j, 'k' ) ) black = true; break;
}
if( black ) cout << "Game #" << n << ": black king is in check." << endl;
else if( white ) cout << "Game #" << n << ": white king is in check." << endl;
else cout << "Game #" << n << ": no king is in check." << endl;
}
return 0;
}
Code: Select all
K......k
........
........
........
........
........
........
.......b
K......k
........
..p.....
........
........
........
........
.......b
Kp.....k
prn.....
..p.....
........
........
........
........
.......b
K..R...k
........
..p.....
........
........
........
........
.......b
K.pr...k
b....q..
..p..nn.
........
..b.....
........
........
bq.....b
K..RB..k
.....R.P
..p.....
.......Q
........
........
........
.......b
......B.
........
..p.....
........
..Kpk..r
........
........
.......b
......P.
...K.k..
..p.....
........
........
........
........
.......b
........
...K....
pppppppp
..n.n...
........
........
.k......
.......b
........
...K....
pppppppp
........
........
........
k.......
.......b
........
........
........
........
........
........
........
........
Code: Select all
Game #1: white king is in check.
Game #2: no king is in check.
Game #3: white king is in check.
Game #4: black king is in check.
Game #5: no king is in check.
Game #6: no king is in check.
Game #7: no king is in check.
Game #8: no king is in check.
Game #9: white king is in check.
Game #10: no king is in check.
Code: Select all
#include <iostream.h>
void main()
{
bool t;
char c[8][8];
register int i=0, j=0;
int i_temp, j_temp, d=0;
hi: t=true; d++;
while(cin>>c[0][0]>>c[0][1]>>c[0][2]>>c[0][3]>>c[0][4]>>c[0][5]>>c[0][6]>>c[0][7]
>>c[1][0]>>c[1][1]>>c[1][2]>>c[1][3]>>c[1][4]>>c[1][5]>>c[1][6]>>c[1][7]
>>c[2][0]>>c[2][1]>>c[2][2]>>c[2][3]>>c[2][4]>>c[2][5]>>c[2][6]>>c[2][7]
>>c[3][0]>>c[3][1]>>c[3][2]>>c[3][3]>>c[3][4]>>c[3][5]>>c[3][6]>>c[3][7]
>>c[4][0]>>c[4][1]>>c[4][2]>>c[4][3]>>c[4][4]>>c[4][5]>>c[4][6]>>c[4][7]
>>c[5][0]>>c[5][1]>>c[5][2]>>c[5][3]>>c[5][4]>>c[5][5]>>c[5][6]>>c[5][7]
>>c[6][0]>>c[6][1]>>c[6][2]>>c[6][3]>>c[6][4]>>c[6][5]>>c[6][6]>>c[6][7]
>>c[7][0]>>c[7][1]>>c[7][2]>>c[7][3]>>c[7][4]>>c[7][5]>>c[7][6]>>c[7][7])
{
for(i=0; i<8; i++)
for(j=0; j<8; j++)
if(c[i][j]!='.')
t=false;
if(t)
break;
else
{
for(i=0; i<8; i++)
for(j=0; j<8; j++)
if(c[i][j]=='k')
{
i_temp=i;
j_temp=j;
}
i=i_temp;
j=j_temp;
while((i++)!=7 && (j++)!=7 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='B'))
if(c[i][j]=='Q' || c[i][j]=='B')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i--)!=0 && (j--)!=0 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='B'))
if(c[i][j]=='Q' || c[i][j]=='B')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i--)!=0 && (j++)!=7 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='B'))
if(c[i][j]=='Q' || c[i][j]=='B')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i++)!=7 && (j--)!=0 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='B'))
if(c[i][j]=='Q' || c[i][j]=='B')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((j++)!=7 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='R'))
if(c[i][j]=='Q' || c[i][j]=='R')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((j--)!=0 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='R'))
if(c[i][j]=='Q' || c[i][j]=='R')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i++)!=7 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='R'))
if(c[i][j]=='Q' || c[i][j]=='R')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i--)!=0 && (c[i][j]=='.' || c[i][j]=='Q' || c[i][j]=='R'))
if(c[i][j]=='Q' || c[i][j]=='R')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
if(c[i+1][j+1]=='P' || c[i+1][j-1]=='P')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
if(c[i+2][j+1]=='N' || c[i+2][j-1]=='N'
|| c[i+1][j-2]=='N' || c[i-1][j-2]=='N'
|| c[i-2][j-1]=='N' || c[i-2][j+1]=='N'
|| c[i-1][j+2]=='N' || c[i+1][j+2]=='N')
{cout << "Game #" << d <<": black king is in check.\n"; goto hi;}
for(i=0; i<8; i++)
for(j=0; j<8; j++)
if(c[i][j]=='K')
{
i_temp=i;
j_temp=j;
}
i=i_temp;
j=j_temp;
while((i++)!=7 && (j++)!=7 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='b'))
if(c[i][j]=='q' || c[i][j]=='b')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i--)!=0 && (j--)!=0 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='b'))
if(c[i][j]=='q' || c[i][j]=='b')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i--)!=0 && (j++)!=7 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='b'))
if(c[i][j]=='q' || c[i][j]=='b')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i++)!=7 && (j--)!=0 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='b'))
if(c[i][j]=='q' || c[i][j]=='b')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((j++)!=7 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='r'))
if(c[i][j]=='q' || c[i][j]=='r')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((j--)!=0 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='r'))
if(c[i][j]=='q' || c[i][j]=='r')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i++)!=7 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='r'))
if(c[i][j]=='q' || c[i][j]=='r')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
while((i--)!=0 && (c[i][j]=='.' || c[i][j]=='q' || c[i][j]=='r'))
if(c[i][j]=='q' || c[i][j]=='r')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
if(c[i-1][j-1]=='p' || c[i-1][j+1]=='p')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
i=i_temp;
j=j_temp;
if(c[i+2][j+1]=='n' || c[i+2][j-1]=='n'
|| c[i+1][j-2]=='n' || c[i-1][j-2]=='n'
|| c[i-2][j-1]=='n' || c[i-2][j+1]=='n'
|| c[i-1][j+2]=='n' || c[i+1][j+2]=='n')
{cout << "Game #" << d <<": white king is in check.\n"; goto hi;}
cout << "Game #" << d <<": no king is in check.\n"; goto hi;
}
}
}