PC/UVa:110107/10196
这道题其实不难,但是有几个点需要注意:
- 各种边界条件特别容易出错
- 车、 象、后的前进路线上不能有其它棋子挡着
- 其实不用判断王将军王的情况,因为如果有一个王将军了另一个王,那么必定出现两个王互相将军的情况
#include <iostream>
using namespace std;
char board[8][8] = { 0 };
bool checkp(int i, int j)
{
if (i + 1 < 8 && j - 1 >= 0 && board[i + 1][j - 1] == 'K')
return true;
if (i + 1 < 8 && j + 1 < 8 && board[i + 1][j + 1] == 'K')
return true;
return false;
}
bool checkP(int i, int j)
{
if (i - 1 >= 0 && j - 1 >= 0 && board[i - 1][j - 1] == 'k')
return true;
if (i - 1 >= 0 && j + 1 < 8 && board[i - 1][j + 1] == 'k')
return true;
return false;
}
bool checkrR(int i, int j, char king)
{
//up
for (int k = i - 1; k >= 0; k--)
{
if (board[k][j] == king) return true;
else if (board[k][j] == '.') continue;
else break;
}
//right
for (int k = j + 1; k < 8; k++)
{
if (board[i][k] == king) return true;
else if (board[i][k] == '.') continue;
else break;
}
//down
for (int k = i + 1; k < 8; k++)
{
if (board[k][j] == king) return true;
else if (board[k][j] == '.') continue;
else break;
}
//left
for (int k = j - 1; k >= 0; k--)
{
if (board[i][k] == king) return true;
else if (board[i][k] == '.') continue;
else break;
}
return false;
}
bool checkbB(int i, int j, char king)
{
//up left
for (int k = 1; i - k >= 0 && j - k >= 0; k++)
{
if (board[i - k][j - k] == king) return true;
else if (board[i - k][j - k] == '.') continue;
else break;
}
//up right
for (int k = 1; i - k >= 0 && j + k < 8; k++)
{
if (board[i - k][j + k] == king) return true;
else if (board[i - k][j + k] == '.') continue;
else break;
}
//down left
for (int k = 1; i + k < 8 && j - k >= 0; k++)
{
if (board[i + k][j - k] == king) return true;
else if (board[i + k][j - k] == '.') continue;
else break;
}
//down right
for (int k = 1; i + k < 8 && j + k < 8; k++)
{
if (board[i + k][j + k] == king) return true;
else if (board[i + k][j + k] == '.') continue;
else break;
}
return false;
}
bool checkqQ(int i, int j, char king)
{
bool b = checkrR(i, j, king);
if (b) return b;
else return checkbB(i, j, king);
}
bool checkkK(int i, int j, char king)
{
if (i - 1 >= 0 && j - 1 >= 0 && board[i - 1][j - 1] == king) return true;
else if (i - 1 >= 0 && board[i - 1][j] == king) return true;
else if (i - 1 >= 0 && j + 1 < 8 && board[i - 1][j + 1] == king) return true;
else if (j + 1 < 8 && board[i][j + 1] == king) return true;
else if (i + 1 < 8 && j + 1 < 8 && board[i + 1][j + 1] == king) return true;
else if (i + 1 < 8 && board[i + 1][j] == king) return true;
else if (i + 1 < 8 && j - 1 >= 0 && board[i + 1][j - 1] == king) return true;
else if (j - 1 >= 0 && board[i][j - 1] == king) return true;
else return false;
}
bool checknN(int i, int j, char king)
{
if (i - 1 >= 0 && j - 2 >= 0 && board[i - 1][j - 2] == king) return true;
else if (i - 2 >= 0 && j - 1 >= 0 && board[i - 2][j - 1] == king) return true;
else if (i - 2 >= 0 && j + 1 < 8 && board[i - 2][j + 1] == king) return true;
else if (i - 1 >= 0 && j + 2 < 8 && board[i - 1][j + 2] == king) return true;
else if (i + 1 < 8 && j + 2 < 8 && board[i + 1][j + 2] == king) return true;
else if (i + 2 < 8 && j + 1 < 8 && board[i + 2][j + 1] == king) return true;
else if (i + 2 < 8 && j - 1 >= 0 && board[i + 2][j - 1] == king) return true;
else if (i + 1 < 8 && j - 2 >= 0 && board[i + 1][j - 2] == king) return true;
else return false;
}
bool check(int cnt)
{
bool bLast = true, bBlack = false, bWhite = false;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cin >> board[i][j];
if (board[i][j] != '.') bLast = false;
}
}
if (bLast) return bLast;
cout << "Game #" << cnt << ": ";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
switch (board[i][j])
{
case 'p':
bWhite = checkp(i, j);
break;
case 'P':
bBlack = checkP(i, j);
break;
case 'r':
bWhite = checkrR(i, j, 'K');
break;
case 'R':
bBlack = checkrR(i, j, 'k');
break;
case 'b':
bWhite = checkbB(i, j, 'K');
break;
case 'B':
bBlack = checkbB(i, j, 'k');
break;
case 'q':
bWhite = checkqQ(i, j, 'K');
break;
case 'Q':
bBlack = checkqQ(i, j, 'k');
break;
case 'k':
bWhite = checkkK(i, j, 'K');
break;
case 'K':
bBlack = checkkK(i, j, 'k');
break;
case 'n':
bWhite = checknN(i, j, 'K');
break;
case 'N':
bBlack = checknN(i, j, 'k');
break;
default:
break;
}
if (bWhite || bBlack){
if (bWhite) cout << "white king is in check." << endl;
else if (bBlack) cout << "black king is in check." << endl;
return false;
}
}
}
cout << "no king is in check." << endl;
return false;
}
int main()
{
bool bLast = false;
int cnt = 1;
while (!bLast){
bLast = check(cnt++);
}
return 0;
}
/*
..k.....
ppp.pppp
........
.R...B..
........
........
PPPPPPPP
K.......
rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR
rnbqk.nr
ppp..ppp
....p...
...p....
.bPP....
.....N..
PP..PPPP
RNBQKB.R
........
........
........
........
........
........
........
........
*/