U E D R , A S I H C RSS

Check The Check/Celfin

이야기

체계적인 설계를 하지 않고 짜서 그런지 헷갈리는 부분이 많았다.

소스

~cpp
#include <iostream>
using namespace std;

char chess[12][12];
int exit_cond;
int i, j, m, n;
int gameNum=0;
bool black, white;

bool checkPp(int x, int y, char Pp)
{
	if(Pp=='P')
		return (chess[x+1][y+1] ==Pp || chess[x-1][y+1]==Pp);
	else if(Pp=='p')
		return (chess[x+1][y-1] ==Pp || chess[x-1][y-1]==Pp);
	return false;
}

bool checkRr(int x, int y, char Rr)
{
	for(m=x-1; m>1; m--)
	{
		if(chess[m][y]==Rr)
			return true;
		else if(chess[m][y]!='.')
			break;
	}
	for(m=x+1; m<10; m++)
	{
		if(chess[m][y]==Rr)
			return true;
		else if(chess[m][y]!='.')
			break;
	}
	for(m=y-1; m>1; m--)
	{
		if(chess[x][m]==Rr)
			return true;
		else if(chess[x][m]!='.')
			break;
	}
	for(m=y+1; m<10; m++)
	{
		if(chess[x][m]==Rr)
			return true;
		else if(chess[x][m]!='.')
			break;
	}
	return false;
}

bool checkBb(int x, int y, char Bb)
{
	for(m=x-1, n=y-1; m>1 && n>1; m--, n--)
	{
		if(chess[m][n]==Bb)
			return true;
		else if(chess[m][n]!='.')
			break;
	}
	for(m=x-1, n=y+1; m>1 && n<10; m--, n++)
	{
		if(chess[m][n]==Bb)
			return true;
		else if(chess[m][n]!='.')
			break;
	}
	for(m=x+1, n=y-1; m<10 && n>1; m++, n--)
	{
		if(chess[m][n]==Bb)
			return true;
		else if(chess[m][n]!='.')
			break;
	}
	for(m=x+1, n=y+1; m<10 && n<10; m++, n++)
	{
		if(chess[m][n]==Bb)
			return true;
		else if(chess[m][n]!='.')
			break;
	}
	return false;
}

bool checkQq(int x, int y, char Qq)
{
	if(checkBb(x,y,Qq) || checkRr(x,y,Qq))
		return true;
	return false;
}

bool checkKk(int x, int y, char Kk)
{
	if(chess[x-1][y-1]==Kk || chess[x][y-1]==Kk || chess[x+1][y-1]==Kk || chess[x+1][y]==Kk || chess[x+1][y+1]==Kk || chess[x][y+1]==Kk || chess[x-1][y+1]==Kk || chess[x-1][y]==Kk)
		return true;
	else 
		return false;
}

bool checkNn(int x, int y, char Nn)
{
	if(chess[x-1][y-2]==Nn || chess[x+1][y-2]==Nn || chess[x+2][y-1]==Nn || chess[x+2][y+1]==Nn || chess[x+1][y+2]==Nn || chess[x-1][y+2]==Nn || chess[x-2][y+1]==Nn || chess[x-2][y-1]==Nn)
		return true;
	else 
		return false;
}

void check()
{
	gameNum++;
	
	black = false;
	white = false;

	for(i=2; i<10; i++)
	{
		for(j=2; j<10; j++)
		{
			if(chess[j][i]=='k')
			{
				if(checkPp(j,i,'P') || checkRr(j,i,'R') || checkBb(j,i,'B') || checkQq(j,i,'Q') || checkKk(j,i,'K') || checkNn(j,i,'N'))
					black = true;
			}
			if(chess[j][i]=='K')
			{
				if(checkPp(j,i,'p') || checkRr(j,i,'r') || checkBb(j,i,'b') || checkQq(j,i,'q') || checkKk(j,i,'k') || checkNn(j,i,'n'))
					white = true;
			}
		}
	}

	if(black)
		cout << "Game #" <<gameNum << ": black king is in check."<<endl;
	else if(white)
		cout << "Game #" <<gameNum << ": white king is in check."<<endl;
	else
		cout << "Game #" <<gameNum << ": no king is in check." <<endl;
}

int main()
{
	exit_cond = 0;
	while(exit_cond!=64)
	{
		exit_cond = 0;
		for(i=0; i<12; i++)
		{
			for(j=0; j<12; j++)
			{
				if(i==0 || j==0 || i==1 || j==1 || i==10 || j==10 || i==11 || j==11)
					chess[j][i] = '*';
				else
					cin >> chess[j][i];
				if(chess[j][i]=='.')
					exit_cond++;
			}
		}
		if(exit_cond!=64)
			check();
	}
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:51
Processing time 0.0201 sec