감 ¶
2005/03/13 Accepted 0:00.010 64
깔게 고 .
기 고 굳 그 기 간 그게 .
toupper ctype.h include.
겠.
깔게 고 .
기 고 굳 그 기 간 그게 .
toupper ctype.h include.
겠.
¶
~cpp
#include <iostream>
using namespace std;
#include <ctype.h>
const int EMPTY = 0;
const int BLACK = 1;
const int WHITE = 2;
char board[8][8];
int whereSide(int y, int x)
{
if (board[y][x] >= 'a' && board[y][x] <= 'z')
return BLACK;
else if (board[y][x] >= 'A' && board[y][x] <= 'Z')
return WHITE;
else
return EMPTY;
}
bool isInBoard(int y, int x)
{
if (y >= 0 && y < 8 && x >= 0 && x < 8)
return true;
return false;
}
bool PawnCheck(int y, int x, int side)
{
if (side == WHITE)
{
if (isInBoard(y - 1, x - 1) && board[y - 1][x - 1] == 'p')
return true;
else if (isInBoard(y - 1, x + 1) && board[y - 1][x + 1] == 'p')
return true;
}
else
{
if (isInBoard(y + 1, x - 1) && board[y + 1][x - 1] == 'P')
return true;
else if (isInBoard(y + 1, x - 1) && board[y + 1][x + 1] == 'P')
return true;
}
return false;
}
bool RookCheck(int y, int x, int side)
{
int move[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int i, k;
for (k = 0; k < 4; k++)
{
for (i = 1; i <= 7 && isInBoard(y + i * move[k][0], x + i * move[k][1]); i++)
{
if (board[y + i * move[k][0]][x + i * move[k][1]] == EMPTY)
continue;
else if (whereSide(y + i * move[k][0], x + i * move[k][1]) == side)
break;
else
{
if (toupper(board[y + i * move[k][0]][x + i * move[k][1]]) == 'R')
return true;
else
break;
}
}
}
return false;
}
bool BishopCheck(int y, int x, int side)
{
int move[4][2] = {{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
int i, k;
for (k = 0; k < 4; k++)
{
for (i = 1; i <= 7 && isInBoard(y + i * move[k][0], x + i * move[k][1]); i++)
{
if (board[y + i * move[k][0]][x + i * move[k][1]] == EMPTY)
continue;
else if (whereSide(y + i * move[k][0], x + i * move[k][1]) == side)
break;
else
{
if (toupper(board[y + i * move[k][0]][x + i * move[k][1]]) == 'B')
return true;
else
break;
}
}
}
return false;
}
bool QueenCheck(int y, int x, int side)
{
int move[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
int i, k;
for (k = 0; k < 8; k++)
{
for (i = 1; i <= 7 && isInBoard(y + i * move[k][0], x + i * move[k][1]); i++)
{
if (board[y + i * move[k][0]][x + i * move[k][1]] == EMPTY)
continue;
else if (whereSide(y + i * move[k][0], x + i * move[k][1]) == side)
break;
else
{
if (toupper(board[y + i * move[k][0]][x + i * move[k][1]]) == 'Q')
return true;
else
break;
}
}
}
return false;
}
bool KingCheck(int y, int x, int side)
{
int move[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
int k;
for (k = 0; k < 8; k++)
{
if (isInBoard(y + move[k][0], x + move[k][1]))
{
if (whereSide(y + move[k][0], x + move[k][1]) != side && toupper(board[y + move[k][0]][x + move[k][1]]) == 'K')
return true;
}
}
return false;
}
bool KnightCheck(int y, int x, int side)
{
int move[8][2] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
int k;
for (k = 0; k < 8; k++)
{
if (isInBoard(y + move[k][0], x + move[k][1]))
{
if (whereSide(y + move[k][0], x + move[k][1]) != side && toupper(board[y + move[k][0]][x + move[k][1]]) == 'N')
return true;
}
}
return false;
}
bool isInCheck(int y, int x, int side)
{
if (PawnCheck(y, x, side))
return true;
if (RookCheck(y, x, side))
return true;
if (BishopCheck(y, x, side))
return true;
if (QueenCheck(y, x, side))
return true;
if (KingCheck(y, x, side))
return true;
if (KnightCheck(y, x, side))
return true;
return false;
}
int main()
{
int i, j;
bool isEmpty;
int gameCnt = 0;
while (true)
{
gameCnt++;
isEmpty = true;
for (i = 0; i < 8; i++)
cin >> board[i];
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
if (board[i][j] == '.')
board[i][j] = EMPTY;
if (whereSide(i, j) != EMPTY)
{
isEmpty = false;
}
}
if (isEmpty)
break;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
if ((board[i][j] == 'K' || board[i][j] == 'k') && isInCheck(i, j, whereSide(i, j)))
{
if (whereSide(i, j) == WHITE)
{
cout << "Game #" << gameCnt << ": white king is in check." << endl;
i = 8;
break;;
}
else
{
cout << "Game #" << gameCnt << ": black king is in check." << endl;
i = 8;
break;;
}
}
}
if (i == 8)
cout << "Game #" << gameCnt << ": no king is in check." << endl;
}
return 0;
}










