juanjo12x

UVA_10126_Check_The_Check

Jul 26th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cctype>
  4. #include <cstdlib>
  5. using namespace std;
  6. int rowBlack, colBlack, rowWhite, colWhite;
  7. char chessBoard[9][9];
  8.  
  9. bool readGame()
  10. {
  11. string tmp, myStr = "";
  12. int index = 0;
  13.     for(int a=0; a<8; a++)
  14.     {
  15.         cin>>tmp;
  16.         myStr += tmp;
  17.     }
  18.  
  19.     bool flag = false;
  20.     for(int x=0; x<8; x++)
  21.         for(int y=0; y<8; y++, index++)
  22.         {
  23.             chessBoard[x][y] = myStr[index];
  24.             if(myStr[index] != '.')
  25.                flag = true;
  26.             if(chessBoard[x][y] == 'k')
  27.             {
  28.                 rowBlack = x;
  29.                 colBlack = y;
  30.             }
  31.             if(chessBoard[x][y] == 'K')
  32.             {
  33.                 rowWhite = x;
  34.                 colWhite = y;
  35.             }
  36.         }
  37. return flag;
  38. }
  39.  
  40. bool checkRock(int a,int b, int kRow, int kCol, char king)
  41. {
  42.     if(a != kRow && b != kCol)
  43.        return false;
  44.     for(int x=a+1; x<8; x++)
  45.     {
  46.         if(chessBoard[x][b] == king)
  47.            return true;
  48.         else if(isalpha(chessBoard[x][b]) && chessBoard[x][b] != king)
  49.                break;
  50.     }
  51.     for(int x=a-1; x>=0; x--)
  52.     {
  53.         if(chessBoard[x][b] == king)
  54.            return true;
  55.         else if(isalpha(chessBoard[x][b]) && chessBoard[x][b] != king)
  56.                break;
  57.     }
  58.     for(int x=b+1; x<8; x++)
  59.     {
  60.         if(chessBoard[a][x] == king)
  61.            return true;
  62.         else if(isalpha(chessBoard[a][x]) && chessBoard[a][x] != king)
  63.                break;
  64.     }
  65.     for(int x=b-1; x>=0; x--)
  66.     {
  67.         if(chessBoard[a][x] == king)
  68.            return true;
  69.         else if(isalpha(chessBoard[a][x]) && chessBoard[a][x] != king)
  70.                break;
  71.     }
  72. return false;
  73. }
  74.  
  75. bool checkBishop(int x, int y, int rBlack, int cBlack, char king)
  76. {
  77.     if(abs(x - rBlack) - abs(y - cBlack) != 0)
  78.        return false;
  79.  
  80.     for(int a=x-1, b=y-1;  a>=0 && b>=0; a--, b--)
  81.     {
  82.         if(chessBoard[a][b] == king)
  83.            return true;
  84.         else if(isalpha(chessBoard[a][b]) && chessBoard[a][b] != king)
  85.                 break;
  86.     }
  87.     for(int a=x-1, b=y+1;  a>=0 && b<8; a--, b++)
  88.     {
  89.         if(chessBoard[a][b] == king)
  90.            return true;
  91.         else if(isalpha(chessBoard[a][b]) && chessBoard[a][b] != king)
  92.                 break;
  93.     }
  94.     for(int a=x+1, b=y-1;  a<8 && b>=0; a++, b--)
  95.     {
  96.         if(chessBoard[a][b] == king)
  97.            return true;
  98.         else if(isalpha(chessBoard[a][b]) && chessBoard[a][b] != king)
  99.                 break;
  100.     }
  101.     for(int a=x+1, b=y+1;  a<8 && b<8; a++, b++)
  102.     {
  103.         if(chessBoard[a][b] == king)
  104.            return true;
  105.         else if(isalpha(chessBoard[a][b]) && chessBoard[a][b] != king)
  106.                 break;
  107.     }
  108. return false;
  109. }
  110.  
  111. bool checkKnight(int x, int y, int rBlack, int cBlack, char king)
  112. {
  113.     if(x-2>=0 && y-1>=0 && chessBoard[x-2][y-1] == king)
  114.        return true;
  115.     else if(x-1>=0 && y-2>=0 && chessBoard[x-1][y-2] == king)
  116.             return true;
  117.     else if(x+1<8 && y-2>=0 && chessBoard[x+1][y-2] == king)
  118.            return true;
  119.     else if(x+2<8 && y-1>=0 && chessBoard[x+2][y-1] == king)
  120.            return true;
  121.     else if(x+2<8 && y+1<8 && chessBoard[x+2][y+1] == king)
  122.            return true;
  123.     else if(x+1<8 && y+2<8 && chessBoard[x+1][y+2] == king)
  124.            return true;
  125.     else if(x-1>=0 && y+2<8 && chessBoard[x-1][y+2] == king)
  126.            return true;
  127.     else if(x-2>=0 && y+1<8 && chessBoard[x-2][y+1] == king)
  128.            return true;
  129.     else
  130.          return false;
  131. }
  132.  
  133. bool checkPawn(int x,int y, int rowBlack, int colBlack, char king)
  134. {
  135.     if(x-1 >=0 && y-1 >=0 && chessBoard[x-1][y-1] == king)
  136.         return true;
  137.     else if(x-1 >=0 && y+1<8 && chessBoard[x-1][y+1] == king)
  138.            return true;
  139.     else
  140.         return false;
  141. }
  142.  
  143. bool checkPawnWhite(int x,int y, int rowBlack, int colBlack, char king)
  144. {
  145.     if(x+1<8 && y-1 >=0 && chessBoard[x+1][y-1] == king)
  146.         return true;
  147.     else if(x+1<8 && y+1<8 && chessBoard[x+1][y+1] == king)
  148.            return true;
  149.     else
  150.         return false;
  151. }
  152.  
  153. bool checkBlack()
  154. {
  155.     for(int x=0; x<8; x++)
  156.         for(int y=0; y<8; y++)
  157.         {
  158.             if(chessBoard[x][y] != '.' && isupper(chessBoard[x][y]))
  159.             {
  160.                 if(chessBoard[x][y] == 'R')
  161.                 {
  162.                     if(checkRock(x,y, rowBlack, colBlack, 'k'))
  163.                       return true;
  164.                 }
  165.                 else if(chessBoard[x][y] == 'B')
  166.                 {
  167.                     if(checkBishop(x,y, rowBlack, colBlack, 'k'))
  168.                        return true;
  169.                 }
  170.                 else if(chessBoard[x][y] == 'Q')
  171.                 {
  172.                     if(checkRock(x,y, rowBlack, colBlack, 'k') || checkBishop(x,y, rowBlack, colBlack, 'k'))
  173.                        return true;
  174.                 }
  175.                 else if(chessBoard[x][y] == 'N')
  176.                 {
  177.                     if(checkKnight(x, y, rowBlack, colBlack, 'k'))
  178.                        return true;
  179.                 }
  180.                 else if(chessBoard[x][y] == 'P')
  181.                 {
  182.                     if(checkPawn(x,y, rowBlack, colBlack, 'k'))
  183.                        return true;
  184.                 }
  185.             }
  186.         }
  187. return false;
  188. }
  189.  
  190. bool checkWhite()
  191. {
  192.     for(int x=0; x<8; x++)
  193.         for(int y=0; y<8; y++)
  194.         {
  195.             if(chessBoard[x][y] != '.' && islower(chessBoard[x][y]))
  196.             {
  197.                 if(chessBoard[x][y] == 'r')
  198.                 {
  199.                     if(checkRock(x,y, rowWhite, colWhite, 'K'))
  200.                       return true;
  201.                 }
  202.                 else if(chessBoard[x][y] == 'b')
  203.                 {
  204.                     if(checkBishop(x,y, rowWhite, colWhite, 'K'))
  205.                        return true;
  206.                 }
  207.                 else if(chessBoard[x][y] == 'q')
  208.                 {
  209.                     if(checkRock(x,y, rowWhite, colWhite, 'K') || checkBishop(x,y, rowWhite, colWhite, 'K'))
  210.                        return true;
  211.                 }
  212.                 else if(chessBoard[x][y] == 'n')
  213.                 {
  214.                     if(checkKnight(x, y, rowWhite, colWhite, 'K'))
  215.                        return true;
  216.                 }
  217.                 else if(chessBoard[x][y] == 'p')
  218.                 {
  219.                     if(checkPawnWhite(x,y, rowWhite, colWhite, 'K'))
  220.                        return true;
  221.                 }
  222.             }
  223.         }
  224. return false;
  225. }int main()
  226. {
  227. int casos = 1;
  228. while(readGame())
  229. {
  230.     if(checkBlack())
  231.        printf("Game #%d: black king is in check.\n", casos++);
  232.     else if(checkWhite())
  233.             printf("Game #%d: white king is in check.\n", casos++);
  234.     else
  235.         printf("Game #%d: no king is in check.\n", casos++);
  236. }
  237. return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment