Programming Challenges 习题 1.6.7

PC/UVa:110107/10196

Check the Check

这道题其实不难,但是有几个点需要注意:

  • 各种边界条件特别容易出错
  • 车、 象、后的前进路线上不能有其它棋子挡着
  • 其实不用判断王将军王的情况,因为如果有一个王将军了另一个王,那么必定出现两个王互相将军的情况
#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

........
........
........
........
........
........
........
........
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值