소감

2005/02/19 Accepted 0:00.004 64
King의 check상태를 좀더 쉽게 알 수 있으려면 어떻게 해야 할까?

코드

~cpp 
// no10196 - Check the Check
#include <iostream>
#include <cstring>
using namespace std;

const int MAX_CASE = 300;
const int TIED = -1;
const int BLACK = 0;
const int WHITE = 1;

char chess[10][10];

inline void eatline() { while(cin.get() != '\n') continue; };
bool DoItChess();
int WhoIsWin();
int Pawn(int row, int col, char enemy);
int Rook(int row, int col, char enemy);
int Bishop(int row, int col, char enemy);
int Queen(int row, int col, char enemy);
int Knight(int row, int col, char enemy);

int main()
{
	int win[MAX_CASE];
	int nCase = 0;
	while(DoItChess())
	{
		win[nCase++] = WhoIsWin();	
		eatline();
	}
	for (int i=0; i<nCase; i++)
	{
		cout << "Game #" << i+1 << ":";
		if (win[i] == BLACK)
			cout << " white";
		else if (win[i] == WHITE)
			cout << " black";
		else
			cout << " no";
		cout << " king is in check.\n";
	}
	return 0;
}

bool DoItChess()
{
	int endCount = 0;
	for (int i=0; i<8; i++)
	{
		cin.getline(chess[i], 10, '\n');
		if (strcmp(chess[i], "........") == 0)
			endCount++;
	}
	if (endCount == 8)
		return false;
	return true;	
}

int WhoIsWin()
{
	int check = TIED;
	char enemy;
	for (int i=0; i<8; i++)
	{
		for (int j=0; j<8; j++)
		{
			int key = int(chess[i][j]);
			key < int('a') ? enemy = 'k' : enemy = 'K';		
			switch(chess[i][j])
			{
			case 'p':
			case 'P':
				check = Pawn(i, j, enemy);
				break;
			case 'r':
			case 'R':
				check = Rook(i, j, enemy);
				break;
			case 'b':
			case 'B':
				check = Bishop(i, j, enemy);
				break;
			case 'q':
			case 'Q':
				check = Queen(i, j, enemy);
				break;
			case 'n':
			case 'N':
				check = Knight(i, j, enemy);
				break;
			}
			if (check == BLACK || check == WHITE)
				return check;
		}
	}
	return check;
}

int Pawn(int row, int col, char enemy)
{
	if (enemy == 'K' && (chess[row+1][col-1] == enemy || chess[row+1][col+1] == enemy))
		return BLACK;
	if (enemy == 'k' && (chess[row-1][col-1] == enemy || chess[row-1][col+1] == enemy))
		return WHITE;
	return TIED;
}

int Rook(int row, int col, char enemy)
{
	int i;
	bool check = false;
	for (i=row-1; i>=0; i--)
	{
		if (chess[i][col] == enemy)
		{
			check = true;
			break;
		}
		else if (chess[i][col] == '.')
			continue;
		else break;
	}
	for (i=row+1; i<8; i++)
	{
		if (chess[i][col] == enemy)
		{
			check = true;
			break;
		}
		else if (chess[i][col] == '.')
			continue;
		else break;
	}
	for (i=col-1; i>=0; i--)
	{
		if (chess[row][i] == enemy)
		{
			check = true;
			break;
		}
		else if (chess[row][i] == '.')
			continue;
		else break;
	}
	for (i=col+1; i<8; i++)
	{
		if (chess[row][i] == enemy)
		{
			check = true;
			break;
		}
		else if (chess[row][i] == '.')
			continue;
		else break;
	}
	if (check)
	{
		if(enemy == 'K')
			return BLACK; 
		else return WHITE;
	}
	return TIED;
}

int Bishop(int row, int col, char enemy)
{
	int i;
	bool check = false;
	bool temp[2] = {0,};
	int count = 0;
	for (i=row-1; i>=0; i--)
	{
		count++;
		if (col - count >= 0 && !temp[0])
		{
			if (chess[i][col-count] == enemy)
			{
				check = true;
				break;
			}
			else if (chess[i][col-count] == '.')
				;
			else temp[0] = 1;
		}
		if (col + count < 8 && !temp[1])
		{
			if (chess[i][col+count] == enemy)
			{
				check = true;
				break;
			}
			else if (chess[i][col+count] == '.')
				;
			else temp[1] = 1;
		}	
	}
	count = 0;
	temp[0] = temp[1] = 0;
	for (i=row+1; i<8; i++)
	{
		count++;
		if (col - count >= 0 && !temp[0])
		{
			if (chess[i][col-count] == enemy)
			{
				check = true;
				break;
			}
			else if (chess[i][col-count] == '.')
				;
			else temp[0] = 1;
		}
		if (col + count < 8 && !temp[1])
		{
			if (chess[i][col+count] == enemy)
			{
				check = true;
				break;
			}
			else if (chess[i][col+count] == '.')
				;
			else temp[1] = 1;
		}	
	}
	if (check)
	{
		if(enemy == 'K')
			return BLACK; 
		else return WHITE;
	}
	return TIED;
}

int Queen(int row, int col, char enemy)
{
	if (Rook(row, col, enemy) == BLACK || Bishop(row, col, enemy) == BLACK)
		return BLACK;
	else if(Rook(row, col, enemy) == WHITE || Bishop(row, col, enemy) == WHITE)
		return WHITE;
	return TIED;
}

int Knight(int row, int col, char enemy)
{
	bool check = false;
	if (chess[row-2][col-1] == enemy || chess[row-2][col+1] == enemy)
		check = true;
	if (chess[row-1][col-2] == enemy || chess[row-1][col+2] == enemy)
		check = true;
	if (chess[row+2][col-1] == enemy || chess[row+2][col+1] == enemy)
		check = true;
	if (chess[row+1][col-2] == enemy || chess[row+1][col+2] == enemy)
		check = true;
	if (check && enemy == 'K')
		return BLACK;
	if (check && enemy == 'k')
		return WHITE;
	return TIED;
}
Retrieved from http://wiki.zeropage.org/wiki.php/CheckTheCheck/문보창
last modified 2021-02-07 05:22:51