More actions
imported>Unknown No edit summary |
(Repair batch-0001 pages from live compare) |
||
| Line 6: | Line 6: | ||
using namespace std; | using namespace std; | ||
char chess | char chess[12][12]; | ||
int exit_cond; | int exit_cond; | ||
int i, j, m, n; | int i, j, m, n; | ||
| Line 15: | Line 15: | ||
{ | { | ||
if(Pp=='P') | if(Pp=='P') | ||
return (chess | return (chess[x+1][y+1] ==Pp || chess[x-1][y+1]==Pp); | ||
else if(Pp=='p') | else if(Pp=='p') | ||
return (chess | return (chess[x+1][y-1] ==Pp || chess[x-1][y-1]==Pp); | ||
return false; | return false; | ||
} | } | ||
| Line 25: | Line 25: | ||
for(m=x-1; m>1; m--) | for(m=x-1; m>1; m--) | ||
{ | { | ||
if(chess | if(chess[m][y]==Rr) | ||
return true; | return true; | ||
else if(chess | else if(chess[m][y]!='.') | ||
break; | break; | ||
} | } | ||
for(m=x+1; m<10; m++) | for(m=x+1; m<10; m++) | ||
{ | { | ||
if(chess | if(chess[m][y]==Rr) | ||
return true; | return true; | ||
else if(chess | else if(chess[m][y]!='.') | ||
break; | break; | ||
} | } | ||
for(m=y-1; m>1; m--) | for(m=y-1; m>1; m--) | ||
{ | { | ||
if(chess | if(chess[x][m]==Rr) | ||
return true; | return true; | ||
else if(chess | else if(chess[x][m]!='.') | ||
break; | break; | ||
} | } | ||
for(m=y+1; m<10; m++) | for(m=y+1; m<10; m++) | ||
{ | { | ||
if(chess | if(chess[x][m]==Rr) | ||
return true; | return true; | ||
else if(chess | else if(chess[x][m]!='.') | ||
break; | break; | ||
} | } | ||
| Line 58: | Line 58: | ||
for(m=x-1, n=y-1; m>1 && n>1; m--, n--) | for(m=x-1, n=y-1; m>1 && n>1; m--, n--) | ||
{ | { | ||
if(chess | if(chess[m][n]==Bb) | ||
return true; | return true; | ||
else if(chess | else if(chess[m][n]!='.') | ||
break; | break; | ||
} | } | ||
for(m=x-1, n=y+1; m>1 && n<10; m--, n++) | for(m=x-1, n=y+1; m>1 && n<10; m--, n++) | ||
{ | { | ||
if(chess | if(chess[m][n]==Bb) | ||
return true; | return true; | ||
else if(chess | else if(chess[m][n]!='.') | ||
break; | break; | ||
} | } | ||
for(m=x+1, n=y-1; m<10 && n>1; m++, n--) | for(m=x+1, n=y-1; m<10 && n>1; m++, n--) | ||
{ | { | ||
if(chess | if(chess[m][n]==Bb) | ||
return true; | return true; | ||
else if(chess | else if(chess[m][n]!='.') | ||
break; | break; | ||
} | } | ||
for(m=x+1, n=y+1; m<10 && n<10; m++, n++) | for(m=x+1, n=y+1; m<10 && n<10; m++, n++) | ||
{ | { | ||
if(chess | if(chess[m][n]==Bb) | ||
return true; | return true; | ||
else if(chess | else if(chess[m][n]!='.') | ||
break; | break; | ||
} | } | ||
| Line 96: | Line 96: | ||
bool checkKk(int x, int y, char Kk) | bool checkKk(int x, int y, char Kk) | ||
{ | { | ||
if(chess | 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; | return true; | ||
else | else | ||
| Line 104: | Line 104: | ||
bool checkNn(int x, int y, char Nn) | bool checkNn(int x, int y, char Nn) | ||
{ | { | ||
if(chess | 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; | return true; | ||
else | else | ||
| Line 121: | Line 121: | ||
for(j=2; j<10; j++) | for(j=2; j<10; j++) | ||
{ | { | ||
if(chess | 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')) | 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; | black = true; | ||
} | } | ||
if(chess | 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')) | 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')) | ||
| Line 153: | Line 153: | ||
{ | { | ||
if(i==0 || j==0 || i==1 || j==1 || i==10 || j==10 || i==11 || j==11) | if(i==0 || j==0 || i==1 || j==1 || i==10 || j==10 || i==11 || j==11) | ||
chess | chess[j][i] = '*'; | ||
else | else | ||
cin >> chess | cin >> chess[j][i]; | ||
if(chess | if(chess[j][i]=='.') | ||
exit_cond++; | exit_cond++; | ||
} | } | ||
Latest revision as of 23:56, 26 March 2026
이야기
체계적인 설계를 하지 않고 짜서 그런지 헷갈리는 부분이 많았다.
소스
#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;
}