Computer Programming Contest Preparation

ToolBox - Source for: 101/10196/chess.c



/home/toolbox/public_html/solutions/101/10196/chess.c
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 
    4 int KING[2],king[2];
    5 char board[8][8];
    6 char rook[2] = { 'R', 'r' };
    7 char bishop[2] = { 'B', 'b' };
    8 char knight[2] = { 'K', 'k' };
    9 char queen[2] = { 'Q', 'q' };
   10 char pawn[2] = { 'P', 'p' };
   11 
   12 int uporlow; /* 0 = lower, 1 = upper */
   13 
   14 int steveIrwinIsDead();
   15 void print();
   16 char nextdot(int x, int y, int dx, int dy);
   17 int game_num=0;
   18 
   19 void input(void)
   20 {
   21     int x = 0;
   22     game_num++;
   23     while(x < 8)
   24         {
   25             scanf("%c%c%c%c%c%c%c%c\n", &board[x][0], &board[x][1], &board[x][2], &board[x][3], &board[x][4], &board[x][5], &board[x][6], &board[x][7]);
   26             x++;
   27         }
   28     if(steveIrwinIsDead())
   29         {
   30             /* We are done bitches!! */
   31             exit(1);
   32         }
   33 }
   34 
   35 
   36 int steveIrwinIsDead()
   37 {
   38     /* Consults steve Irwin's Ghost to determine things */
   39     int x,y;
   40     int dead=1;
   41     for(x=0; x<8; x++)
   42         {
   43             for(y=0; y<8; y++)
   44                 {
   45                     if(board[x][y]!='.')
   46                         dead=0;
   47                     if(board[x][y]=='k')
   48                         {
   49                             king[0]=x;
   50                             king[1]=y;
   51                         }
   52                     else if (board[x][y]=='K')
   53                         {
   54                             KING[0]=x;
   55                             KING[1]=y;
   56                         }
   57                 }
   58         }
   59     return dead;
   60 }
   61 
   62 void print()
   63 {
   64     int x, y;
   65     for(x = 0; x < 8; x++)
   66         {
   67             for(y = 0; y < 8; y++)
   68                 {
   69                     printf("%c", board[x][y]);
   70                 }
   71             putchar('\n');
   72         }
   73 }
   74 int pawncheck(int x, int y)
   75 {
   76 
   77     if(0 == uporlow)
   78         {
   79             if(nextdot(x, y, -1, -1) == 'P')
   80                 return 1;
   81             if(nextdot(x, y, -1, 1) == 'P')
   82                 return 1;
   83         }
   84     if(1 == uporlow)
   85         {
   86             if(nextdot(x, y, 1, 1) == 'p')
   87                 return 1;
   88             if(nextdot(x, y, 1, -1) == 'p')
   89                 return 1;
   90         }
   91     return -1;
   92 }
   93 char nextdot(int x, int y, int dx, int dy)
   94 {
   95     if(7 < x+dx)
   96         return 'X';
   97     if(7 < y+dy)
   98         return 'X';
   99     if(0 > x+dx)
  100         return 'X';
  101     if(0 > y+dy)
  102         return 'X';
  103     if('k' == board[x+dx][y+dy] || 'K' == board[x+dx][y+dy])
  104         return '.';
  105     else
  106         return board[x+dx][y+dy];
  107 }
  108 int knightcheck(int x, int y)
  109 {
  110     int z,a;
  111     z = 2, a = 1;
  112 
  113     if(nextdot(x, y, z, a) == knight[uporlow])
  114         return 1;
  115     if(nextdot(x, y, -z, a) == knight[uporlow])
  116         return 1;
  117     if(nextdot(x, y, z, -a) == knight[uporlow])
  118         return 1;
  119     if(nextdot(x, y, -z, -a) == knight[uporlow])
  120         return 1;
  121     if(nextdot(x, y, a, z) == knight[uporlow])
  122         return 1;
  123     if(nextdot(x, y, -a, z) == knight[uporlow])
  124         return 1;
  125     if(nextdot(x, y, a, -z) == knight[uporlow])
  126         return 1;
  127     if(nextdot(x, y, -a, -z) == knight[uporlow])
  128         return 1;
  129     return -1;
  130 }
  131 int rookcheck(int x, int y)
  132 {
  133     int z;
  134     char temp;
  135     z = 0;
  136     while('.' == nextdot(x, y, z, 0))
  137         {
  138             z++;
  139         }
  140     temp = nextdot(x, y, z, 0);
  141     if(rook[uporlow] == temp || queen[uporlow] == temp)
  142         return 1;
  143     z = 0;
  144     while('.' == nextdot(x, y, -z, 0))
  145         {
  146             z++;
  147         }
  148     temp = nextdot(x, y, -z, 0);
  149     if(rook[uporlow] == temp || queen[uporlow] == temp)
  150         return 1;
  151     z = 0;
  152     while('.' == nextdot(x, y, 0, z))
  153         {
  154             z++;
  155         }
  156     temp = nextdot(x, y, 0, z);
  157     if(rook[uporlow] == temp || queen[uporlow] == temp)
  158         return 1;
  159     z = 0;
  160     while('.' == nextdot(x, y, 0, -z))
  161         {
  162             z++;
  163         }
  164     temp = nextdot(x, y, 0, -z);
  165     if(rook[uporlow] == temp || queen[uporlow] == temp)
  166         return 1;
  167     return -1;
  168 }
  169 
  170 int bishopcheck(int x, int y)
  171 {
  172     int z;
  173     char temp;
  174     z = 0;
  175     while('.' == nextdot(x, y, z, z))
  176         {
  177             z++;
  178         }
  179     temp = nextdot(x, y, z, z);
  180     if(bishop[uporlow] == temp || queen[uporlow] == temp)
  181         return 1;
  182     z = 0;
  183     while('.' == nextdot(x, y, -z, -z))
  184         {
  185             z++;
  186         }
  187     temp = nextdot(x, y, -z, -z);
  188     if(bishop[uporlow] == temp || queen[uporlow] == temp)
  189         return 1;
  190     z = 0;
  191     while('.' == nextdot(x, y, z, -z))
  192         {
  193             z++;
  194         }
  195     temp = nextdot(x, y, z, -z);
  196     if(bishop[uporlow] == temp || queen[uporlow] == temp)
  197         return 1;
  198     z = 0;
  199     while('.' == nextdot(x, y, -z, z))
  200         {
  201             z++;
  202         }
  203     temp = nextdot(x, y, -z, z);
  204     if(bishop[uporlow] == temp || queen[uporlow] == temp)
  205         return 1;
  206     return -1;
  207 }
  208 
  209 int main()
  210 {
  211     for(;;)
  212         {
  213             input();
  214             uporlow=1;
  215             if(1 == rookcheck(KING[0],KING[1]) || 1 == bishopcheck(KING[0],KING[1]) || 1 == knightcheck(KING[0],KING[1]) || 1 == pawncheck(KING[0],KING[1]))
  216                 {
  217                     printf("Game #%d:  white king is in check.\n",game_num);
  218                 }
  219             else
  220                 {
  221                     uporlow=0;
  222                     if(1 == rookcheck(king[0],king[1]) || 1 == bishopcheck(king[0],king[1]) || 1 == knightcheck(king[0],king[1]) || 1 == pawncheck(king[0],king[1]))
  223                         printf("Game #%d:  black king is in check.\n",game_num);
  224                     else
  225                         {
  226                             printf("Game #%d:  no king is in check.\n",game_num);
  227                         }
  228                 }
  229         }
  230     return 0;
  231 }
  232 
  233